Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implemented filter configuration and loading

  • Loading branch information...
commit b381a83de7bb5c64b4beee26fb21eca5966a167b 1 parent 81bab52
@WesleyVanOpdorp WesleyVanOpdorp authored
View
23 DependencyInjection/Configuration.php 100644 → 100755
@@ -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')
View
22 DependencyInjection/DoctrineMongoDBExtension.php 100644 → 100755
@@ -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
66 ManagerConfigurator.php
@@ -0,0 +1,66 @@
+<?php
+
+/*
+ * This file is part of the Doctrine Bundle
+ *
+ * The code was originally distributed inside the Symfony framework.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ * (c) Doctrine Project, Benjamin Eberlei <kontakt@beberlei.de>
+ *
+ * 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 EntityManager
+ *
+ * @author Christophe Coevoet <stof@notk.org>
+ */
+class ManagerConfigurator
+{
+ private $enabledFilters = array();
+
+ /**
+ * Construct.
+ *
+ * @param array $enabledFilters
+ */
+ public function __construct(array $enabledFilters)
+ {
+ $this->enabledFilters = $enabledFilters;
+ }
+
+ /**
+ * Create a connection by name.
+ *
+ * @param EntityManager $entityManager
+ */
+ public function configure(DocumentManager $documentManager)
+ {
+ $this->enableFilters($documentManager);
+ }
+
+ /**
+ * Enable filters for an given entity manager
+ *
+ * @param EntityManager $entityManager
+ *
+ * @return null
+ */
+ private function enableFilters(DocumentManager $documentManager)
+ {
+ if (empty($this->enabledFilters)) {
+ return;
+ }
+
+ $filterCollection = $documentManager->getFilterCollection();
+ foreach ($this->enabledFilters as $filter) {
+ $filterCollection->enable($filter);
+ }
+ }
+}
View
6 Resources/config/mongodb.xml 100644 → 100755
@@ -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%" />
View
0  Tests/DependencyInjection/AbstractMongoDBExtensionTest.php 100644 → 100755
File mode changed
Please sign in to comment.
Something went wrong with that request. Please try again.