CacheableDependencyTrait.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace Drupal\Core\Cache;
  3. /**
  4. * Trait for \Drupal\Core\Cache\CacheableDependencyInterface.
  5. */
  6. trait CacheableDependencyTrait {
  7. /**
  8. * Cache contexts.
  9. *
  10. * @var string[]
  11. */
  12. protected $cacheContexts = [];
  13. /**
  14. * Cache tags.
  15. *
  16. * @var string[]
  17. */
  18. protected $cacheTags = [];
  19. /**
  20. * Cache max-age.
  21. *
  22. * @var int
  23. */
  24. protected $cacheMaxAge = Cache::PERMANENT;
  25. /**
  26. * Sets cacheability; useful for value object constructors.
  27. *
  28. * @param \Drupal\Core\Cache\CacheableDependencyInterface $cacheability
  29. * The cacheability to set.
  30. *
  31. * @return $this
  32. */
  33. protected function setCacheability(CacheableDependencyInterface $cacheability) {
  34. $this->cacheContexts = $cacheability->getCacheContexts();
  35. $this->cacheTags = $cacheability->getCacheTags();
  36. $this->cacheMaxAge = $cacheability->getCacheMaxAge();
  37. return $this;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function getCacheTags() {
  43. return $this->cacheTags;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function getCacheContexts() {
  49. return $this->cacheContexts;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function getCacheMaxAge() {
  55. return $this->cacheMaxAge;
  56. }
  57. }