JsonFormat.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 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 
19 
20 /**
21  * JsonFormat implements the JSON request/response format. All data will
22  * be serialized using the json_encode method except for Nodes.
23  * Nodes are serialized into an array before encoding (see JsonFormat::serializeValue)
24  * using the NodeSerializer class.
25  * On serialization the data will be outputted directly using the print command.
26  *
27  * JsonFormat collects the response data from all executed controllers
28  * into one response array and returns it all at once at the end of
29  * script execution. This prevents from having multiple chunks of JSON
30  * from each controller response that can't be decoded by clients.
31  *
32  * @author ingo herwig <ingo@wemove.com>
33  */
35  use CacheTrait;
36 
37  const CACHE_SECTION = 'jsonformat';
38 
39  private static $logger = null;
40 
41  protected $cache = null;
42  protected $serializer = null;
43 
44  /**
45  * Constructor
46  * @param $serializer NodeSerializer instance
47  * @param $dynamicCache Cache instance
48  */
49  public function __construct(NodeSerializer $serializer, Cache $dynamicCache) {
50  $this->serializer = $serializer;
51  $this->cache = $dynamicCache;
52  if (self::$logger == null) {
53  self::$logger = LogManager::getLogger(__CLASS__);
54  }
55  }
56 
57  /**
58  * @see Format::getMimeType()
59  */
60  public function getMimeType(Response $response=null) {
61  return 'application/json';
62  }
63 
64  /**
65  * @see CacheTrait::getCache()
66  */
67  protected function getCache() {
68  return $this->cache;
69  }
70 
71  /**
72  * @see CacheTrait::getBaseCacheSection()
73  */
74  protected function getBaseCacheSection() {
75  return self::CACHE_SECTION;
76  }
77 
78  /**
79  * @see AbstractFormat::afterSerialize()
80  */
81  protected function afterSerialize(Response $response) {
82  // output response payload
83  $caching = $this->isCaching($response);
84  if (!$caching || !$this->isCached($response)) {
85  // encode data
86  $payload = json_encode($response->getValues());
87  if (self::$logger->isDebugEnabled()) {
88  self::$logger->debug($response->getValues());
89  self::$logger->debug($payload);
90  }
91  // cache result
92  if ($caching) {
93  $this->putInCache($response, $payload);
94  }
95  }
96  else {
97  $payload = $this->getFromCache($response);
98  }
99  print($payload);
100 
101  return parent::afterSerialize($response);
102  }
103 
104  /**
105  * @see HierarchicalFormat::isSerializedNode()
106  */
107  protected function isSerializedNode($value) {
108  return $this->serializer->isSerializedNode($value);
109  }
110 
111  /**
112  * @see HierarchicalFormat::serializeNode()
113  */
114  protected function serializeNode($value) {
115  $node = $this->serializer->serializeNode($value);
116  return $node;
117  }
118 
119  /**
120  * @see HierarchicalFormat::deserializeNode()
121  */
122  protected function deserializeNode($value) {
123  $result = $this->serializer->deserializeNode($value);
124  return $result;
125  }
126 }
127 ?>
Response holds the response values that are used as output from Controller instances.
Definition: Response.php:20
NodeSerializer implementations are used to serialize Nodes into an array representation or deserializ...
JsonFormat implements the JSON request/response format.
Definition: JsonFormat.php:34
getValues()
Get all key value pairs.
putInCache(Response $response, $payload)
Store the payload of the response in the cache.
Definition: CacheTrait.php:78
trait CacheTrait
CacheTrait adds support for cached responses.
Definition: CacheTrait.php:21
__construct(NodeSerializer $serializer, Cache $dynamicCache)
Constructor.
Definition: JsonFormat.php:49
HierarchicalFormat is used as base class for formats that are able to represent hierarchical data lik...
static getLogger($name)
Get the logger with the given name.
Definition: LogManager.php:37
LogManager is used to retrieve Logger instances.
Definition: LogManager.php:20
getFromCache(Response $response)
Load the payload of the response from the cache.
Definition: CacheTrait.php:87
isCaching(Response $response)
Check if the response should be cached.
Definition: CacheTrait.php:69
Cache defines the interface for cache implementations.
Definition: Cache.php:21
isCached(Response $response)
Check if the response identified by it's cache id is cached for this format.