Skip to content

Commit

Permalink
Use DoctrineCacheBundle to create cache drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
kinncj authored and FabioBatSilva committed Feb 28, 2014
1 parent df57980 commit e37301f
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 36 deletions.
9 changes: 8 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ private function getOrmCacheDriverNode($name)
->scalarNode('class')->end()
->scalarNode('id')->end()
->scalarNode('namespace')->defaultNull()->end()
->scalarNode('cache_provider')->defaultNull()->end()
->end()
;

Expand Down
61 changes: 57 additions & 4 deletions DependencyInjection/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,43 @@
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.
*
* @author Jonathan H. Wage <[email protected]>
* @author Fabien Potencier <[email protected]>
* @author Benjamin Eberlei <[email protected]>
* @author Fabio B. Silva <[email protected]>
* @author Kinn Coelho Julião <[email protected]>
*/
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}
*/
Expand All @@ -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);
}
Expand Down Expand Up @@ -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));
Expand All @@ -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);

Expand Down Expand Up @@ -506,7 +535,7 @@ protected function loadOrmEntityManagerMappingInformation(array $entityManager,

/**
* Loads an ORM second level cache bundle mapping information.
*
*
* @example
* entity_managers:
* default:
Expand Down Expand Up @@ -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.
*
Expand All @@ -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);
}

/**
Expand Down
1 change: 1 addition & 0 deletions DoctrineBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
40 changes: 20 additions & 20 deletions Tests/DependencyInjection/AbstractDoctrineExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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'
));
}
Expand All @@ -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%'));
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand Down
18 changes: 10 additions & 8 deletions Tests/DependencyInjection/DoctrineExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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()
Expand Down
15 changes: 12 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit e37301f

Please sign in to comment.