From 28e615a656548cbedcdd3760423d7948e6fe656e Mon Sep 17 00:00:00 2001 From: Robin Chalas Date: Tue, 30 May 2017 14:30:58 +0200 Subject: [PATCH] Fix decorating TagAware adapters in dev --- .../Compiler/CacheCollectorPass.php | 7 ++-- .../Cache/Adapter/TraceableAdapter.php | 4 +-- .../Adapter/TraceableTagAwareAdapter.php | 36 +++++++++++++++++++ .../Adapter/TraceableTagAwareAdapterTest.php | 34 ++++++++++++++++++ 4 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 src/Symfony/Component/Cache/Adapter/TraceableTagAwareAdapter.php create mode 100644 src/Symfony/Component/Cache/Tests/Adapter/TraceableTagAwareAdapterTest.php diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CacheCollectorPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CacheCollectorPass.php index 3d1908bf9b..a4b89144ef 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CacheCollectorPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CacheCollectorPass.php @@ -11,7 +11,9 @@ namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler; +use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface; use Symfony\Component\Cache\Adapter\TraceableAdapter; +use Symfony\Component\Cache\Adapter\TraceableTagAwareAdapter; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; @@ -34,11 +36,12 @@ class CacheCollectorPass implements CompilerPassInterface $collectorDefinition = $container->getDefinition('data_collector.cache'); foreach ($container->findTaggedServiceIds('cache.pool') as $id => $attributes) { - if ($container->getDefinition($id)->isAbstract()) { + $definition = $container->getDefinition($id); + if ($definition->isAbstract()) { continue; } - $container->register($id.'.recorder', TraceableAdapter::class) + $container->register($id.'.recorder', is_subclass_of($definition->getClass(), TagAwareAdapterInterface::class) ? TraceableTagAwareAdapter::class : TraceableAdapter::class) ->setDecoratedService($id) ->addArgument(new Reference($id.'.recorder.inner')) ->setPublic(false); diff --git a/src/Symfony/Component/Cache/Adapter/TraceableAdapter.php b/src/Symfony/Component/Cache/Adapter/TraceableAdapter.php index d813c61ad9..1ddf65b301 100644 --- a/src/Symfony/Component/Cache/Adapter/TraceableAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/TraceableAdapter.php @@ -22,7 +22,7 @@ use Psr\Cache\CacheItemInterface; */ class TraceableAdapter implements AdapterInterface { - private $pool; + protected $pool; private $calls = array(); public function __construct(AdapterInterface $pool) @@ -177,7 +177,7 @@ class TraceableAdapter implements AdapterInterface } } - private function start($name) + protected function start($name) { $this->calls[] = $event = new TraceableAdapterEvent(); $event->name = $name; diff --git a/src/Symfony/Component/Cache/Adapter/TraceableTagAwareAdapter.php b/src/Symfony/Component/Cache/Adapter/TraceableTagAwareAdapter.php new file mode 100644 index 0000000000..de68955d8e --- /dev/null +++ b/src/Symfony/Component/Cache/Adapter/TraceableTagAwareAdapter.php @@ -0,0 +1,36 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Cache\Adapter; + +/** + * @author Robin Chalas + */ +class TraceableTagAwareAdapter extends TraceableAdapter implements TagAwareAdapterInterface +{ + public function __construct(TagAwareAdapterInterface $pool) + { + parent::__construct($pool); + } + + /** + * {@inheritdoc} + */ + public function invalidateTags(array $tags) + { + $event = $this->start(__FUNCTION__); + try { + return $event->result = $this->pool->invalidateTags($tags); + } finally { + $event->end = microtime(true); + } + } +} diff --git a/src/Symfony/Component/Cache/Tests/Adapter/TraceableTagAwareAdapterTest.php b/src/Symfony/Component/Cache/Tests/Adapter/TraceableTagAwareAdapterTest.php new file mode 100644 index 0000000000..38f3c6c087 --- /dev/null +++ b/src/Symfony/Component/Cache/Tests/Adapter/TraceableTagAwareAdapterTest.php @@ -0,0 +1,34 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Cache\Tests\Adapter; + +use Symfony\Component\Cache\Adapter\FilesystemAdapter; +use Symfony\Component\Cache\Adapter\TagAwareAdapter; +use Symfony\Component\Cache\Adapter\TraceableTagAwareAdapter; + +class TraceableTagAwareAdapterTest extends TraceableAdapterTest +{ + public function testInvalidateTags() + { + $pool = new TraceableTagAwareAdapter(new TagAwareAdapter(new FilesystemAdapter())); + $pool->invalidateTags(array('foo')); + $calls = $pool->getCalls(); + $this->assertCount(1, $calls); + + $call = $calls[0]; + $this->assertSame('invalidateTags', $call->name); + $this->assertSame(0, $call->hits); + $this->assertSame(0, $call->misses); + $this->assertNotEmpty($call->start); + $this->assertNotEmpty($call->end); + } +}