openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

BaseOutput.class.php (2627B)


      1 <?php
      2 
      3 namespace cms\output;
      4 
      5 use BadMethodCallException;
      6 use cms\action\RequestParams;
      7 use cms\action\Response;
      8 use cms\base\Language as L;
      9 use cms\base\Startup;
     10 use cms\Dispatcher;
     11 use Exception;
     12 use util\Http;
     13 use logger\Logger;
     14 use \util\exception\ObjectNotFoundException;
     15 use util\exception\UIException;
     16 use util\exception\SecurityException;
     17 
     18 
     19 /**
     20  * base class for output.
     21  */
     22 abstract class BaseOutput implements Output
     23 {
     24 	/**
     25 	 * Outputting the data to the client.
     26 	 * Must be overwritten by subclasses.
     27 	 * @param $request RequestParams
     28 	 * @param $data array data array
     29 	 * @return void
     30 	 */
     31 	abstract protected function outputData($request, $data);
     32 
     33 	/**
     34 	 * Calling the dispatcher.
     35 	 */
     36 	public function execute()
     37     {
     38 		$request  = new RequestParams();
     39 		$response = new Response();
     40 
     41 		try {
     42 			$this->beforeAction( $request );
     43 
     44 			// special behaviour of UI actions, these are placed in a special package.
     45 			if   ( in_array( $request->action,['index','tree','title','usergroup']) )
     46 				$request->isUIAction = true;
     47 
     48 			$dispatcher = new Dispatcher();
     49 			$dispatcher->setRequestAndResponse( $request,$response );
     50 
     51 			$dispatcher->doAction();      // calling the action ...
     52 
     53 			if   ( $contentType = $this->getContentType() )
     54 				header('Content-Type: '.$contentType.'; charset='.Startup::CHARSET);
     55 
     56 			$response->setHTTPHeader();
     57 
     58 			$this->outputData( $request,$response->getOutputData() );  // ... and output the data
     59 
     60 		} catch (BadMethodCallException $e) {
     61 			// Action-Method does not exist.
     62 			Logger::debug( $e );
     63 			Http::noContent();
     64 			$this->setError("Method not found",$e);
     65 		} catch (ObjectNotFoundException $e) {
     66 			Logger::debug( $e ); // only debug, because this may happen on links to deleted objects.
     67 			Http::noContent();
     68 			$this->setError("No content",$e);
     69 		} catch (UIException $e) {
     70 			Logger::warn( $e );
     71 			$this->setError(L::lang($e->key,$e->params),$e);
     72 		} catch (SecurityException $e) {
     73 			Logger::info($e);
     74 			Http::forbidden();
     75 			$this->setError("You are not allowed to execute this action.",$e);
     76 		} catch (Exception $e) {
     77 			Logger::warn( $e );
     78 			// Sorry, our service is currently unavailable
     79 			Http::serverError();
     80 			$this->setError("Internal CMS error",$e);
     81 		}
     82     }
     83 
     84 
     85 	/**
     86 	 * This method is executed before the dispatcher is called.
     87 	 * Subclasses may override this to prepare the response.
     88 	 * @param $request RequestParams
     89 	 * @return void
     90 	 */
     91 	protected function beforeAction( $request )
     92 	{
     93 	}
     94 
     95 
     96 	/**
     97 	 * Is called if an error is thrown.
     98 	 *
     99 	 * @param $text string a message
    100 	 * @param $cause Exception
    101 	 */
    102 	abstract protected function setError($text, $cause);
    103 }