File modules/cms/output/BaseOutput.class.php

Last commit: Sat Mar 19 01:29:19 2022 +0100	dankert	Fix: PreviewOutput should inherit from UIOutput.
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 }
Download modules/cms/output/BaseOutput.class.php
History Sat, 19 Mar 2022 01:29:19 +0100 dankert Fix: PreviewOutput should inherit from UIOutput. Sat, 19 Mar 2022 00:09:47 +0100 dankert Refactoring: Outputs are setting their content-type themself. Sun, 13 Feb 2022 23:35:26 +0100 dankert Refactoring: New class "Response" which stores all output information. Sun, 6 Feb 2022 21:56:52 +0100 dankert New CssOutput which outputs the CSS in a cleaner way. Wed, 2 Feb 2022 01:12:42 +0100 dankert Better support for API requests. Mon, 31 Jan 2022 00:00:48 +0100 dankert Some documentation... Sun, 30 Jan 2022 23:38:42 +0100 dankert Refactoring: Only 1 http-endpoint for both the UI and the API. Path "/api" is not available any more, all API data is served under "/".