Permalink
Browse files

Merge pull request #170 from WesleyVanOpdorp/FilterConfigSupport

Filter config support
  • Loading branch information...
2 parents c711aa0 + 9fa9d8e commit ae16f64dfa9c2b0e702b9ace92d98d7c39f4d715 @jmikola jmikola committed Feb 11, 2013
@@ -109,6 +109,29 @@ private function addDocumentManagersSection(ArrayNodeDefinition $rootNode)
->end()
->end()
->scalarNode('auto_mapping')->defaultFalse()->end()
+ ->arrayNode('filters')
+ ->useAttributeAsKey('name')
+ ->prototype('array')
+ ->beforeNormalization()
+ ->ifString()
+ ->then(function($v) { return array('class' => $v); })
+ ->end()
+ ->beforeNormalization()
+ // The content of the XML node is returned as the "value" key so we need to rename it
+ ->ifTrue(function($v) {return is_array($v) && isset($v['value']); })
+ ->then(function($v) {
+ $v['class'] = $v['value'];
+ unset($v['value']);
+
+ return $v;
+ })
+ ->end()
+ ->children()
+ ->scalarNode('class')->isRequired()->end()
+ ->booleanNode('enabled')->defaultFalse()->end()
+ ->end()
+ ->end()
+ ->end()
->scalarNode('retry_connect')->defaultValue(0)->end()
->scalarNode('retry_query')->defaultValue(0)->end()
->arrayNode('metadata_cache_driver')
@@ -193,6 +193,22 @@ protected function loadDocumentManager(array $documentManager, $defaultDM, $defa
$methods['setLoggerCallable'] = array($loggers[0], 'logQuery');
}
+ $enabledFilters = array();
+ foreach ($documentManager['filters'] as $name => $filter) {
+ $odmConfigDef->addMethodCall('addFilter', array($name, $filter['class']));
+ if ($filter['enabled']) {
+ $enabledFilters[] = $name;
+ }
+ }
+
+ $managerConfiguratorName = sprintf('doctrine_mongodb.odm.%s_manager_configurator', $documentManager['name']);
+
+ $managerConfiguratorDef = $container
+ ->setDefinition($managerConfiguratorName, new DefinitionDecorator('doctrine_mongodb.odm.manager_configurator.abstract'))
+ ->replaceArgument(0, $enabledFilters)
+ ;
+
+
foreach ($methods as $method => $arg) {
if ($odmConfigDef->hasMethodCall($method)) {
$odmConfigDef->removeMethodCall($method);
@@ -210,7 +226,11 @@ protected function loadDocumentManager(array $documentManager, $defaultDM, $defa
$odmDmDef->setFactoryClass('%doctrine_mongodb.odm.document_manager.class%');
$odmDmDef->setFactoryMethod('create');
$odmDmDef->addTag('doctrine_mongodb.odm.document_manager');
- $container->setDefinition(sprintf('doctrine_mongodb.odm.%s_document_manager', $documentManager['name']), $odmDmDef);
+
+ $container
+ ->setDefinition(sprintf('doctrine_mongodb.odm.%s_document_manager', $documentManager['name']), $odmDmDef)
+ ->setConfigurator(array(new Reference($managerConfiguratorName), 'configure'))
+ ;
if ($documentManager['name'] == $defaultDM) {
$container->setAlias(
View
@@ -0,0 +1,61 @@
+<?php
+
+/*
+ * This file is part of the Doctrine MongoDB Bundle
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Doctrine\Bundle\MongoDBBundle;
+
+use Doctrine\ODM\MongoDB\DocumentManager;
+
+/**
+ * Configurator for an DocumentManager
+ *
+ * @author Wesley van Opdorp <wesley.van.opdorp@freshheads.com>
+ */
+class ManagerConfigurator
+{
+ private $enabledFilters = array();
+
+ /**
+ * Construct.
+ *
+ * @param array $enabledFilters
+ */
+ public function __construct(array $enabledFilters)
+ {
+ $this->enabledFilters = $enabledFilters;
+ }
+
+ /**
+ * Create a connection by name.
+ *
+ * @param DocumentManager $documentManager
+ */
+ public function configure(DocumentManager $documentManager)
+ {
+ $this->enableFilters($documentManager);
+ }
+
+ /**
+ * Enable filters for an given document manager
+ *
+ * @param DocumentManager $documentManager
+ *
+ * @return null
+ */
+ private function enableFilters(DocumentManager $documentManager)
+ {
+ if (empty($this->enabledFilters)) {
+ return;
+ }
+
+ $filterCollection = $documentManager->getFilterCollection();
+ foreach ($this->enabledFilters as $filter) {
+ $filterCollection->enable($filter);
+ }
+ }
+}
@@ -9,6 +9,7 @@
<parameter key="doctrine_mongodb.odm.connection.class">Doctrine\MongoDB\Connection</parameter>
<parameter key="doctrine_mongodb.odm.configuration.class">Doctrine\ODM\MongoDB\Configuration</parameter>
<parameter key="doctrine_mongodb.odm.document_manager.class">Doctrine\ODM\MongoDB\DocumentManager</parameter>
+ <parameter key="doctrine_mongodb.odm.manager_configurator.class">Doctrine\Bundle\MongoDBBundle\ManagerConfigurator</parameter>
<parameter key="doctrine_mongodb.odm.logger.class">Doctrine\Bundle\MongoDBBundle\Logger\Logger</parameter>
<parameter key="doctrine_mongodb.odm.logger.aggregate.class">Doctrine\Bundle\MongoDBBundle\Logger\AggregateLogger</parameter>
<parameter key="doctrine_mongodb.odm.data_collector.standard.class">Doctrine\Bundle\MongoDBBundle\DataCollector\StandardDataCollector</parameter>
@@ -79,6 +80,11 @@
<argument>%doctrine_mongodb.odm.yml_mapping_dirs%</argument>
</service>
+ <!-- The configurator cannot be a private service -->
+ <service id="doctrine_mongodb.odm.manager_configurator.abstract" class="%doctrine_mongodb.odm.manager_configurator.class%" abstract="true">
+ <argument type="collection" />
+ </service>
+
<!-- cache -->
<service id="doctrine_mongodb.odm.cache.array" class="%doctrine_mongodb.odm.cache.array.class%" />
@@ -67,6 +67,7 @@ public function testFullConfiguration($config)
$configuration = new Configuration(false);
$options = $processor->processConfiguration($configuration, array($config));
+
$expected = array(
'proxy_dir' => '%kernel.cache_dir%/doctrine/odm/mongodb/Proxies',
'proxy_namespace' => 'Test_Proxies',
@@ -105,6 +106,12 @@ public function testFullConfiguration($config)
'dm1' => array(
'logging' => '%kernel.debug%',
'auto_mapping' => false,
+ 'filters' => array(
+ 'test_filter' => array(
+ 'class' => 'TestClass',
+ 'enabled' => true
+ )
+ ),
'metadata_cache_driver' => array(
'type' => 'memcache',
'class' => 'fooClass',
@@ -130,6 +137,7 @@ public function testFullConfiguration($config)
'database' => 'db1',
'logging' => true,
'auto_mapping' => false,
+ 'filters' => array(),
'metadata_cache_driver' => array(
'type' => 'apc',
),
@@ -227,7 +235,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('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)), 'retry_connect' => 0, 'retry_query' => 0))),
+ 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, 'filters' => array(), 'mappings' => array('foomap' => array('type' => 'val1', 'mapping' => true), 'barmap' => array('prefix' => 'val3', 'mapping' => true)), 'retry_connect' => 0, 'retry_query' => 0))),
);
// connections are merged non-recursively.
@@ -249,8 +257,8 @@ public function optionProvider()
array('document_managers' => array('bardm' => array('database' => 'val3'))),
),
array('document_managers' => 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(), 'retry_connect' => 0, 'retry_query' => 0),
- '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(), 'retry_connect' => 0, 'retry_query' => 0),
+ 'foodm' => array('database' => 'val1', 'metadata_cache_driver' => array('type' => 'array'), 'logging' => '%kernel.debug%', 'profiler' => array('enabled' => '%kernel.debug%', 'pretty' => '%kernel.debug%'), 'auto_mapping' => false, 'filters' => array(), 'mappings' => array(), 'retry_connect' => 0, 'retry_query' => 0),
+ 'bardm' => array('database' => 'val3', 'metadata_cache_driver' => array('type' => 'array'), 'logging' => '%kernel.debug%', 'profiler' => array('enabled' => '%kernel.debug%', 'pretty' => '%kernel.debug%'), 'auto_mapping' => false, 'filters' => array(), 'mappings' => array(), 'retry_connect' => 0, 'retry_query' => 0),
)),
);
@@ -291,8 +299,8 @@ public function getNormalizationTests()
)),
'document_managers',
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(), 'retry_connect' => 0, 'retry_query' => 0),
- '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(), 'retry_connect' => 0, 'retry_query' => 0),
+ 'foo' => array('connection' => 'conn1', 'metadata_cache_driver' => array('type' => 'array'), 'logging' => '%kernel.debug%', 'profiler' => array('enabled' => '%kernel.debug%', 'pretty' => '%kernel.debug%'), 'auto_mapping' => false, 'filters' => array(), 'mappings' => array(), 'retry_connect' => 0, 'retry_query' => 0),
+ 'bar' => array('connection' => 'conn2', 'metadata_cache_driver' => array('type' => 'array'), 'logging' => '%kernel.debug%', 'profiler' => array('enabled' => '%kernel.debug%', 'pretty' => '%kernel.debug%'), 'auto_mapping' => false, 'filters' => array(), 'mappings' => array(), 'retry_connect' => 0, 'retry_query' => 0),
),
),
// mapping configuration that's beneath a specific document manager
@@ -311,6 +319,7 @@ public function getNormalizationTests()
'logging' => '%kernel.debug%',
'profiler' => array('enabled' => '%kernel.debug%', 'pretty' => '%kernel.debug%'),
'auto_mapping' => false,
+ 'filters' => array(),
'retry_connect' => 0,
'retry_query' => 0,
),
@@ -39,6 +39,10 @@ doctrine_mongodb:
host: host_val
port: 1234
instance_class: instance_val
+ filters:
+ test-filter:
+ class: TestClass
+ enabled: true
dm2:
connection: dm2_connection
database: db1

0 comments on commit ae16f64

Please sign in to comment.