123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?php
- namespace Drupal\Core\Cache;
- /**
- * Defines a generic class for passing cacheability metadata.
- *
- * @ingroup cache
- */
- class CacheableMetadata implements RefinableCacheableDependencyInterface {
- use RefinableCacheableDependencyTrait;
- /**
- * {@inheritdoc}
- */
- public function getCacheTags() {
- return $this->cacheTags;
- }
- /**
- * Sets cache tags.
- *
- * @param string[] $cache_tags
- * The cache tags to be associated.
- *
- * @return $this
- */
- public function setCacheTags(array $cache_tags) {
- $this->cacheTags = $cache_tags;
- return $this;
- }
- /**
- * {@inheritdoc}
- */
- public function getCacheContexts() {
- return $this->cacheContexts;
- }
- /**
- * Sets cache contexts.
- *
- * @param string[] $cache_contexts
- * The cache contexts to be associated.
- *
- * @return $this
- */
- public function setCacheContexts(array $cache_contexts) {
- $this->cacheContexts = $cache_contexts;
- return $this;
- }
- /**
- * {@inheritdoc}
- */
- public function getCacheMaxAge() {
- return $this->cacheMaxAge;
- }
- /**
- * Sets the maximum age (in seconds).
- *
- * Defaults to Cache::PERMANENT
- *
- * @param int $max_age
- * The max age to associate.
- *
- * @return $this
- *
- * @throws \InvalidArgumentException
- * If a non-integer value is supplied.
- */
- public function setCacheMaxAge($max_age) {
- if (!is_int($max_age)) {
- throw new \InvalidArgumentException('$max_age must be an integer');
- }
- $this->cacheMaxAge = $max_age;
- return $this;
- }
- /**
- * Merges the values of another CacheableMetadata object with this one.
- *
- * @param \Drupal\Core\Cache\CacheableMetadata $other
- * The other CacheableMetadata object.
- *
- * @return static
- * A new CacheableMetadata object, with the merged data.
- */
- public function merge(CacheableMetadata $other) {
- $result = clone $this;
- // This is called many times per request, so avoid merging unless absolutely
- // necessary.
- if (empty($this->cacheContexts)) {
- $result->cacheContexts = $other->cacheContexts;
- }
- elseif (empty($other->cacheContexts)) {
- $result->cacheContexts = $this->cacheContexts;
- }
- else {
- $result->cacheContexts = Cache::mergeContexts($this->cacheContexts, $other->cacheContexts);
- }
- if (empty($this->cacheTags)) {
- $result->cacheTags = $other->cacheTags;
- }
- elseif (empty($other->cacheTags)) {
- $result->cacheTags = $this->cacheTags;
- }
- else {
- $result->cacheTags = Cache::mergeTags($this->cacheTags, $other->cacheTags);
- }
- if ($this->cacheMaxAge === Cache::PERMANENT) {
- $result->cacheMaxAge = $other->cacheMaxAge;
- }
- elseif ($other->cacheMaxAge === Cache::PERMANENT) {
- $result->cacheMaxAge = $this->cacheMaxAge;
- }
- else {
- $result->cacheMaxAge = Cache::mergeMaxAges($this->cacheMaxAge, $other->cacheMaxAge);
- }
- return $result;
- }
- /**
- * Applies the values of this CacheableMetadata object to a render array.
- *
- * @param array &$build
- * A render array.
- */
- public function applyTo(array &$build) {
- $build['#cache']['contexts'] = $this->cacheContexts;
- $build['#cache']['tags'] = $this->cacheTags;
- $build['#cache']['max-age'] = $this->cacheMaxAge;
- }
- /**
- * Creates a CacheableMetadata object with values taken from a render array.
- *
- * @param array $build
- * A render array.
- *
- * @return static
- */
- public static function createFromRenderArray(array $build) {
- $meta = new static();
- $meta->cacheContexts = (isset($build['#cache']['contexts'])) ? $build['#cache']['contexts'] : [];
- $meta->cacheTags = (isset($build['#cache']['tags'])) ? $build['#cache']['tags'] : [];
- $meta->cacheMaxAge = (isset($build['#cache']['max-age'])) ? $build['#cache']['max-age'] : Cache::PERMANENT;
- return $meta;
- }
- /**
- * Creates a CacheableMetadata object from a depended object.
- *
- * @param \Drupal\Core\Cache\CacheableDependencyInterface|mixed $object
- * The object whose cacheability metadata to retrieve. If it implements
- * CacheableDependencyInterface, its cacheability metadata will be used,
- * otherwise, the passed in object must be assumed to be uncacheable, so
- * max-age 0 is set.
- *
- * @return static
- */
- public static function createFromObject($object) {
- if ($object instanceof CacheableDependencyInterface) {
- $meta = new static();
- $meta->cacheContexts = $object->getCacheContexts();
- $meta->cacheTags = $object->getCacheTags();
- $meta->cacheMaxAge = $object->getCacheMaxAge();
- return $meta;
- }
- // Objects that don't implement CacheableDependencyInterface must be assumed
- // to be uncacheable, so set max-age 0.
- $meta = new static();
- $meta->cacheMaxAge = 0;
- return $meta;
- }
- }
|