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

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Core\Cache;
4
5 /**
6 * Trait for \Drupal\Core\Cache\RefinableCacheableDependencyInterface.
7 */
8 trait RefinableCacheableDependencyTrait {
9
10 /**
11 * Cache contexts.
12 *
13 * @var string[]
14 */
15 protected $cacheContexts = [];
16
17 /**
18 * Cache tags.
19 *
20 * @var string[]
21 */
22 protected $cacheTags = [];
23
24 /**
25 * Cache max-age.
26 *
27 * @var int
28 */
29 protected $cacheMaxAge = Cache::PERMANENT;
30
31 /**
32 * {@inheritdoc}
33 */
34 public function getCacheTags() {
35 return $this->cacheTags;
36 }
37
38 /**
39 * {@inheritdoc}
40 */
41 public function getCacheContexts() {
42 return $this->cacheContexts;
43 }
44
45 /**
46 * {@inheritdoc}
47 */
48 public function getCacheMaxAge() {
49 return $this->cacheMaxAge;
50 }
51
52 /**
53 * {@inheritdoc}
54 */
55 public function addCacheableDependency($other_object) {
56 if ($other_object instanceof CacheableDependencyInterface) {
57 $this->addCacheContexts($other_object->getCacheContexts());
58 $this->addCacheTags($other_object->getCacheTags());
59 $this->mergeCacheMaxAge($other_object->getCacheMaxAge());
60 }
61 else {
62 // Not a cacheable dependency, this can not be cached.
63 $this->cacheMaxAge = 0;
64 }
65 return $this;
66 }
67
68 /**
69 * {@inheritdoc}
70 */
71 public function addCacheContexts(array $cache_contexts) {
72 if ($cache_contexts) {
73 $this->cacheContexts = Cache::mergeContexts($this->cacheContexts, $cache_contexts);
74 }
75 return $this;
76 }
77
78 /**
79 * {@inheritdoc}
80 */
81 public function addCacheTags(array $cache_tags) {
82 if ($cache_tags) {
83 $this->cacheTags = Cache::mergeTags($this->cacheTags, $cache_tags);
84 }
85 return $this;
86 }
87
88 /**
89 * {@inheritdoc}
90 */
91 public function mergeCacheMaxAge($max_age) {
92 $this->cacheMaxAge = Cache::mergeMaxAges($this->cacheMaxAge, $max_age);
93 return $this;
94 }
95
96 }