comparison core/lib/Drupal/Core/Cache/CacheableMetadata.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Core\Cache;
4
5 /**
6 * Defines a generic class for passing cacheability metadata.
7 *
8 * @ingroup cache
9 */
10 class CacheableMetadata implements RefinableCacheableDependencyInterface {
11
12 use RefinableCacheableDependencyTrait;
13
14 /**
15 * {@inheritdoc}
16 */
17 public function getCacheTags() {
18 return $this->cacheTags;
19 }
20
21 /**
22 * Sets cache tags.
23 *
24 * @param string[] $cache_tags
25 * The cache tags to be associated.
26 *
27 * @return $this
28 */
29 public function setCacheTags(array $cache_tags) {
30 $this->cacheTags = $cache_tags;
31 return $this;
32 }
33
34 /**
35 * {@inheritdoc}
36 */
37 public function getCacheContexts() {
38 return $this->cacheContexts;
39 }
40
41 /**
42 * Sets cache contexts.
43 *
44 * @param string[] $cache_contexts
45 * The cache contexts to be associated.
46 *
47 * @return $this
48 */
49 public function setCacheContexts(array $cache_contexts) {
50 $this->cacheContexts = $cache_contexts;
51 return $this;
52 }
53
54 /**
55 * {@inheritdoc}
56 */
57 public function getCacheMaxAge() {
58 return $this->cacheMaxAge;
59 }
60
61 /**
62 * Sets the maximum age (in seconds).
63 *
64 * Defaults to Cache::PERMANENT
65 *
66 * @param int $max_age
67 * The max age to associate.
68 *
69 * @return $this
70 *
71 * @throws \InvalidArgumentException
72 * If a non-integer value is supplied.
73 */
74 public function setCacheMaxAge($max_age) {
75 if (!is_int($max_age)) {
76 throw new \InvalidArgumentException('$max_age must be an integer');
77 }
78
79 $this->cacheMaxAge = $max_age;
80 return $this;
81 }
82
83 /**
84 * Merges the values of another CacheableMetadata object with this one.
85 *
86 * @param \Drupal\Core\Cache\CacheableMetadata $other
87 * The other CacheableMetadata object.
88 *
89 * @return static
90 * A new CacheableMetadata object, with the merged data.
91 */
92 public function merge(CacheableMetadata $other) {
93 $result = clone $this;
94
95 // This is called many times per request, so avoid merging unless absolutely
96 // necessary.
97 if (empty($this->cacheContexts)) {
98 $result->cacheContexts = $other->cacheContexts;
99 }
100 elseif (empty($other->cacheContexts)) {
101 $result->cacheContexts = $this->cacheContexts;
102 }
103 else {
104 $result->cacheContexts = Cache::mergeContexts($this->cacheContexts, $other->cacheContexts);
105 }
106
107 if (empty($this->cacheTags)) {
108 $result->cacheTags = $other->cacheTags;
109 }
110 elseif (empty($other->cacheTags)) {
111 $result->cacheTags = $this->cacheTags;
112 }
113 else {
114 $result->cacheTags = Cache::mergeTags($this->cacheTags, $other->cacheTags);
115 }
116
117 if ($this->cacheMaxAge === Cache::PERMANENT) {
118 $result->cacheMaxAge = $other->cacheMaxAge;
119 }
120 elseif ($other->cacheMaxAge === Cache::PERMANENT) {
121 $result->cacheMaxAge = $this->cacheMaxAge;
122 }
123 else {
124 $result->cacheMaxAge = Cache::mergeMaxAges($this->cacheMaxAge, $other->cacheMaxAge);
125 }
126 return $result;
127 }
128
129 /**
130 * Applies the values of this CacheableMetadata object to a render array.
131 *
132 * @param array &$build
133 * A render array.
134 */
135 public function applyTo(array &$build) {
136 $build['#cache']['contexts'] = $this->cacheContexts;
137 $build['#cache']['tags'] = $this->cacheTags;
138 $build['#cache']['max-age'] = $this->cacheMaxAge;
139 }
140
141 /**
142 * Creates a CacheableMetadata object with values taken from a render array.
143 *
144 * @param array $build
145 * A render array.
146 *
147 * @return static
148 */
149 public static function createFromRenderArray(array $build) {
150 $meta = new static();
151 $meta->cacheContexts = (isset($build['#cache']['contexts'])) ? $build['#cache']['contexts'] : [];
152 $meta->cacheTags = (isset($build['#cache']['tags'])) ? $build['#cache']['tags'] : [];
153 $meta->cacheMaxAge = (isset($build['#cache']['max-age'])) ? $build['#cache']['max-age'] : Cache::PERMANENT;
154 return $meta;
155 }
156
157 /**
158 * Creates a CacheableMetadata object from a depended object.
159 *
160 * @param \Drupal\Core\Cache\CacheableDependencyInterface|mixed $object
161 * The object whose cacheability metadata to retrieve. If it implements
162 * CacheableDependencyInterface, its cacheability metadata will be used,
163 * otherwise, the passed in object must be assumed to be uncacheable, so
164 * max-age 0 is set.
165 *
166 * @return static
167 */
168 public static function createFromObject($object) {
169 if ($object instanceof CacheableDependencyInterface) {
170 $meta = new static();
171 $meta->cacheContexts = $object->getCacheContexts();
172 $meta->cacheTags = $object->getCacheTags();
173 $meta->cacheMaxAge = $object->getCacheMaxAge();
174 return $meta;
175 }
176
177 // Objects that don't implement CacheableDependencyInterface must be assumed
178 // to be uncacheable, so set max-age 0.
179 $meta = new static();
180 $meta->cacheMaxAge = 0;
181 return $meta;
182 }
183
184 }