RefinableCacheableDependencyTrait.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace Drupal\Core\Cache;
  3. /**
  4. * Trait for \Drupal\Core\Cache\RefinableCacheableDependencyInterface.
  5. */
  6. trait RefinableCacheableDependencyTrait {
  7. use CacheableDependencyTrait;
  8. /**
  9. * {@inheritdoc}
  10. */
  11. public function addCacheableDependency($other_object) {
  12. if ($other_object instanceof CacheableDependencyInterface) {
  13. $this->addCacheContexts($other_object->getCacheContexts());
  14. $this->addCacheTags($other_object->getCacheTags());
  15. $this->mergeCacheMaxAge($other_object->getCacheMaxAge());
  16. }
  17. else {
  18. // Not a cacheable dependency, this can not be cached.
  19. $this->cacheMaxAge = 0;
  20. }
  21. return $this;
  22. }
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function addCacheContexts(array $cache_contexts) {
  27. if ($cache_contexts) {
  28. $this->cacheContexts = Cache::mergeContexts($this->cacheContexts, $cache_contexts);
  29. }
  30. return $this;
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function addCacheTags(array $cache_tags) {
  36. if ($cache_tags) {
  37. $this->cacheTags = Cache::mergeTags($this->cacheTags, $cache_tags);
  38. }
  39. return $this;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function mergeCacheMaxAge($max_age) {
  45. $this->cacheMaxAge = Cache::mergeMaxAges($this->cacheMaxAge, $max_age);
  46. return $this;
  47. }
  48. }