From a0ee087d7a64cbb549227e07443e9322b57ff6f0 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Thu, 16 Apr 2015 12:59:35 +0200 Subject: [PATCH] Issue #2471743 by pwolanin: Create a more generic superclass of \Drupal\Core\Render\BubbleableMetadata --- .../Drupal/Core/Cache/CacheableMetadata.php | 132 ++++++++++++++++++ .../Drupal/Core/Render/BubbleableMetadata.php | 103 +------------- .../Core/Cache/CacheableMetadataTest.php | 72 ++++++++++ 3 files changed, 206 insertions(+), 101 deletions(-) create mode 100644 core/lib/Drupal/Core/Cache/CacheableMetadata.php create mode 100644 core/tests/Drupal/Tests/Core/Cache/CacheableMetadataTest.php diff --git a/core/lib/Drupal/Core/Cache/CacheableMetadata.php b/core/lib/Drupal/Core/Cache/CacheableMetadata.php new file mode 100644 index 00000000000..04088b43849 --- /dev/null +++ b/core/lib/Drupal/Core/Cache/CacheableMetadata.php @@ -0,0 +1,132 @@ +tags; + } + + /** + * Adds cache tags. + * + * @param string[] $cache_tags + * The cache tags to be added. + * + * @return $this + */ + public function addCacheTags(array $cache_tags) { + $this->tags = Cache::mergeTags($this->tags, $cache_tags); + return $this; + } + + /** + * Sets cache tags. + * + * @param string[] $cache_tags + * The cache tags to be associated. + * + * @return $this + */ + public function setCacheTags(array $cache_tags) { + $this->tags = $cache_tags; + return $this; + } + + /** + * {@inheritdoc} + */ + public function getCacheContexts() { + return $this->contexts; + } + + /** + * Adds cache contexts. + * + * @param string[] $cache_contexts + * The cache contexts to be added. + * + * @return $this + */ + public function addCacheContexts(array $cache_contexts) { + $this->contexts = Cache::mergeContexts($this->contexts, $cache_contexts); + return $this; + } + + /** + * Sets cache contexts. + * + * @param string[] $cache_contexts + * The cache contexts to be associated. + * + * @return $this + */ + public function setCacheContexts(array $cache_contexts) { + $this->contexts = $cache_contexts; + return $this; + } + + /** + * {@inheritdoc} + */ + public function getCacheMaxAge() { + return $this->maxAge; + } + + /** + * 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->maxAge = $max_age; + return $this; + } + +} diff --git a/core/lib/Drupal/Core/Render/BubbleableMetadata.php b/core/lib/Drupal/Core/Render/BubbleableMetadata.php index bf7812e618a..b51c055bd7e 100644 --- a/core/lib/Drupal/Core/Render/BubbleableMetadata.php +++ b/core/lib/Drupal/Core/Render/BubbleableMetadata.php @@ -10,13 +10,14 @@ namespace Drupal\Core\Render; use Drupal\Component\Utility\NestedArray; use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheableDependencyInterface; +use Drupal\Core\Cache\CacheableMetadata; /** * Value object used for bubbleable rendering metadata. * * @see \Drupal\Core\Render\RendererInterface::render() */ -class BubbleableMetadata implements CacheableDependencyInterface { +class BubbleableMetadata extends CacheableMetadata { /** * Cache contexts. @@ -132,106 +133,6 @@ class BubbleableMetadata implements CacheableDependencyInterface { return $meta; } - /** - * Gets cache tags. - * - * @return string[] - */ - public function getCacheTags() { - return $this->tags; - } - - /** - * Adds cache tags. - * - * @param string[] $cache_tags - * The cache tags to be added. - * - * @return $this - */ - public function addCacheTags(array $cache_tags) { - $this->tags = Cache::mergeTags($this->tags, $cache_tags); - return $this; - } - - /** - * Sets cache tags. - * - * @param string[] $cache_tags - * The cache tags to be associated. - * - * @return $this - */ - public function setCacheTags(array $cache_tags) { - $this->tags = $cache_tags; - return $this; - } - - /** - * Gets cache contexts. - * - * @return string[] - */ - public function getCacheContexts() { - return $this->contexts; - } - - /** - * Adds cache contexts. - * - * @param string[] $cache_contexts - * The cache contexts to be added. - * - * @return $this - */ - public function addCacheContexts(array $cache_contexts) { - $this->contexts = Cache::mergeContexts($this->contexts, $cache_contexts); - return $this; - } - - /** - * Sets cache contexts. - * - * @param string[] $cache_contexts - * The cache contexts to be associated. - * - * @return $this - */ - public function setCacheContexts(array $cache_contexts) { - $this->contexts = $cache_contexts; - return $this; - } - - /** - * Gets the maximum age (in seconds). - * - * @return int - */ - public function getCacheMaxAge() { - return $this->maxAge; - } - - /** - * Sets the maximum age (in seconds). - * - * Defaults to Cache::PERMANENT - * - * @param int $max_age - * The max age to associate. - * - * @return $this - * - * @throws \InvalidArgumentException - */ - public function setCacheMaxAge($max_age) { - if (!is_int($max_age)) { - throw new \InvalidArgumentException('$max_age must be an integer'); - } - - $this->maxAge = $max_age; - return $this; - } - /** * Gets assets. * diff --git a/core/tests/Drupal/Tests/Core/Cache/CacheableMetadataTest.php b/core/tests/Drupal/Tests/Core/Cache/CacheableMetadataTest.php new file mode 100644 index 00000000000..7918d609399 --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Cache/CacheableMetadataTest.php @@ -0,0 +1,72 @@ +addCacheTags($add); + $this->assertEquals($expected, $metadata->getCacheTags()); + } + } + + /** + * Test valid and invalid values as max age. + * + * @covers ::setCacheMaxAge + * @dataProvider providerSetCacheMaxAge + */ + public function testSetCacheMaxAge($data, $expect_exception) { + $metadata = new CacheableMetadata(); + if ($expect_exception) { + $this->setExpectedException('\InvalidArgumentException'); + } + $metadata->setCacheMaxAge($data); + $this->assertEquals($data, $metadata->getCacheMaxAge()); + } + + /** + * Data provider for testSetCacheMaxAge. + */ + public function providerSetCacheMaxAge() { + return [ + [0 , FALSE], + ['http', TRUE], + ['0', TRUE], + [new \stdClass(), TRUE], + [300, FALSE], + [[], TRUE], + [8.0, TRUE] + ]; + } +}