CacheableResponseTrait.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace Drupal\Core\Cache;
  3. /**
  4. * Provides an implementation of CacheableResponseInterface.
  5. *
  6. * @see \Drupal\Core\Cache\CacheableResponseInterface
  7. */
  8. trait CacheableResponseTrait {
  9. /**
  10. * The cacheability metadata.
  11. *
  12. * @var \Drupal\Core\Cache\CacheableMetadata
  13. */
  14. protected $cacheabilityMetadata;
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public function addCacheableDependency($dependency) {
  19. // A trait doesn't have a constructor, so initialize the cacheability
  20. // metadata if that hasn't happened yet.
  21. if (!isset($this->cacheabilityMetadata)) {
  22. $this->cacheabilityMetadata = new CacheableMetadata();
  23. }
  24. $this->cacheabilityMetadata = $this->cacheabilityMetadata->merge(CacheableMetadata::createFromObject($dependency));
  25. return $this;
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function getCacheableMetadata() {
  31. // A trait doesn't have a constructor, so initialize the cacheability
  32. // metadata if that hasn't happened yet.
  33. if (!isset($this->cacheabilityMetadata)) {
  34. $this->cacheabilityMetadata = new CacheableMetadata();
  35. }
  36. return $this->cacheabilityMetadata;
  37. }
  38. }