JsonFormat.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 
16 
17 /**
18  * JsonFormat realizes the JSON request/response format. All data will
19  * be serialized using the json_encode method except for Nodes.
20  * Nodes are serialized into an array before encoding (see JsonFormat::serializeValue)
21  * using the NodeSerializer class.
22  * On serialization the data will be outputted directly using the print command.
23  *
24  * JsonFormat collects the response data from all executed controllers
25  * into one response array and returns it all at once at the end of
26  * script execution. This prevents from having multiple junks of json
27  * from each controller response that can't be decoded by clients.
28  *
29  * @author ingo herwig <ingo@wemove.com>
30  */
32 
33  private static $_jsonData = array();
34  private static $_jsonUsed = false;
35  private static $_logger = null;
36 
37  protected $_serializer = null;
38 
39  /**
40  * Constructor
41  * @param $serializer NodeSerializer instance
42  */
43  public function __construct(NodeSerializer $serializer) {
44  $this->_serializer = $serializer;
45  if (self::$_logger == null) {
46  self::$_logger = LogManager::getLogger(__CLASS__);
47  }
48  }
49 
50  public static function printJSONResult() {
51  if (self::$_jsonUsed) {
52  $data = self::$_jsonData;
53  if ($data !== null) {
54  $encoded = json_encode($data);
55  if (self::$_logger->isDebugEnabled('JsonFormat')) {
56  self::$_logger->debug($data, 'JsonFormat');
57  self::$_logger->debug($encoded, 'JsonFormat');
58  }
59  print($encoded);
60  }
61  }
62  }
63 
64  /**
65  * @see Format::getMimeType()
66  */
67  public function getMimeType() {
68  return 'application/json';
69  }
70 
71  /**
72  * @see HierarchicalFormat::afterSerialize()
73  */
74  protected function afterSerialize($values) {
75  // TODO: check if merging is required for multiple actions
76  /*
77  // merge data into global data array
78  // new values override old
79  self::$_jsonData = array_merge(self::$_jsonData, $data);
80  */
81  self::$_jsonData = $values;
82  self::$_jsonUsed = true;
83  return $values;
84  }
85 
86  /**
87  * @see HierarchicalFormat::isSerializedNode()
88  */
89  protected function isSerializedNode($value) {
90  return $this->_serializer->isSerializedNode($value);
91  }
92 
93  /**
94  * @see HierarchicalFormat::serializeNode()
95  */
96  protected function serializeNode($value) {
97  $node = $this->_serializer->serializeNode($value);
98  return $node;
99  }
100 
101  /**
102  * @see HierarchicalFormat::deserializeNode()
103  */
104  protected function deserializeNode($value) {
105  $result = $this->_serializer->deserializeNode($value);
106  return $result;
107  }
108 }
109 
110 // register the output method
111 register_shutdown_function(array(__NAMESPACE__.'\JsonFormat', 'printJSONResult'));
112 ?>
JsonFormat realizes the JSON request/response format.
Definition: JsonFormat.php:31
NodeSerializer implementations are used to serialize Nodes into an array representation or deserializ...
__construct(NodeSerializer $serializer)
Constructor.
Definition: JsonFormat.php:43
static getLogger($name)
Get the logger with the given name.
Definition: LogManager.php:35
HierarchicalFormat maybe used as base class for formats that are able to represent hierarchical data ...