diff --git a/core/lib/Drupal/Core/Cache/Cacheability.php b/core/lib/Drupal/Core/Cache/Cacheability.php new file mode 100644 index 0000000..3d36289 --- /dev/null +++ b/core/lib/Drupal/Core/Cache/Cacheability.php @@ -0,0 +1,135 @@ +cacheTags = $cache_tags; + return $this; + } + + /** + * 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; + } + + /** + * 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; + } + + /** + * 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; + } + +} diff --git a/core/lib/Drupal/Core/Cache/CacheableMetadata.php b/core/lib/Drupal/Core/Cache/CacheableMetadata.php index 33429bf..83deb7d 100644 --- a/core/lib/Drupal/Core/Cache/CacheableMetadata.php +++ b/core/lib/Drupal/Core/Cache/CacheableMetadata.php @@ -7,83 +7,14 @@ namespace Drupal\Core\Cache; /** - * Defines a generic class for passing cacheability metadata. + * Defines a generic value object for passing cacheability metadata. * * @ingroup cache - * + * @internal + * @deprecated in Drupal 8.0.x, will be removed before Drupal 9.0.0. Use + * \Drupal\Core\Cache\Cacheability instead. */ -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; - } +class CacheableMetadata extends Cacheability { /** * Merges the values of another CacheableMetadata object with this one. @@ -131,59 +62,4 @@ public function merge(CacheableMetadata $other) { 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; - } - }