3 namespace Drupal\Core\Cache;
6 * Trait for \Drupal\Core\Cache\RefinableCacheableDependencyInterface.
8 trait RefinableCacheableDependencyTrait {
15 protected $cacheContexts = [];
22 protected $cacheTags = [];
29 protected $cacheMaxAge = Cache::PERMANENT;
34 public function getCacheTags() {
35 return $this->cacheTags;
41 public function getCacheContexts() {
42 return $this->cacheContexts;
48 public function getCacheMaxAge() {
49 return $this->cacheMaxAge;
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());
62 // Not a cacheable dependency, this can not be cached.
63 $this->cacheMaxAge = 0;
71 public function addCacheContexts(array $cache_contexts) {
72 if ($cache_contexts) {
73 $this->cacheContexts = Cache::mergeContexts($this->cacheContexts, $cache_contexts);
81 public function addCacheTags(array $cache_tags) {
83 $this->cacheTags = Cache::mergeTags($this->cacheTags, $cache_tags);
91 public function mergeCacheMaxAge($max_age) {
92 $this->cacheMaxAge = Cache::mergeMaxAges($this->cacheMaxAge, $max_age);