8000 [FrameworkBundle] Allow configuring taggable cache pools by nicolas-grekas · Pull Request #26934 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] Allow configuring taggable cache pools #26934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions 5 src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.2.0
-----

* Allowed configuring taggable cache pools via a new `framework.cache.pools.tags` option (bool|service-id)

4.1.0
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public function process(ContainerBuilder $container)
foreach ($container->findTaggedServiceIds('cache.pool.clearer') as $id => $attr) {
$clearer = $container->getDefinition($id);
$pools = array();
foreach ($clearer->getArgument(0) as $id => $ref) {
if ($container->hasDefinition($id)) {
$pools[$id] = new Reference($id);
foreach ($clearer->getArgument(0) as $name => $ref) {
if ($container->hasDefinition($ref)) {
$pools[$name] = new Reference($ref);
}
}
$clearer->replaceArgument(0, $pools);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function process(ContainerBuilder $container)
$clearers = array();
$attributes = array(
'provider',
'name',
'namespace',
'default_lifetime',
'reset',
Expand All @@ -56,8 +57,9 @@ public function process(ContainerBuilder $container)
$tags[0] += $t[0];
}
}
$name = $tags[0]['name'] ?? $id;
if (!isset($tags[0]['namespace'])) {
$tags[0]['namespace'] = $this->getNamespace($seed, $id);
$tags[0]['namespace'] = $this->getNamespace($seed, $name);
}
if (isset($tags[0]['clearer'])) {
$clearer = $tags[0]['clearer'];
Expand All @@ -67,7 +69,7 @@ public function process(ContainerBuilder $container)
} else {
$clearer = null;
}
unset($tags[0]['clearer']);
unset($tags[0]['clearer'], $tags[0]['name']);

if (isset($tags[0]['provider'])) {
$tags[0]['provider'] = new Reference(static::getServiceProvider($container, $tags[0]['provider']));
Expand All @@ -86,14 +88,14 @@ public function process(ContainerBuilder $container)
unset($tags[0][$attr]);
}
if (!empty($tags[0])) {
throw new InvalidArgumentException(sprintf('Invalid "cache.pool" tag for service "%s": accepted attributes are "clearer", "provider", "namespace", "default_lifetime" and "reset", found "%s".', $id, implode('", "', array_keys($tags[0]))));
throw new InvalidArgumentException(sprintf('Invalid "cache.pool" tag for service "%s": accepted attributes are "clearer", "provider", "name", "namespace", "default_lifetime" and "reset", found "%s".', $id, implode('", "', array_keys($tags[0]))));
}

if (null !== $clearer) {
$clearers[$clearer][$id] = new Reference($id, $container::IGNORE_ON_UNINITIALIZED_REFERENCE);
$clearers[$clearer][$name] = new Reference($id, $container::IGNORE_ON_UNINITIALIZED_REFERENCE);
}

$pools[$id] = new Reference($id, $container::IGNORE_ON_UNINITIALIZED_REFERENCE);
$pools[$name] = new Reference($id, $container::IGNORE_ON_UNINITIALIZED_REFERENCE);
}

$clearer = 'cache.global_clearer';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,7 @@ private function addCacheSection(ArrayNodeDefinition $rootNode)
->prototype('array')
->children()
->scalarNode('adapter')->defaultValue('cache.app')->end()
->scalarNode('tags')->defaultNull()->end()
->booleanNode('public')->defaultFalse()->end()
->integerNode('default_lifetime')->end()
->scalarNode('provider')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\Cache\Adapter\AbstractAdapter;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\LoaderInterface;
Expand Down Expand Up @@ -1556,12 +1557,31 @@ private function registerCacheConfiguration(array $config, ContainerBuilder $con
$config['pools']['cache.'.$name] = array(
'adapter' => $config[$name],
'public' => true,
'tags' => false,
);
}
foreach ($config['pools'] as $name => $pool) {
if ($config['pools'][$pool['adapter']]['tags'] ?? false) {
$pool['adapter'] = '.'.$pool['adapter'].'.inner';
}
$definition = new ChildDefinition($pool['adapter']);

if ($pool['tags']) {
if ($config['pools'][$pool['tags']]['tags'] ?? false) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really want to fail silently if you misconfigure a cache pool service for the tags by having a typo there?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can do otherwise: you should be allowed to reference a service you created manually.
For the "typo" case, this will create a Reference to a missing service, so you'll still get an error in the end.

$pool['tags'] = '.'.$pool['tags'].'.inner';
}
$container->register($name, TagAwareAdapter::class)
->addArgument(new Reference('.'.$name.'.inner'))
->addArgument(true !== $pool['tags'] ? new Reference($pool['tags']) : null)
->setPublic($pool['public'])
;

$pool['name'] = $name;
$pool['public'] = false;
$name = '.'.$name.'.inner';
}
$definition->setPublic($pool['public']);
unset($pool['adapter'], $pool['public']);
unset($pool['adapter'], $pool['public'], $pool['tags']);

$definition->addTag('cache.pool', $pool);
$container->setDefinition($name, $definition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public function testPoolRefsAreWeak()
$publicPool->addTag('cache.pool', array('clearer' => 'clearer_alias'));
$container->setDefinition('public.pool', $publicPool);

$publicPool = new Definition();
$publicPool->addArgument('namespace');
$publicPool->addTag('cache.pool', array('clearer' => 'clearer_alias', 'name' => 'pool2'));
$container->setDefinition('public.pool2', $publicPool);

$privatePool = new Definition();
$privatePool->setPublic(false);
$privatePool->addArgument('namespace');
Expand All @@ -55,7 +60,11 @@ public function testPoolRefsAreWeak()
$pass->process($container);
}

$this->assertEquals(array(array('public.pool' => new Reference('public.pool'))), $clearer->getArguments());
$this->assertEquals(array(array('public.pool' => new Reference('public.pool'))), $globalClearer->getArguments());
$expected = array(array(
'public.pool' => new Reference('public.pool'),
'pool2' => new Reference('public.pool2'),
));
$this->assertEquals($expected, $clearer->getArguments());
$this->assertEquals($expected, $globalClearer->getArguments());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,28 @@ public function testArgsAreReplaced()
$this->assertSame(3, $cachePool->getArgument(2));
}

public function testWithNameAttribute()
{
$container = new ContainerBuilder();
$container->setParameter('kernel.debug', false);
$container->setParameter('kernel.name', 'app');
$container->setParameter('kernel.environment', 'prod');
$container->setParameter('cache.prefix.seed', 'foo');
$cachePool = new Definition();
$cachePool->addTag('cache.pool', array(
'name' => 'foobar',
'provider' => 'foobar',
));
$cachePool->addArgument(null);
$cachePool->addArgument(null);
$cachePool->addArgument(null);
$container->setDefinition('app.cache_pool', $cachePool);

$this->cachePoolPass->process($container);

$this->assertSame('9HvPgAayyh', $cachePool->getArgument(1));
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid "cache.pool" tag for service "app.cache_pool": accepted attributes are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\Adapter\RedisAdapter;
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
use Symfony\Component\Cache\Exception\InvalidArgumentException;

class CachePoolsTest extends WebTestCase
Expand Down Expand Up @@ -94,6 +95,25 @@ private function doTestCachePools($options, $adapterClass)

$item = $pool2->getItem($key);
$this->assertTrue($item->isHit());

$prefix = "\0".TagAwareAdapter::class."\0";
$pool4 = $container->get('cache.pool4');
$this->assertInstanceof(TagAwareAdapter::class, $pool4);
$pool4 = (array) $pool4;
$this->assertSame($pool4[$prefix.'pool'], $pool4[$prefix.'tags'] ?? $pool4['tags']);

$pool5 = $container->get('cache.pool5');
$this->assertInstanceof(TagAwareAdapter::class, $pool5);
$pool5 = (array) $pool5;
$this->assertSame($pool2, $pool5[$prefix.'tags'] ?? $pool5['tags']);

$pool6 = $container->get('cache.pool6');
$this->assertInstanceof(TagAwareAdapter::class, $pool6);
$pool6 = (array) $pool6;
$this->assertSame($pool4[$prefix.'pool'], $pool6[$prefix.'tags'] ?? $pool6['tags']);

$pool7 = $container->get('cache.pool7');
$this->assertNotInstanceof(TagAwareAdapter::class, $pool7);
}

protected static function createKernel(array $options = array())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,15 @@ framework:
adapter: cache.pool3
cache.pool3:
clearer: ~
cache.pool4:
tags: true
public: true
cache.pool5:
tags: cache.pool2
public: true
cache.pool6:
tags: cache.pool4
public: true
cache.pool7:
adapter: cache.pool4
public: true
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,17 @@ framework:
cache.pool2:
public: true
clearer: ~
cache.pool3:
clearer: ~
cache.pool4:
tags: true
public: true
cache.pool5:
tags: cache.pool2
public: true
cache.pool6:
tags: cache.pool4
public: true
cache.pool7:
adapter: cache.pool4
public: true
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,17 @@ framework:
cache.pool2:
public: true
clearer: ~
cache.pool3:
clearer: ~
cache.pool4:
tags: true
public: true
cache.pool5:
tags: cache.pool2
public: true
cache.pool6:
tags: cache.pool4
public: true
cache.pool7:
adapter: cache.pool4
public: true
0