Commit a0ee087d authored by alexpott's avatar alexpott

Issue #2471743 by pwolanin: Create a more generic superclass of ...

Issue #2471743 by pwolanin: Create a more generic superclass of  \Drupal\Core\Render\BubbleableMetadata
parent f364fc76
<?php
/**
* @file
* Contains \Drupal\Core\CacheableMetadata
*/
namespace Drupal\Core\Cache;
/**
* Defines a generic class for passing cacheability metadata.
*
* @ingroup cache
*/
class CacheableMetadata implements CacheableDependencyInterface {
/**
* Cache contexts.
*
* @var string[]
*/
protected $contexts = [];
/**
* Cache tags.
*
* @var string[]
*/
protected $tags = [];
/**
* Cache max-age.
*
* @var int
*/
protected $maxAge = Cache::PERMANENT;
/**
* {@inheritdoc}
*/
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;
}
/**
* {@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;
}
}
......@@ -10,13 +10,14 @@
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 @@ public static function createFromObject($object) {
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.
*
......
<?php
/**
* @file
* Contains \Drupal\Tests\Core\Cache\CacheableMetadataTest.
*/
namespace Drupal\Tests\Core\Cache;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Render\Element;
/**
* @coversDefaultClass \Drupal\Core\Cache\CacheableMetadata
* @group Cache
*/
class CacheableMetadataTest extends UnitTestCase {
/**
* This delegates to Cache::mergeTags(), so just a basic test.
*
* @covers ::addCacheTags
*/
public function testAddCacheTags() {
$metadata = new CacheableMetadata();
$add_expected = [
[ [], [] ],
[ ['foo:bar'], ['foo:bar'] ],
[ ['foo:baz'], ['foo:bar', 'foo:baz'] ],
[ ['axx:first', 'foo:baz'], ['axx:first', 'foo:bar', 'foo:baz'] ],
[ [], ['axx:first', 'foo:bar', 'foo:baz'] ],
[ ['axx:first'], ['axx:first', 'foo:bar', 'foo:baz'] ],
];
foreach ($add_expected as $data) {
list($add, $expected) = $data;
$metadata->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]
];
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment