Skip to content

Commit

Permalink
fixed cache config and using the abstract method
Browse files Browse the repository at this point in the history
  • Loading branch information
bamarni committed May 10, 2012
1 parent 57cc020 commit bb1a415
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 57 deletions.
3 changes: 2 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,13 @@ private function addDocumentManagersSection(ArrayNodeDefinition $rootNode)
->end()
->scalarNode('auto_mapping')->defaultFalse()->end()
->arrayNode('metadata_cache_driver')
->addDefaultsIfNotSet()
->beforeNormalization()
->ifTrue(function($v) { return !is_array($v); })
->then(function($v) { return array('type' => $v); })
->end()
->children()
->scalarNode('type')->end()
->scalarNode('type')->defaultValue('array')->isRequired()->end()
->scalarNode('class')->end()
->scalarNode('host')->end()
->scalarNode('port')->end()
Expand Down
53 changes: 3 additions & 50 deletions DependencyInjection/DoctrineMongoDBExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ public function load(array $configs, ContainerBuilder $container)
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

// can't currently default this correctly in Configuration
if (!isset($config['metadata_cache_driver'])) {
$config['metadata_cache_driver'] = array('type' => 'array');
}

if (empty($config['default_connection'])) {
$keys = array_keys($config['connections']);
$config['default_connection'] = reset($keys);
Expand All @@ -72,7 +67,6 @@ public function load(array $configs, ContainerBuilder $container)
$config['document_managers'],
$config['default_document_manager'],
$config['default_database'],
$config['metadata_cache_driver'],
$container
);
}
Expand Down Expand Up @@ -112,10 +106,9 @@ protected function overrideParameters($options, ContainerBuilder $container)
* @param array $dmConfigs An array of document manager configs
* @param string $defaultDM The default document manager name
* @param string $defaultDB The default db name
* @param string $defaultMetadataCache The default metadata cache configuration
* @param ContainerBuilder $container A ContainerBuilder instance
*/
protected function loadDocumentManagers(array $dmConfigs, $defaultDM, $defaultDB, $defaultMetadataCache, ContainerBuilder $container)
protected function loadDocumentManagers(array $dmConfigs, $defaultDM, $defaultDB, ContainerBuilder $container)
{
$dms = array();
foreach ($dmConfigs as $name => $documentManager) {
Expand All @@ -124,7 +117,6 @@ protected function loadDocumentManagers(array $dmConfigs, $defaultDM, $defaultDB
$documentManager,
$defaultDM,
$defaultDB,
$defaultMetadataCache,
$container
);
$dms[$name] = sprintf('doctrine.odm.mongodb.%s_document_manager', $name);
Expand All @@ -138,10 +130,9 @@ protected function loadDocumentManagers(array $dmConfigs, $defaultDM, $defaultDB
* @param array $documentManager A document manager configuration array
* @param string $defaultDM The default document manager name
* @param string $defaultDB The default db name
* @param string $defaultMetadataCache The default metadata cache configuration
* @param ContainerBuilder $container A ContainerBuilder instance
*/
protected function loadDocumentManager(array $documentManager, $defaultDM, $defaultDB, $defaultMetadataCache, ContainerBuilder $container)
protected function loadDocumentManager(array $documentManager, $defaultDM, $defaultDB, ContainerBuilder $container)
{
$configServiceName = sprintf('doctrine.odm.mongodb.%s_configuration', $documentManager['name']);
$connectionName = isset($documentManager['connection']) ? $documentManager['connection'] : $documentManager['name'];
Expand All @@ -155,7 +146,7 @@ protected function loadDocumentManager(array $documentManager, $defaultDM, $defa
}

$this->loadDocumentManagerBundlesMappingInformation($documentManager, $odmConfigDef, $container);
$this->loadDocumentManagerMetadataCacheDriver($documentManager, $container, $defaultMetadataCache);
$this->loadObjectManagerCacheDriver($documentManager, $container, 'metadata_cache');

$methods = array(
'setMetadataCacheImpl' => new Reference(sprintf('doctrine.odm.mongodb.%s_metadata_cache', $documentManager['name'])),
Expand Down Expand Up @@ -226,44 +217,6 @@ protected function loadDocumentManager(array $documentManager, $defaultDM, $defa
}
}

/**
* Loads the configured document manager metadata cache driver.
*
* @param array $config A configured document manager array
* @param ContainerBuilder $container A ContainerBuilder instance
* @param array $defaultMetadataCache The default metadata cache configuration array
*/
protected function loadDocumentManagerMetadataCacheDriver(array $documentManager, ContainerBuilder $container, $defaultMetadataCache)
{
$dmMetadataCacheDriver = isset($documentManager['metadata_cache_driver']) ? $documentManager['metadata_cache_driver'] : $defaultMetadataCache;
$type = $dmMetadataCacheDriver['type'];
$serviceName = sprintf('doctrine.odm.mongodb.%s_metadata_cache', $documentManager['name']);

if ('service' === $type) {
$container->setAlias($serviceName, new Alias($dmMetadataCacheDriver['id'], false));

return;
}

if ('memcache' === $type) {
$memcacheClass = isset($dmMetadataCacheDriver['class']) ? $dmMetadataCacheDriver['class'] : sprintf('%%doctrine.odm.mongodb.cache.%s.class%%', $type);
$cacheDef = new Definition($memcacheClass);
$memcacheHost = isset($dmMetadataCacheDriver['host']) ? $dmMetadataCacheDriver['host'] : '%doctrine.odm.mongodb.cache.memcache_host%';
$memcachePort = isset($dmMetadataCacheDriver['port']) ? $dmMetadataCacheDriver['port'] : '%doctrine.odm.mongodb.cache.memcache_port%';
$memcacheInstanceClass = isset($dmMetadataCacheDriver['instance-class']) ? $dmMetadataCacheDriver['instance-class'] : (isset($dmMetadataCacheDriver['instance_class']) ? $dmMetadataCacheDriver['instance_class'] : '%doctrine.odm.mongodb.cache.memcache_instance.class%');
$memcacheInstance = new Definition($memcacheInstanceClass);
$memcacheInstance->addMethodCall('connect', array($memcacheHost, $memcachePort));
$container->setDefinition(sprintf('doctrine.odm.mongodb.%s_memcache_instance', $documentManager['name']), $memcacheInstance);
$cacheDef->addMethodCall('setMemcache', array(new Reference(sprintf('doctrine.odm.mongodb.%s_memcache_instance', $documentManager['name']))));
} else {
$cacheDef = new Definition(sprintf('%%doctrine.odm.mongodb.cache.%s.class%%', $type));
}

$cacheDef->setPublic(false);

$container->setDefinition($serviceName, $cacheDef);
}

/**
* Loads the configured connections.
*
Expand Down
2 changes: 2 additions & 0 deletions Tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ protected function setUp()
$this->container = new ContainerBuilder(new ParameterBag(array(
'kernel.bundles' => array(),
'kernel.cache_dir' => sys_get_temp_dir(),
'kernel.root_dir' => sys_get_temp_dir(),
'kernel.environment' => 'test',
'kernel.debug' => true,
)));

Expand Down
13 changes: 7 additions & 6 deletions Tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public function optionProvider()
array('document_managers' => array('default' => array('mappings' => array('foomap' => array('type' => 'val1'), 'barmap' => array('dir' => 'val2'))))),
array('document_managers' => array('default' => array('mappings' => array('barmap' => array('prefix' => 'val3'))))),
),
array('document_managers' => array('default' => array('logging' => '%kernel.debug%', 'profiler' => array('enabled' => '%kernel.debug%', 'pretty' => '%kernel.debug%'), 'auto_mapping' => false, 'mappings' => array('foomap' => array('type' => 'val1', 'mapping' => true), 'barmap' => array('prefix' => 'val3', 'mapping' => true))))),
array('document_managers' => array('default' => array('metadata_cache_driver' => array('type' => 'array'), 'logging' => '%kernel.debug%', 'profiler' => array('enabled' => '%kernel.debug%', 'pretty' => '%kernel.debug%'), 'auto_mapping' => false, 'mappings' => array('foomap' => array('type' => 'val1', 'mapping' => true), 'barmap' => array('prefix' => 'val3', 'mapping' => true))))),
);

// connections are merged non-recursively.
Expand All @@ -234,8 +234,8 @@ public function optionProvider()
array('document_managers' => array('bardm' => array('database' => 'val3'))),
),
array('document_managers' => array(
'foodm' => array('database' => 'val1', 'logging' => '%kernel.debug%', 'profiler' => array('enabled' => '%kernel.debug%', 'pretty' => '%kernel.debug%'), 'auto_mapping' => false, 'mappings' => array()),
'bardm' => array('database' => 'val3', 'logging' => '%kernel.debug%', 'profiler' => array('enabled' => '%kernel.debug%', 'pretty' => '%kernel.debug%'), 'auto_mapping' => false, 'mappings' => array()),
'foodm' => array('database' => 'val1', 'metadata_cache_driver' => array('type' => 'array'), 'logging' => '%kernel.debug%', 'profiler' => array('enabled' => '%kernel.debug%', 'pretty' => '%kernel.debug%'), 'auto_mapping' => false, 'mappings' => array()),
'bardm' => array('database' => 'val3', 'metadata_cache_driver' => array('type' => 'array'), 'logging' => '%kernel.debug%', 'profiler' => array('enabled' => '%kernel.debug%', 'pretty' => '%kernel.debug%'), 'auto_mapping' => false, 'mappings' => array()),
)),
);

Expand All @@ -250,7 +250,7 @@ public function testNormalizeOptions(array $config, $targetKey, array $normalize
$processor = new Processor();
$configuration = new Configuration(false);
$options = $processor->processConfiguration($configuration, array($config));
$this->assertSame($normalized, $options[$targetKey]);
$this->assertEquals($normalized, $options[$targetKey]);
}

public function getNormalizationTests()
Expand All @@ -276,8 +276,8 @@ public function getNormalizationTests()
)),
'document_managers',
array(
'foo' => array('connection' => 'conn1', 'logging' => '%kernel.debug%', 'profiler' => array('enabled' => '%kernel.debug%', 'pretty' => '%kernel.debug%'), 'auto_mapping' => false, 'mappings' => array()),
'bar' => array('connection' => 'conn2', 'logging' => '%kernel.debug%', 'profiler' => array('enabled' => '%kernel.debug%', 'pretty' => '%kernel.debug%'), 'auto_mapping' => false, 'mappings' => array()),
'foo' => array('connection' => 'conn1', 'metadata_cache_driver' => array('type' => 'array'), 'logging' => '%kernel.debug%', 'profiler' => array('enabled' => '%kernel.debug%', 'pretty' => '%kernel.debug%'), 'auto_mapping' => false, 'mappings' => array()),
'bar' => array('connection' => 'conn2', 'metadata_cache_driver' => array('type' => 'array'), 'logging' => '%kernel.debug%', 'profiler' => array('enabled' => '%kernel.debug%', 'pretty' => '%kernel.debug%'), 'auto_mapping' => false, 'mappings' => array()),
),
),
// mapping configuration that's beneath a specific document manager
Expand All @@ -291,6 +291,7 @@ public function getNormalizationTests()
array(
'foo' => array(
'connection' => 'conn1',
'metadata_cache_driver' => array('type' => 'array'),
'mappings' => array('foo-mapping' => array('type' => 'xml', 'mapping' => true)),
'logging' => '%kernel.debug%',
'profiler' => array('enabled' => '%kernel.debug%', 'pretty' => '%kernel.debug%'),
Expand Down

0 comments on commit bb1a415

Please sign in to comment.