openrat-cms

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

OutputFactory.class.php (3228B)


      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 			{
    102 				Http::notAcceptable();
    103 				header('Content-Type: text/plain');
    104 				echo "Accepted output types are: ".implode(",",array_keys(self::MAP_OUTPUT));
    105 				exit;
    106 			}
    107 
    108 			return self::MAP_OUTPUT[ $reqOutput ];
    109 		}
    110 
    111 		// Try 2: Lets check the HTTP request "Accept" header.
    112 		//print_r(Http::getAccept());
    113 		foreach( Http::getAccept() as $acceptType )
    114 			if   ( array_key_exists( $acceptType, self::MAP_ACCEPT ) )
    115 				return self::MAP_ACCEPT[ $acceptType ];
    116 
    117 		// Fallback
    118 		Http::notAcceptable();
    119 		header('Content-Type: text/plain');
    120 		echo "Accepted types are: ".implode(",",array_keys(self::MAP_ACCEPT));
    121 		exit;
    122 	}
    123 
    124 }