diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CachePoolPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CachePoolPass.php index 4b8e0afb7bb3..a30b95d3f067 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CachePoolPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CachePoolPass.php @@ -44,13 +44,15 @@ public function process(ContainerBuilder $container) if ($pool->isAbstract()) { continue; } - $tags[0]['namespace'] = $this->getNamespace($namespaceSuffix, isset($tags[0]['namespace']) ? $tags[0]['namespace'] : $id); while ($adapter instanceof DefinitionDecorator) { $adapter = $container->findDefinition($adapter->getParent()); if ($t = $adapter->getTag('cache.pool')) { $tags[0] += $t[0]; } } + if (!isset($tags[0]['namespace'])) { + $tags[0]['namespace'] = $this->getNamespace($namespaceSuffix, $id); + } if (isset($tags[0]['clearer'])) { $clearer = $container->getDefinition($tags[0]['clearer']); } else { diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index 7d8bf5f0a03e..b2b1eca5aa7f 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php @@ -557,27 +557,37 @@ private function addCacheSection(ArrayNodeDefinition $rootNode) ->info('Cache configuration') ->addDefaultsIfNotSet() ->fixXmlConfig('pool') + ->fixXmlConfig('adapter') ->children() - ->arrayNode('pools') + ->arrayNode('adapters') ->useAttributeAsKey('name') ->prototype('array') ->children() - ->scalarNode('adapter') - ->info('The cache pool adapter service to use as template definition.') - ->defaultValue('cache.adapter.shared') + ->scalarNode('parent') + ->isRequired() + ->info('The parent cache adapter service.') ->end() - ->booleanNode('public')->defaultFalse()->end() ->integerNode('default_lifetime')->end() ->scalarNode('provider') - ->info('The service name to use as provider when the specified adapter needs one.') - ->end() - ->scalarNode('namespace') - ->info('The namespace where cached items are stored. Auto-generated by default. Set to false to disable namespacing.') + ->info('The service name to use as provider when the specified parent adapter needs one.') ->end() ->scalarNode('clearer')->defaultValue('cache.default_pools_clearer')->end() ->end() ->end() ->end() + ->arrayNode('pools') + ->useAttributeAsKey('name') + ->prototype('array') + ->children() + ->scalarNode('adapter') + ->info('The cache adapter service to use as template definition.') + ->defaultValue('cache.adapter.shared') + ->end() + ->booleanNode('public')->defaultTrue()->end() + ->integerNode('default_lifetime')->end() + ->end() + ->end() + ->end() ->end() ->end() ->end() diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 3f1845dd8f7d..1bda4d6797a3 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -1037,6 +1037,15 @@ private function registerPropertyInfoConfiguration(array $config, ContainerBuild private function registerCacheConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader) { + foreach ($config['adapters'] as $name => $adapterConfig) { + $adapterDefinition = new DefinitionDecorator($adapterConfig['parent']); + $adapterDefinition->setAbstract(true); + unset($adapterConfig['parent']); + + $adapterDefinition->addTag('cache.pool', $adapterConfig); + $container->setDefinition('cache.adapter.'.$name, $adapterDefinition); + } + foreach ($config['pools'] as $name => $poolConfig) { $poolDefinition = new DefinitionDecorator($poolConfig['adapter']); $poolDefinition->setPublic($poolConfig['public']); diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/cache_pools.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/cache_pools.xml index 6929b2299348..ffc7820af977 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/cache_pools.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/cache_pools.xml @@ -10,26 +10,23 @@ - - - - - - - - - + + - + - + + + + + @@ -39,6 +36,7 @@ + @@ -49,6 +47,7 @@ + @@ -59,12 +58,14 @@ + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd b/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd index aa2c057d356e..103418128b9e 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd @@ -206,16 +206,23 @@ + + + + + + + + + - - diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php index b41d568c64fb..17e45f79d480 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php @@ -267,6 +267,7 @@ protected static function getBundleDefaultConfig() 'packages' => array(), ), 'cache' => array( + 'adapters' => array(), 'pools' => array(), ), ); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/cache.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/cache.php index 7cf634b92e39..1cffc07b8bfa 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/cache.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/cache.php @@ -2,6 +2,17 @@ $container->loadFromExtension('framework', array( 'cache' => array( + 'adapters' => array( + 'foo' => array( + 'parent' => 'cache.adapter.filesystem', + 'default_lifetime' => 30, + ), + 'app_redis' => array( + 'parent' => 'cache.adapter.redis', + 'provider' => 'app.redis_connection', + 'default_lifetime' => 30, + ), + ), 'pools' => array( 'foo' => array( 'adapter' => 'cache.adapter.apcu', @@ -10,7 +21,6 @@ 'bar' => array( 'adapter' => 'cache.adapter.doctrine', 'default_lifetime' => 5, - 'provider' => 'app.doctrine_cache_provider', ), 'baz' => array( 'adapter' => 'cache.adapter.filesystem', @@ -19,7 +29,6 @@ 'foobar' => array( 'adapter' => 'cache.adapter.psr6', 'default_lifetime' => 10, - 'provider' => 'app.cache_pool', ), 'def' => array( 'default_lifetime' => 11, diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/cache.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/cache.xml index d6f472716ff8..937a05c05e96 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/cache.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/cache.xml @@ -7,10 +7,12 @@ + + - + - + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/cache.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/cache.yml index 395009f18ad1..656c68440b62 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/cache.yml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/cache.yml @@ -1,5 +1,13 @@ framework: cache: + adapters: + foo: + parent: cache.adapter.filesystem + default_lifetime: 30 + app_redis: + parent: cache.adapter.redis + provider: app.redis_connection + default_lifetime: 30 pools: foo: adapter: cache.adapter.apcu @@ -7,13 +15,11 @@ framework: bar: adapter: cache.adapter.doctrine default_lifetime: 5 - provider: app.doctrine_cache_provider baz: adapter: cache.adapter.filesystem default_lifetime: 7 foobar: adapter: cache.adapter.psr6 default_lifetime: 10 - provider: app.cache_pool def: default_lifetime: 11 diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php index 6abee616bbe4..79c0b02b9924 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php @@ -615,6 +615,14 @@ public function testPropertyInfoEnabled() $this->assertTrue($container->has('property_info')); } + public function testCacheAdapterServices() + { + $container = $this->createContainerFromFile('cache'); + + $this->assertCacheAdaptersServiceDefinitionIsCreated($container, 'foo', 'cache.adapter.foo', null, 30); + $this->assertCacheAdaptersServiceDefinitionIsCreated($container, 'app_redis', 'cache.adapter.redis', 'app.redis_connection', 30); + } + public function testCachePoolServices() { $container = $this->createContainerFromFile('cache'); @@ -697,6 +705,46 @@ private function assertVersionStrategy(ContainerBuilder $container, Reference $r } } + private function assertCacheAdaptersServiceDefinitionIsCreated(ContainerBuilder $container, $name, $parent, $provider, $defaultLifetime) + { + $id = 'cache.adapter.'.$name; + + $this->assertTrue($container->has($id), sprintf('Service definition "%s" for cache adapter "%s" is registered', $id, $name)); + + $adapterDefinition = $container->getDefinition($id); + + $this->assertTrue($adapterDefinition->hasTag('cache.pool'), sprintf('Service definition "%s" is tagged with the "cache.pool" tag.', $id)); + $this->assertTrue($adapterDefinition->isAbstract(), sprintf('Service definition "%s" is abstract.', $id)); + + $tag = $adapterDefinition->getTag('cache.pool'); + $this->assertTrue(isset($tag[0]['default_lifetime']), 'The default lifetime is stored as an attribute of the "cache.pool" tag.'); + $this->assertSame($defaultLifetime, $tag[0]['default_lifetime'], 'The default lifetime is stored as an attribute of the "cache.pool" tag.'); + + if ($provider) { + $this->assertTrue(isset($tag[0]['provider']), 'The provider is stored as an attribute of the "cache.pool" tag.'); + $this->assertSame($provider, $tag[0]['provider'], 'The provider is stored as an attribute of the "cache.pool" tag.'); + } else { + $this->assertFalse(isset($tag[0]['provider']), 'No provider is stored as an attribute of the "cache.pool" tag.'); + } + + $this->assertInstanceOf(DefinitionDecorator::class, $adapterDefinition, sprintf('Cache adapter "%s" is based on a parent.', $name)); + + $adapterId = $adapterDefinition->getParent(); + $adapterDefinition = $container->findDefinition($adapterId); + + switch ($parent) { + case 'cache.adapter.apcu': + $this->assertSame(ApcuAdapter::class, $adapterDefinition->getClass()); + break; + case 'cache.adapter.doctrine': + $this->assertSame(DoctrineAdapter::class, $adapterDefinition->getClass()); + break; + case 'cache.adapter.filesystem': + $this->assertSame(FilesystemAdapter::class, $adapterDefinition->getClass()); + break; + } + } + private function assertCachePoolServiceDefinitionIsCreated(ContainerBuilder $container, $name, $adapter, $defaultLifetime) { $id = 'cache.pool.'.$name; @@ -728,7 +776,5 @@ private function assertCachePoolServiceDefinitionIsCreated(ContainerBuilder $con $this->assertSame(FilesystemAdapter::class, $adapterDefinition->getClass()); break; } - - $this->assertTrue($adapterDefinition->isAbstract(), sprintf('Service definition "%s" is abstract.', $adapterId)); } }