Issue #2464877 by Wim Leers: Update RendererInterface::addDependency() to accept *any* object, not only CachableDependencyInterface objects
parent
6435e2ee5d
commit
a4e51c82e6
|
@ -109,18 +109,26 @@ class BubbleableMetadata implements CacheableDependencyInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a bubbleable metadata object from a cacheable depended object.
|
* Creates a bubbleable metadata object from a depended object.
|
||||||
*
|
*
|
||||||
* @param \Drupal\Core\Cache\CacheableDependencyInterface $object
|
* @param \Drupal\Core\Cache\CacheableDependencyInterface|mixed $object
|
||||||
* The object whose cacheability metadata to retrieve.
|
* The object whose cacheability metadata to retrieve.
|
||||||
*
|
*
|
||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public static function createFromObject(CacheableDependencyInterface $object) {
|
public static function createFromObject($object) {
|
||||||
|
if ($object instanceof CacheableDependencyInterface) {
|
||||||
|
$meta = new static();
|
||||||
|
$meta->contexts = $object->getCacheContexts();
|
||||||
|
$meta->tags = $object->getCacheTags();
|
||||||
|
$meta->maxAge = $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 = new static();
|
||||||
$meta->contexts = $object->getCacheContexts();
|
$meta->maxAge = 0;
|
||||||
$meta->tags = $object->getCacheTags();
|
|
||||||
$meta->maxAge = $object->getCacheMaxAge();
|
|
||||||
return $meta;
|
return $meta;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -792,7 +792,7 @@ class Renderer implements RendererInterface {
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function addDependency(array &$elements, CacheableDependencyInterface $dependency) {
|
public function addDependency(array &$elements, $dependency) {
|
||||||
$meta_a = BubbleableMetadata::createFromRenderArray($elements);
|
$meta_a = BubbleableMetadata::createFromRenderArray($elements);
|
||||||
$meta_b = BubbleableMetadata::createFromObject($dependency);
|
$meta_b = BubbleableMetadata::createFromObject($dependency);
|
||||||
$meta_a->merge($meta_b)->applyTo($elements);
|
$meta_a->merge($meta_b)->applyTo($elements);
|
||||||
|
|
|
@ -351,10 +351,10 @@ interface RendererInterface {
|
||||||
*
|
*
|
||||||
* @param array &$elements
|
* @param array &$elements
|
||||||
* The render array to update.
|
* The render array to update.
|
||||||
* @param \Drupal\Core\Cache\CacheableDependencyInterface $dependency
|
* @param \Drupal\Core\Cache\CacheableDependencyInterface|mixed $dependency
|
||||||
* The dependency.
|
* The dependency.
|
||||||
*/
|
*/
|
||||||
public function addDependency(array &$elements, CacheableDependencyInterface $dependency);
|
public function addDependency(array &$elements, $dependency);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Merges two attachments arrays (which live under the '#attached' key).
|
* Merges two attachments arrays (which live under the '#attached' key).
|
||||||
|
|
|
@ -136,7 +136,7 @@ class BubbleableMetadataTest extends UnitTestCase {
|
||||||
* @covers ::createFromObject
|
* @covers ::createFromObject
|
||||||
* @dataProvider providerTestCreateFromObject
|
* @dataProvider providerTestCreateFromObject
|
||||||
*/
|
*/
|
||||||
public function testCreateFromObject(CacheableDependencyInterface $object, BubbleableMetadata $expected) {
|
public function testCreateFromObject($object, BubbleableMetadata $expected) {
|
||||||
$this->assertEquals($expected, BubbleableMetadata::createFromObject($object));
|
$this->assertEquals($expected, BubbleableMetadata::createFromObject($object));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -153,12 +153,16 @@ class BubbleableMetadataTest extends UnitTestCase {
|
||||||
$nonempty_metadata->setCacheContexts(['qux'])
|
$nonempty_metadata->setCacheContexts(['qux'])
|
||||||
->setCacheTags(['foo:bar'])
|
->setCacheTags(['foo:bar'])
|
||||||
->setCacheMaxAge(600);
|
->setCacheMaxAge(600);
|
||||||
|
$uncacheable_metadata = new BubbleableMetadata();
|
||||||
|
$uncacheable_metadata->setCacheMaxAge(0);
|
||||||
|
|
||||||
$empty_cacheable_object = new TestCacheableDependency([], [], Cache::PERMANENT);
|
$empty_cacheable_object = new TestCacheableDependency([], [], Cache::PERMANENT);
|
||||||
$nonempty_cacheable_object = new TestCacheableDependency(['qux'], ['foo:bar'], 600);
|
$nonempty_cacheable_object = new TestCacheableDependency(['qux'], ['foo:bar'], 600);
|
||||||
|
$uncacheable_object = new \stdClass();
|
||||||
|
|
||||||
$data[] = [$empty_cacheable_object, $empty_metadata];
|
$data[] = [$empty_cacheable_object, $empty_metadata];
|
||||||
$data[] = [$nonempty_cacheable_object, $nonempty_metadata];
|
$data[] = [$nonempty_cacheable_object, $nonempty_metadata];
|
||||||
|
$data[] = [$uncacheable_object, $uncacheable_metadata];
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
|
@ -626,7 +626,7 @@ class RendererTest extends RendererTestBase {
|
||||||
*
|
*
|
||||||
* @dataProvider providerTestAddDependency
|
* @dataProvider providerTestAddDependency
|
||||||
*/
|
*/
|
||||||
public function testAddDependency(array $build, CacheableDependencyInterface $object, array $expected) {
|
public function testAddDependency(array $build, $object, array $expected) {
|
||||||
$this->renderer->addDependency($build, $object);
|
$this->renderer->addDependency($build, $object);
|
||||||
$this->assertEquals($build, $expected);
|
$this->assertEquals($build, $expected);
|
||||||
}
|
}
|
||||||
|
@ -681,6 +681,26 @@ class RendererTest extends RendererTestBase {
|
||||||
'#post_render_cache' => [],
|
'#post_render_cache' => [],
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
|
// Cacheable render array, no cacheability.
|
||||||
|
[
|
||||||
|
[
|
||||||
|
'#cache' => [
|
||||||
|
'contexts' => ['theme'],
|
||||||
|
'tags' => ['bar'],
|
||||||
|
'max-age' => 600,
|
||||||
|
]
|
||||||
|
],
|
||||||
|
new \stdClass(),
|
||||||
|
[
|
||||||
|
'#cache' => [
|
||||||
|
'contexts' => ['theme'],
|
||||||
|
'tags' => ['bar'],
|
||||||
|
'max-age' => 0,
|
||||||
|
],
|
||||||
|
'#attached' => [],
|
||||||
|
'#post_render_cache' => [],
|
||||||
|
],
|
||||||
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue