diff --git a/.travis.yml b/.travis.yml index 705ba9557..1bc56262d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,4 +5,11 @@ php: - 5.4 - 5.5 -before_script: composer install --dev --prefer-source +before_script: + - composer install --dev --prefer-source + +script: + - ./vendor/bin/phpunit -v --coverage-clover ./build/logs/clover.xml + +after_script: + - php ./vendor/bin/coveralls -v diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 0c4a21c68..928646254 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -595,6 +595,7 @@ private function getOrmCacheDriverNode($name) ->scalarNode('class')->end() ->scalarNode('id')->end() ->scalarNode('namespace')->defaultNull()->end() + ->scalarNode('cache_provider')->defaultNull()->end() ->end() ; diff --git a/DependencyInjection/DoctrineExtension.php b/DependencyInjection/DoctrineExtension.php index 8fd157502..0ab0ca7e1 100644 --- a/DependencyInjection/DoctrineExtension.php +++ b/DependencyInjection/DoctrineExtension.php @@ -23,6 +23,8 @@ use Symfony\Component\DependencyInjection\Reference; use Symfony\Bridge\Doctrine\DependencyInjection\AbstractDoctrineExtension; use Symfony\Component\Config\FileLocator; +use Doctrine\Bundle\DoctrineCacheBundle\DependencyInjection\SymfonyBridgeAdapter; +use Doctrine\Bundle\DoctrineCacheBundle\DependencyInjection\CacheProviderLoader; /** * DoctrineExtension is an extension for the Doctrine DBAL and ORM library. @@ -30,12 +32,34 @@ * @author Jonathan H. Wage * @author Fabien Potencier * @author Benjamin Eberlei + * @author Fabio B. Silva + * @author Kinn Coelho JuliĆ£o */ class DoctrineExtension extends AbstractDoctrineExtension { + /** + * @var string + */ private $defaultConnection; + + /** + * @var array + */ private $entityManagers; + /** + * @var \Doctrine\Bundle\DoctrineCacheBundle\DependencyInjection\CacheProviderLoader + */ + private $adapter; + + /** + * @param \Doctrine\Bundle\DoctrineCacheBundle\DependencyInjection\SymfonyBridgeAdapter $adapter + */ + public function __construct(SymfonyBridgeAdapter $adapter = null) + { + $this->adapter = $adapter ?: new SymfonyBridgeAdapter(new CacheProviderLoader(), 'doctrine.orm', 'orm'); + } + /** * {@inheritDoc} */ @@ -44,6 +68,8 @@ public function load(array $configs, ContainerBuilder $container) $configuration = $this->getConfiguration($configs, $container); $config = $this->processConfiguration($configuration, $configs); + $this->adapter->loadServicesConfiguration($container); + if (!empty($config['dbal'])) { $this->dbalLoad($config['dbal'], $container); } @@ -84,6 +110,7 @@ protected function dbalLoad(array $config, ContainerBuilder $container) $keys = array_keys($config['connections']); $config['default_connection'] = reset($keys); } + $this->defaultConnection = $config['default_connection']; $container->setAlias('database_connection', sprintf('doctrine.dbal.%s_connection', $this->defaultConnection)); @@ -92,9 +119,11 @@ protected function dbalLoad(array $config, ContainerBuilder $container) $container->setParameter('doctrine.dbal.connection_factory.types', $config['types']); $connections = array(); + foreach (array_keys($config['connections']) as $name) { $connections[$name] = sprintf('doctrine.dbal.%s_connection', $name); } + $container->setParameter('doctrine.connections', $connections); $container->setParameter('doctrine.default_connection', $this->defaultConnection); @@ -506,7 +535,7 @@ protected function loadOrmEntityManagerMappingInformation(array $entityManager, /** * Loads an ORM second level cache bundle mapping information. - * + * * @example * entity_managers: * default: @@ -652,6 +681,20 @@ protected function getMappingResourceExtension() return 'orm'; } + protected function loadCacheDriver($driverName, $entityManager, $driverMap, $container) + { + if (!empty($driverMap['cache_provider'])) { + $aliasId = $this->getObjectManagerElementName($driverName); + $serviceId = printf('doctrine_cache.providers.%s', $driverMap['cache_provider']); + + $container->setAlias($aliasId, new Alias($serviceId, false)); + + return; + } + + return $this->adapter->loadCacheDriver($driverName, $entityManager, $driverMap, $container); + } + /** * Loads a configured entity managers cache drivers. * @@ -660,9 +703,19 @@ protected function getMappingResourceExtension() */ protected function loadOrmCacheDrivers(array $entityManager, ContainerBuilder $container) { - $this->loadObjectManagerCacheDriver($entityManager, $container, 'metadata_cache'); - $this->loadObjectManagerCacheDriver($entityManager, $container, 'result_cache'); - $this->loadObjectManagerCacheDriver($entityManager, $container, 'query_cache'); + $this->loadCacheDriver('metadata_cache', $entityManager['name'], $entityManager['metadata_cache_driver'], $container); + $this->loadCacheDriver('result_cache', $entityManager['name'], $entityManager['result_cache_driver'], $container); + $this->loadCacheDriver('query_cache', $entityManager['name'], $entityManager['query_cache_driver'], $container); + } + + /** + * @param array $objectManager + * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container + * @param string $cacheName + */ + public function loadObjectManagerCacheDriver(array $objectManager, ContainerBuilder $container, $cacheName) + { + $this->loadCacheDriver($cacheName, $objectManager['name'], $objectManager[$cacheName.'_driver'], $container); } /** diff --git a/DoctrineBundle.php b/DoctrineBundle.php index c1d5aa593..b648765af 100644 --- a/DoctrineBundle.php +++ b/DoctrineBundle.php @@ -50,6 +50,7 @@ public function build(ContainerBuilder $container) if ($container->hasExtension('security')) { $container->getExtension('security')->addUserProviderFactory(new EntityFactory('entity', 'doctrine.orm.security.user.provider')); } + $container->addCompilerPass(new DoctrineValidationPass('orm')); $container->addCompilerPass(new EntityListenerPass()); } diff --git a/Tests/DependencyInjection/AbstractDoctrineExtensionTest.php b/Tests/DependencyInjection/AbstractDoctrineExtensionTest.php index ebbda2c7c..ced9fd9b0 100644 --- a/Tests/DependencyInjection/AbstractDoctrineExtensionTest.php +++ b/Tests/DependencyInjection/AbstractDoctrineExtensionTest.php @@ -259,14 +259,14 @@ public function testLoadMultipleConnections() $this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $arguments[1]); $this->assertEquals('doctrine.orm.em2_configuration', (string) $arguments[1]); - $definition = $container->getDefinition('doctrine.orm.em1_metadata_cache'); - $this->assertEquals('%doctrine.orm.cache.xcache.class%', $definition->getClass()); + $definition = $container->getDefinition($container->getAlias('doctrine.orm.em1_metadata_cache')); + $this->assertEquals('%doctrine_cache.xcache.class%', $definition->getClass()); - $definition = $container->getDefinition('doctrine.orm.em1_query_cache'); - $this->assertEquals('%doctrine.orm.cache.array.class%', $definition->getClass()); + $definition = $container->getDefinition($container->getAlias('doctrine.orm.em1_query_cache')); + $this->assertEquals('%doctrine_cache.array.class%', $definition->getClass()); - $definition = $container->getDefinition('doctrine.orm.em1_result_cache'); - $this->assertEquals('%doctrine.orm.cache.array.class%', $definition->getClass()); + $definition = $container->getDefinition($container->getAlias('doctrine.orm.em1_result_cache')); + $this->assertEquals('%doctrine_cache.array.class%', $definition->getClass()); } public function testLoadLogging() @@ -299,11 +299,11 @@ public function testEntityManagerMetadataCacheDriverConfiguration() $this->compileContainer($container); - $definition = $container->getDefinition('doctrine.orm.em1_metadata_cache'); - $this->assertDICDefinitionClass($definition, '%doctrine.orm.cache.xcache.class%'); + $definition = $container->getDefinition($container->getAlias('doctrine.orm.em1_metadata_cache')); + $this->assertDICDefinitionClass($definition, '%doctrine_cache.xcache.class%'); - $definition = $container->getDefinition('doctrine.orm.em2_metadata_cache'); - $this->assertDICDefinitionClass($definition, '%doctrine.orm.cache.apc.class%'); + $definition = $container->getDefinition($container->getAlias('doctrine.orm.em2_metadata_cache')); + $this->assertDICDefinitionClass($definition, '%doctrine_cache.apc.class%'); } public function testEntityManagerMemcacheMetadataCacheDriverConfiguration() @@ -316,15 +316,15 @@ public function testEntityManagerMemcacheMetadataCacheDriverConfiguration() $this->compileContainer($container); - $definition = $container->getDefinition('doctrine.orm.default_metadata_cache'); - $this->assertDICDefinitionClass($definition, 'Doctrine\Common\Cache\MemcacheCache'); + $definition = $container->getDefinition($container->getAlias('doctrine.orm.default_metadata_cache')); + $this->assertDICDefinitionClass($definition, '%doctrine_cache.memcache.class%'); $this->assertDICDefinitionMethodCallOnce($definition, 'setMemcache', - array(new Reference('doctrine.orm.default_memcache_instance')) + array(new Reference('doctrine_cache.services.doctrine.orm.default_metadata_cache.connection')) ); - $definition = $container->getDefinition('doctrine.orm.default_memcache_instance'); - $this->assertDICDefinitionClass($definition, 'Memcache'); - $this->assertDICDefinitionMethodCallOnce($definition, 'connect', array( + $definition = $container->getDefinition('doctrine_cache.services.doctrine.orm.default_metadata_cache.connection'); + $this->assertDICDefinitionClass($definition, '%doctrine_cache.memcache.connection.class%'); + $this->assertDICDefinitionMethodCallOnce($definition, 'addServer', array( 'localhost', '11211' )); } @@ -339,8 +339,8 @@ public function testDependencyInjectionImportsOverrideDefaults() $this->compileContainer($container); - $cacheDefinition = $container->getDefinition('doctrine.orm.default_metadata_cache'); - $this->assertEquals('%doctrine.orm.cache.apc.class%', $cacheDefinition->getClass()); + $cacheDefinition = $container->getDefinition($container->getAlias('doctrine.orm.default_metadata_cache')); + $this->assertEquals('%doctrine_cache.apc.class%', $cacheDefinition->getClass()); $configDefinition = $container->getDefinition('doctrine.orm.default_configuration'); $this->assertDICDefinitionMethodCallOnce($configDefinition, 'setAutoGenerateProxyClasses', array('%doctrine.orm.auto_generate_proxy_classes%')); @@ -524,7 +524,7 @@ public function testSecondLevelCache() $loggerChainDef = $container->getDefinition('doctrine.orm.default_second_level_cache.logger_chain'); $loggerStatisticsDef = $container->getDefinition('doctrine.orm.default_second_level_cache.logger_statistics'); $myQueryRegionDef = $container->getDefinition('doctrine.orm.default_second_level_cache.region.my_query_region_filelock'); - $cacheDriverDef = $container->getDefinition('doctrine.orm.default_second_level_cache.region_cache_driver'); + $cacheDriverDef = $container->getDefinition($container->getAlias('doctrine.orm.default_second_level_cache.region_cache_driver')); $configDef = $container->getDefinition('doctrine.orm.default_configuration'); $myEntityRegionArgs = $myEntityRegionDef->getArguments(); $myQueryRegionArgs = $myQueryRegionDef->getArguments(); @@ -535,7 +535,7 @@ public function testSecondLevelCache() $this->assertDICDefinitionClass($myEntityRegionDef, '%doctrine.orm.second_level_cache.default_region.class%'); $this->assertDICDefinitionClass($loggerChainDef, '%doctrine.orm.second_level_cache.logger_chain.class%'); $this->assertDICDefinitionClass($loggerStatisticsDef, '%doctrine.orm.second_level_cache.logger_statistics.class%'); - $this->assertDICDefinitionClass($cacheDriverDef, '%doctrine.orm.cache.array.class%'); + $this->assertDICDefinitionClass($cacheDriverDef, '%doctrine_cache.array.class%'); $this->assertDICDefinitionMethodCallOnce($configDef, 'setSecondLevelCacheConfiguration'); $this->assertDICDefinitionMethodCallCount($slcFactoryDef, 'setRegion', array(), 3); $this->assertDICDefinitionMethodCallCount($loggerChainDef, 'setLogger', array(), 3); diff --git a/Tests/DependencyInjection/DoctrineExtensionTest.php b/Tests/DependencyInjection/DoctrineExtensionTest.php index 9f183c96c..389475c66 100644 --- a/Tests/DependencyInjection/DoctrineExtensionTest.php +++ b/Tests/DependencyInjection/DoctrineExtensionTest.php @@ -27,9 +27,11 @@ class DoctrineExtensionTest extends \PHPUnit_Framework_TestCase public function testDbalOverrideDefaultConnection() { $container = $this->getContainer(); - $loader = new DoctrineExtension(); + $extension = new DoctrineExtension(); + + $container->registerExtension($extension); - $loader->load(array(array(), array('dbal' => array('default_connection' => 'foo')), array()), $container); + $extension->load(array(array(), array('dbal' => array('default_connection' => 'foo')), array()), $container); // doctrine.dbal.default_connection $this->assertEquals('%doctrine.default_connection%', $container->getDefinition('doctrine')->getArgument(3), '->load() overrides existing configuration options'); @@ -134,14 +136,14 @@ public function testDependencyInjectionConfigurationDefaults() $this->assertEquals('doctrine.orm.default_entity_listener_resolver', (string) $calls[11][1][0]); } - $definition = $container->getDefinition('doctrine.orm.default_metadata_cache'); - $this->assertEquals('%doctrine.orm.cache.array.class%', $definition->getClass()); + $definition = $container->getDefinition($container->getAlias('doctrine.orm.default_metadata_cache')); + $this->assertEquals('%doctrine_cache.array.class%', $definition->getClass()); - $definition = $container->getDefinition('doctrine.orm.default_query_cache'); - $this->assertEquals('%doctrine.orm.cache.array.class%', $definition->getClass()); + $definition = $container->getDefinition($container->getAlias('doctrine.orm.default_query_cache')); + $this->assertEquals('%doctrine_cache.array.class%', $definition->getClass()); - $definition = $container->getDefinition('doctrine.orm.default_result_cache'); - $this->assertEquals('%doctrine.orm.cache.array.class%', $definition->getClass()); + $definition = $container->getDefinition($container->getAlias('doctrine.orm.default_result_cache')); + $this->assertEquals('%doctrine_cache.array.class%', $definition->getClass()); } public function testSingleEntityManagerConfiguration() diff --git a/composer.json b/composer.json index b4573476b..b824efd36 100644 --- a/composer.json +++ b/composer.json @@ -17,20 +17,29 @@ { "name": "Symfony Community", "homepage": "http://symfony.com/contributors" + }, + { + "name": "Doctrine Project", + "homepage": "http://www.doctrine-project.org/" } ], "require": { "php": ">=5.3.2", "symfony/framework-bundle": "~2.2", + "doctrine/dbal": "~2.3", + "jdorn/sql-formatter": "~1.1", "symfony/doctrine-bridge": "~2.2", - "doctrine/dbal": ">=2.3,<2.6-dev", - "jdorn/sql-formatter": "~1.1" + "doctrine/doctrine-cache-bundle": "~1.0" }, "require-dev": { "doctrine/orm": "~2.3", "symfony/yaml": "~2.2", "symfony/validator": "~2.2", - "twig/twig" : "~1" + "twig/twig": "~1", + "satooshi/php-coveralls": "~0.6", + "phpunit/phpunit": "~3.7", + "phpunit/php-code-coverage": "~1.2", + "phpunit/phpunit-mock-objects": "~1.2" }, "suggest": { "symfony/web-profiler-bundle": "to use the data collector",