DefaultFormatter.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
12 
17 
18 /**
19  * Default Formatter implementation.
20  *
21  * @author ingo herwig <ingo@wemove.com>
22  */
23 class DefaultFormatter implements Formatter {
24 
25  private static $_headersSent = false;
26 
27  private $_formats = array();
28 
29  /**
30  * Constructor
31  * @param $formats Array of Format instances to use
32  */
33  public function __construct(array $formats) {
34  $this->_formats = $formats;
35  }
36 
37  /**
38  * @see Formatter::getFormatFromMimeType()
39  */
40  public function getFormatFromMimeType($mimeType) {
41  $firstFormat = null;
42  foreach ($this->_formats as $name => $instance) {
43  $firstFormat = $firstFormat == null ? $name : $firstFormat;
44  if (strpos($mimeType, $instance->getMimeType()) !== false) {
45  return $name;
46  }
47  }
48  if ($firstFormat == null) {
49  throw new ConfigurationException("Configuration section 'Formats' does not contain a format definition for: ".$mimeType);
50  }
51  return $firstFormat;
52  }
53 
54  /**
55  * @see Formatter::deserialize()
56  */
57  public function deserialize(Request $request) {
58  // get the format that should be used for this request format
59  $formatName = $request->getFormat();
60  if (strlen($formatName) == 0) {
61  // the format must be given!
62  throw new ConfigurationException("No request format defined for ".$request->__toString());
63  }
64  $format = $this->getFormat($formatName);
65  $format->deserialize($request);
66  }
67 
68  /**
69  * @see Formatter::serialize()
70  */
71  public function serialize(Response $response) {
72  self::$_headersSent = headers_sent();
73  self::sendHeader('HTTP/1.1 '.$response->getStatus());
74 
75  // if the response has a file, we send it and return
76  $file = $response->getFile();
77  if ($file) {
78  self::sendHeader("Content-Type: application/octet-stream");
79  self::sendHeader('Content-Disposition: attachment; filename="'.basename($file['filename']).'"');
80  self::sendHeader("Pragma: no-cache");
81  self::sendHeader("Expires: 0");
82  echo $file['content'];
83  return;
84  }
85 
86  // default: delegate to response format
87  $formatName = $response->getFormat();
88  if (strlen($formatName) == 0) {
89  // the format must be given!
90  throw new ConfigurationException("No response format defined for ".$response->__toString());
91  }
92  $format = $this->getFormat($formatName);
93  self::sendHeader("Content-Type: ".$format->getMimeType()."; charset=utf-8");
94  foreach ($response->getHeaders() as $name => $value) {
95  self::sendHeader($name.': '.$value);
96  }
97  $format->serialize($response);
98  }
99 
100  /**
101  * Send the given header
102  * @param $header
103  */
104  protected function sendHeader($header) {
105  if (!self::$_headersSent) {
106  header($header);
107  }
108  }
109 
110  /**
111  * Get the format with the given name
112  * @param $name
113  * @return Format
114  */
115  protected function getFormat($name) {
116  if (isset($this->_formats[$name])) {
117  return $this->_formats[$name];
118  }
119  throw new ConfigurationException("Configuration section 'Formats' does not contain a format definition with name: ".$name);
120  }
121 }
122 ?>
Response holds the response values that are used as output from Controller instances.
Definition: Response.php:20
getFormat()
Get the message format.
Formatter is the single entry point for request/response formatting.
Definition: Formatter.php:23
Request holds the request values that are used as input to Controller instances.
Definition: Request.php:20
getFormat($name)
Get the format with the given name.
getStatus()
Get the response HTTP status code.
getFile()
Get the file download.
ConfigurationException signals an exception in the configuration.