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

Last commit: Tue Feb 14 22:27:17 2023 +0100	Jan Dankert	Fix: Add HTTP-Response-Header 'Vary' if the Output is controled bý the Accept-Request-Header.
1 <?php 2 3 namespace cms\output; 4 5 use cms\action\RequestParams; 6 use util\Http; 7 8 class OutputFactory { 9 10 const OUTPUT_PHPARRAY = 1; 11 const OUTPUT_PHPSERIALIZE = 2; 12 const OUTPUT_JSON = 3; 13 const OUTPUT_XML = 4; 14 const OUTPUT_YAML = 5; 15 const OUTPUT_UI = 6; 16 const OUTPUT_PLAIN = 7; 17 const OUTPUT_CSS = 8; 18 const OUTPUT_PREVIEW = 9; 19 20 21 /** 22 * Map 'output' request to output type. 23 */ 24 const MAP_OUTPUT = [ 25 'php-array' => self::OUTPUT_PHPARRAY, 26 'php' => self::OUTPUT_PHPSERIALIZE, 27 'json' => self::OUTPUT_JSON, 28 'xml' => self::OUTPUT_XML, 29 'yaml' => self::OUTPUT_YAML, 30 'plain' => self::OUTPUT_PLAIN, 31 'html' => self::OUTPUT_UI, 32 'css' => self::OUTPUT_CSS, 33 'preview' => self::OUTPUT_PREVIEW, 34 ]; 35 36 /** 37 * Map Accept-Header to Output type. 38 */ 39 const MAP_ACCEPT = [ 40 'application/php-array' => self::OUTPUT_PHPARRAY, 41 'application/php-serialized' => self::OUTPUT_PHPSERIALIZE, 42 'text/json' => self::OUTPUT_JSON, 43 'application/json' => self::OUTPUT_JSON, 44 'text/xml' => self::OUTPUT_XML, 45 'application/xml' => self::OUTPUT_XML, 46 'application/yaml' => self::OUTPUT_YAML, 47 'application/xhtml+xml' => self::OUTPUT_UI, 48 'text/html' => self::OUTPUT_UI, 49 'text/css' => self::OUTPUT_CSS, 50 //'*/*' => self::OUTPUT_HTML, 51 ]; 52 53 54 /** 55 * Creates the output driver. 56 * 57 * Dependent on the HTTP request a output driver will be selected. 58 * 59 * @return Output 60 */ 61 public static function createOutput() { 62 63 switch ( self::discoverOutputType() ) { 64 65 case self::OUTPUT_PHPARRAY: 66 return new PHPArrayOutput(); 67 case self::OUTPUT_PHPSERIALIZE: 68 return new PHPSerializeOutput(); 69 case self::OUTPUT_JSON: 70 return new JsonOutput(); 71 case self::OUTPUT_XML: 72 return new XmlOutput(); 73 case self::OUTPUT_YAML: 74 return new YamlOutput(); 75 case self::OUTPUT_UI: 76 return new UIOutput(); 77 case self::OUTPUT_CSS: 78 return new CssOutput(); 79 case self::OUTPUT_PREVIEW: 80 return new PreviewOutput(); 81 case self::OUTPUT_PLAIN: 82 default: 83 return new PlainOutput(); 84 } 85 } 86 87 88 89 /** 90 * Discovering the output-type for this request 91 * 92 * @return int constant of self::OUTPUT_* 93 */ 94 private static function discoverOutputType() 95 { 96 $reqOutput = strtolower(@$_REQUEST[RequestParams::PARAM_OUTPUT]); 97 98 // Try 1: Checking the 'output' request parameter. 99 if ($reqOutput) { 100 if (!array_key_exists($reqOutput, self::MAP_OUTPUT)) { 101 Http::notAcceptable(); 102 header('Content-Type: text/plain'); 103 echo "Accepted output types are: " . implode(",", array_keys(self::MAP_OUTPUT)); 104 exit; 105 } 106 107 return self::MAP_OUTPUT[$reqOutput]; 108 } 109 110 // Try 2: Lets check the HTTP request "Accept" header. 111 //print_r(Http::getAccept()); 112 foreach (Http::getAccept() as $acceptType) 113 if (array_key_exists($acceptType, self::MAP_ACCEPT)) { 114 header('Vary: Accept'); 115 return self::MAP_ACCEPT[$acceptType]; 116 } 117 118 // Fallback 119 Http::notAcceptable(); 120 header('Content-Type: text/plain'); 121 echo "Accepted types are: ".implode(",",array_keys(self::MAP_ACCEPT)); 122 exit; 123 } 124 125 }
Download modules/cms/output/OutputFactory.class.php
History Tue, 14 Feb 2023 22:27:17 +0100 Jan Dankert Fix: Add HTTP-Response-Header 'Vary' if the Output is controled bý the Accept-Request-Header. Sun, 13 Feb 2022 19:39:49 +0100 dankert Refactoring: Special output type "preview" for previewing pages and files. 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 "/".