GenericFormat.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 
18 
19 /**
20  * GenericFormat is used to output arbitrary responses. It prints the
21  * content of the 'body' value of the response. The mime type is defined
22  * by the 'mime_type' value of the response. The default cache section can
23  * be set with the 'cache_section' value of the response.
24  *
25  * @author ingo herwig <ingo@wemove.com>
26  */
28  use CacheTrait;
29 
30  const CACHE_SECTION = 'genericformat';
31 
32  protected $cache = null;
33 
34  /**
35  * Constructor
36  * @param $dynamicCache Cache instance
37  */
38  public function __construct(Cache $dynamicCache) {
39  $this->cache = $dynamicCache;
40  }
41 
42  /**
43  * @see Format::getMimeType()
44  */
45  public function getMimeType(Response $response=null) {
46  return $response ? $response->getValue('mime_type') : '';
47  }
48 
49  /**
50  * @see CacheTrait::getCache()
51  */
52  protected function getCache() {
53  return $this->cache;
54  }
55 
56  /**
57  * @see CacheTrait::getBaseCacheSection()
58  */
59  protected function getBaseCacheSection() {
60  return self::CACHE_SECTION;
61  }
62 
63  /**
64  * @see AbstractFormat::deserializeValues()
65  */
66  protected function deserializeValues(Request $request) {
67  return $request->getValues();
68  }
69 
70  /**
71  * @see AbstractFormat::serializeValues()
72  */
73  protected function serializeValues(Response $response) {
74  return $response->getValues();
75  }
76 
77  /**
78  * @see AbstractFormat::afterSerialize()
79  */
80  protected function afterSerialize(Response $response) {
81  // output response payload
82  $caching = $this->isCaching($response);
83  if (!$caching || !$this->isCached($response)) {
84  $payload = $response->getValue('body');
85  // cache result
86  if ($caching) {
87  $this->putInCache($response, $payload);
88  }
89  }
90  else {
91  $payload = $this->getFromCache($response);
92  }
93  print($payload);
94 
95  return parent::afterSerialize($response);
96  }
97 }
98 ?>
Response holds the response values that are used as output from Controller instances.
Definition: Response.php:20
Request holds the request values that are used as input to Controller instances.
Definition: Request.php:18
GenericFormat is used to output arbitrary responses.
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
getValue($name, $default=null, $validateDesc=null, $suppressException=false)
Get a value.
Format defines the interface for all format classes.
Definition: Format.php:25
__construct(Cache $dynamicCache)
Constructor.
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
AbstractFormat is used as base class for specialized formats.
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.