From 41526342ea1bb1f8c071411c6b7c6255e7981c09 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 6 Apr 2016 19:53:33 +0200 Subject: [PATCH] [FrameworkBundle] Add default pool & system adapter --- .../Compiler/CachePoolPass.php | 2 +- .../DependencyInjection/Configuration.php | 3 ++- .../DependencyInjection/FrameworkExtension.php | 17 ++++++++++------- .../Resources/config/cache_pools.xml | 11 ++++++++++- .../Resources/config/schema/symfony-1.0.xsd | 1 - .../DependencyInjection/ConfigurationTest.php | 3 +++ .../Functional/app/CachePools/redis_config.yml | 2 +- .../Bundle/FrameworkBundle/composer.json | 2 +- .../Cache/Tests/Adapter/RedisAdapterTest.php | 1 - .../Compiler/ResolveDefinitionTemplatesPass.php | 6 ++---- .../ResolveDefinitionTemplatesPassTest.php | 14 ++++++++++++++ 11 files changed, 44 insertions(+), 18 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CachePoolPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CachePoolPass.php index 8e1e90cfac..9598f537a0 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CachePoolPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CachePoolPass.php @@ -63,7 +63,7 @@ class CachePoolPass implements CompilerPassInterface unset($tags[0][$attr]); } if (!empty($tags[0])) { - throw new \InvalidArgumentException(sprintf('Invalid "cache.pool" tag for service "%s": accepted attributes are "provider", "namespace" and "default_lifetime", found "%s".', $id, implode('", "', array_keys($tags[0])))); + throw new \InvalidArgumentException(sprintf('Invalid "cache.pool" tag for service "%s": accepted attributes are "clearer", "provider", "namespace" and "default_lifetime", found "%s".', $id, implode('", "', array_keys($tags[0])))); } if (null !== $clearer) { diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index e125e3096a..e298b2609c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php @@ -555,6 +555,7 @@ class Configuration implements ConfigurationInterface ->children() ->arrayNode('cache') ->info('Cache configuration') + ->addDefaultsIfNotSet() ->fixXmlConfig('pool') ->children() ->arrayNode('pools') @@ -563,7 +564,7 @@ class Configuration implements ConfigurationInterface ->children() ->scalarNode('adapter') ->info('The cache pool adapter service to use as template definition.') - ->defaultValue('cache.adapter.default') + ->defaultValue('cache.adapter.shared') ->end() ->booleanNode('public')->defaultFalse()->end() ->integerNode('default_lifetime')->end() diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index ae9d79c414..014f447ba1 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -122,6 +122,7 @@ class FrameworkExtension extends Extension $this->registerFragmentsConfiguration($config['fragments'], $container, $loader); $this->registerTranslatorConfiguration($config['translator'], $container); $this->registerProfilerConfiguration($config['profiler'], $container, $loader); + $this->registerCacheConfiguration($config['cache'], $container, $loader); if ($this->isConfigEnabled($container, $config['router'])) { $this->registerRouterConfiguration($config['router'], $container, $loader); @@ -138,10 +139,6 @@ class FrameworkExtension extends Extension $this->registerPropertyInfoConfiguration($config['property_info'], $container, $loader); } - if (isset($config['cache'])) { - $this->registerCacheConfiguration($config['cache'], $container, $loader); - } - $loader->load('debug_prod.xml'); $definition = $container->findDefinition('debug.debug_handlers_listener'); @@ -1022,9 +1019,7 @@ class FrameworkExtension extends Extension private function registerCacheConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader) { - if (!empty($config['pools'])) { - $loader->load('cache_pools.xml'); - } + $loader->load('cache_pools.xml'); foreach ($config['pools'] as $name => $poolConfig) { $poolDefinition = new DefinitionDecorator($poolConfig['adapter']); @@ -1034,6 +1029,14 @@ class FrameworkExtension extends Extension $poolDefinition->addTag('cache.pool', $poolConfig); $container->setDefinition('cache.pool.'.$name, $poolDefinition); } + + $this->addClassesToCompile(array( + 'Psr\Cache\CacheItemInterface', + 'Psr\Cache\CacheItemPoolInterface', + 'Symfony\Component\Cache\Adapter\AdapterInterface', + 'Symfony\Component\Cache\Adapter\AbstractAdapter', + 'Symfony\Component\Cache\CacheItem', + )); } /** diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/cache_pools.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/cache_pools.xml index eef88060cf..2874f41bf9 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/cache_pools.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/cache_pools.xml @@ -10,7 +10,16 @@ - + + + + + + + + + + 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 d8fc67ae29..aa2c057d35 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 @@ -216,7 +216,6 @@ - diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php index 88be061603..9bcadc6810 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php @@ -265,6 +265,9 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase 'base_urls' => array(), 'packages' => array(), ), + 'cache' => array( + 'pools' => array(), + ), ); } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/CachePools/redis_config.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/CachePools/redis_config.yml index 1bafa08c7c..fb2510b6fa 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/CachePools/redis_config.yml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/CachePools/redis_config.yml @@ -8,7 +8,7 @@ services: calls: - [connect, [127.0.0.1]] - cache.adapter.default: + cache.adapter.shared: abstract: true parent: cache.adapter.redis tags: diff --git a/src/Symfony/Bundle/FrameworkBundle/composer.json b/src/Symfony/Bundle/FrameworkBundle/composer.json index ba60449cfc..8016bc3ea4 100644 --- a/src/Symfony/Bundle/FrameworkBundle/composer.json +++ b/src/Symfony/Bundle/FrameworkBundle/composer.json @@ -18,6 +18,7 @@ "require": { "php": ">=5.5.9", "symfony/asset": "~2.8|~3.0", + "symfony/cache": "~3.1", "symfony/class-loader": "~2.8|~3.0", "symfony/dependency-injection": "~3.1", "symfony/config": "~2.8|~3.0", @@ -38,7 +39,6 @@ }, "require-dev": { "symfony/browser-kit": "~2.8|~3.0", - "symfony/cache": "~3.1", "symfony/console": "~2.8|~3.0", "symfony/css-selector": "~2.8|~3.0", "symfony/dom-crawler": "~2.8|~3.0", diff --git a/src/Symfony/Component/Cache/Tests/Adapter/RedisAdapterTest.php b/src/Symfony/Component/Cache/Tests/Adapter/RedisAdapterTest.php index a41514f37d..fe3eaead39 100644 --- a/src/Symfony/Component/Cache/Tests/Adapter/RedisAdapterTest.php +++ b/src/Symfony/Component/Cache/Tests/Adapter/RedisAdapterTest.php @@ -37,7 +37,6 @@ class RedisAdapterTest extends CachePoolTest $e = error_get_last(); self::markTestSkipped($e['message']); } - self::$redis->select(1993); } public static function tearDownAfterClass() diff --git a/src/Symfony/Component/DependencyInjection/Compiler/ResolveDefinitionTemplatesPass.php b/src/Symfony/Component/DependencyInjection/Compiler/ResolveDefinitionTemplatesPass.php index 82e2925572..4451e7c435 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/ResolveDefinitionTemplatesPass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/ResolveDefinitionTemplatesPass.php @@ -15,7 +15,6 @@ use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\DefinitionDecorator; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Exception\RuntimeException; -use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; /** * This replaces all DefinitionDecorator instances with their equivalent fully @@ -97,12 +96,11 @@ class ResolveDefinitionTemplatesPass implements CompilerPassInterface */ private function resolveDefinition(ContainerBuilder $container, DefinitionDecorator $definition) { - try { - $parentDef = $container->findDefinition($parent = $definition->getParent()); - } catch (ServiceNotFoundException $e) { + if (!$container->has($parent = $definition->getParent())) { throw new RuntimeException(sprintf('The parent definition "%s" defined for definition "%s" does not exist.', $parent, $this->currentId)); } + $parentDef = $container->findDefinition($parent); if ($parentDef instanceof DefinitionDecorator) { $id = $this->currentId; $this->currentId = $parent; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveDefinitionTemplatesPassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveDefinitionTemplatesPassTest.php index adb7ba3bdc..31663713d3 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveDefinitionTemplatesPassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveDefinitionTemplatesPassTest.php @@ -309,6 +309,20 @@ class ResolveDefinitionTemplatesPassTest extends \PHPUnit_Framework_TestCase $this->assertEquals(array('Foo', 'Bar'), $def->getAutowiringTypes()); } + public function testProcessResolvesAliases() + { + $container = new ContainerBuilder(); + + $container->register('parent', 'ParentClass'); + $container->setAlias('parent_alias', 'parent'); + $container->setDefinition('child', new DefinitionDecorator('parent_alias')); + + $this->process($container); + + $def = $container->getDefinition('child'); + $this->assertSame('ParentClass', $def->getClass()); + } + protected function process(ContainerBuilder $container) { $pass = new ResolveDefinitionTemplatesPass();