Skip to content
Browse files

[DoctrineMongoDBBundle] switched to compiler passes for proxy/hydrato…

…r directory creation and event listeners
  • Loading branch information...
1 parent 50b9c9e commit 9a8c63a9d16964bcf58cff6ba1988f56f4903094 @avalanche123 avalanche123 committed with fabpot Jan 16, 2011
View
30 DependencyInjection/Compiler/CreateHydratorDirectoryPass.php
@@ -0,0 +1,30 @@
+<?php
+
+namespace Symfony\Bundle\DoctrineMongoDBBundle\DependencyInjection\Compiler;
+
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
+
+class CreateHydratorDirectoryPass implements CompilerPassInterface
+{
+ public function process(ContainerBuilder $container)
+ {
+ if (!$container->hasParameter('doctrine.odm.mongodb.hydrator_dir')) {
+ return;
+ }
+ // Don't do anything if auto_generate_hydrator_classes is false
+ if (!$container->getParameter('doctrine.odm.mongodb.auto_generate_hydrator_classes')) {
+ return;
+ }
+ // Create document proxy directory
+ $hydratorCacheDir = $container->getParameter('doctrine.odm.mongodb.hydrator_dir');
+ if (!is_dir($hydratorCacheDir)) {
+ if (false === @mkdir($hydratorCacheDir, 0777, true)) {
+ die(sprintf('Unable to create the Doctrine Hydrator directory (%s)', dirname($hydratorCacheDir)));
+ }
+ } elseif (!is_writable($hydratorCacheDir)) {
+ die(sprintf('Unable to write in the Doctrine Hydrator directory (%s)', $hydratorCacheDir));
+ }
+ }
+
+}
View
30 DependencyInjection/Compiler/CreateProxyDirectoryPass.php
@@ -0,0 +1,30 @@
+<?php
+
+namespace Symfony\Bundle\DoctrineMongoDBBundle\DependencyInjection\Compiler;
+
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
+
+class CreateProxyDirectoryPass implements CompilerPassInterface
+{
+ public function process(ContainerBuilder $container)
+ {
+ if (!$container->hasParameter('doctrine.odm.mongodb.proxy_dir')) {
+ return;
+ }
+ // Don't do anything if auto_generate_proxy_classes is false
+ if (!$container->getParameter('doctrine.odm.mongodb.auto_generate_proxy_classes')) {
+ return;
+ }
+ // Create document proxy directory
+ $proxyCacheDir = $container->getParameter('doctrine.odm.mongodb.proxy_dir');
+ if (!is_dir($proxyCacheDir)) {
+ if (false === @mkdir($proxyCacheDir, 0777, true)) {
+ die(sprintf('Unable to create the Doctrine Proxy directory (%s)', dirname($proxyCacheDir)));
+ }
+ } elseif (!is_writable($proxyCacheDir)) {
+ die(sprintf('Unable to write in the Doctrine Proxy directory (%s)', $proxyCacheDir));
+ }
+ }
+
+}
View
59 DependencyInjection/Compiler/RegisterEventListenersAndSubscribersPass.php
@@ -0,0 +1,59 @@
+<?php
+
+namespace Symfony\Bundle\DoctrineMongoDBBundle\DependencyInjection\Compiler;
+
+use Symfony\Component\DependencyInjection\Reference;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
+
+class RegisterEventListenersAndSubscribersPass implements CompilerPassInterface
+{
+ protected $container;
+
+ public function process(ContainerBuilder $container)
+ {
+ $this->container = $container;
+ foreach ($container->findTaggedServiceIds('doctrine.odm.mongodb.event_manager') as $id => $tag) {
+ $definition = $container->getDefinition($id);
+ $prefix = substr($id, 0, -1 * strlen('_event_manager'));
+ $this->registerListeners($prefix, $definition);
+ $this->registerSubscribers($prefix, $definition);
+ }
+ }
+
+ protected function registerSubscribers($prefix, $definition)
+ {
+ $subscribers = array_merge(
+ $this->container->findTaggedServiceIds('doctrine.common.event_subscriber'),
+ $this->container->findTaggedServiceIds($prefix.'_event_subscriber')
+ );
+
+ foreach ($subscribers as $id => $instances) {
+ $definition->addMethodCall('addEventSubscriber', array(new Reference($id)));
+ }
+ }
+
+ protected function registerListeners($prefix, $definition)
+ {
+ $listeners = array_merge(
+ $this->container->findTaggedServiceIds('doctrine.common.event_listener'),
+ $this->container->findTaggedServiceIds($prefix.'_event_listener')
+ );
+
+ foreach ($listeners as $listenerId => $instances) {
+ $events = array();
+ foreach ($instances as $attributes) {
+ if (isset($attributes['event'])) {
+ $events[] = $attributes['event'];
+ }
+ }
+
+ if (0 < count($events)) {
+ $definition->addMethodCall('addEventListener', array(
+ $events,
+ new Reference($listenerId),
+ ));
+ }
+ }
+ }
+}
View
57 DependencyInjection/DoctrineMongoDBExtension.php
@@ -5,6 +5,7 @@
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Resource\FileResource;
@@ -31,46 +32,12 @@ class DoctrineMongoDBExtension extends AbstractDoctrineExtension
*/
public function mongodbLoad($config, ContainerBuilder $container)
{
- $this->createProxyDirectory($container->getParameter('kernel.cache_dir'));
- $this->createHydratorDirectory($container->getParameter('kernel.cache_dir'));
$this->loadDefaults($config, $container);
$this->loadConnections($config, $container);
$this->loadDocumentManagers($config, $container);
}
/**
- * Create the Doctrine MongoDB ODM Document proxy directory
- */
- protected function createProxyDirectory($tmpDir)
- {
- // Create document proxy directory
- $proxyCacheDir = $tmpDir.'/doctrine/odm/mongodb/Proxies';
- if (!is_dir($proxyCacheDir)) {
- if (false === @mkdir($proxyCacheDir, 0777, true)) {
- die(sprintf('Unable to create the Doctrine Proxy directory (%s)', dirname($proxyCacheDir)));
- }
- } elseif (!is_writable($proxyCacheDir)) {
- die(sprintf('Unable to write in the Doctrine Proxy directory (%s)', $proxyCacheDir));
- }
- }
-
- /**
- * Create the Doctrine MongoDB ODM Document hydrator directory
- */
- protected function createHydratorDirectory($tmpDir)
- {
- // Create document hydrator directory
- $hydratorCacheDir = $tmpDir.'/doctrine/odm/mongodb/Hydrators';
- if (!is_dir($hydratorCacheDir)) {
- if (false === @mkdir($hydratorCacheDir, 0777, true)) {
- die(sprintf('Unable to create the Doctrine Hydrator directory (%s)', dirname($hydratorCacheDir)));
- }
- } elseif (!is_writable($hydratorCacheDir)) {
- die(sprintf('Unable to write in the Doctrine Hydrator directory (%s)', $hydratorCacheDir));
- }
- }
-
- /**
* Loads the default configuration.
*
* @param array $config An array of configuration settings
@@ -171,20 +138,8 @@ protected function loadDocumentManager(array $documentManager, ContainerBuilder
$eventManagerId = sprintf('doctrine.odm.mongodb.%s_event_manager', $eventManagerName);
if (!$container->hasDefinition($eventManagerId)) {
$eventManagerDef = new Definition('%doctrine.odm.mongodb.event_manager_class%');
- $eventManagerDef->addMethodCall('loadTaggedEventListeners', array(
- new Reference('service_container'),
- ));
- $eventManagerDef->addMethodCall('loadTaggedEventListeners', array(
- new Reference('service_container'),
- sprintf('doctrine.odm.mongodb.%s_event_listener', $eventManagerName),
- ));
- $eventManagerDef->addMethodCall('loadTaggedEventSubscribers', array(
- new Reference('service_container'),
- ));
- $eventManagerDef->addMethodCall('loadTaggedEventSubscribers', array(
- new Reference('service_container'),
- sprintf('doctrine.odm.mongodb.%s_event_subscriber', $eventManagerName),
- ));
+ $eventManagerDef->addTag('doctrine.odm.mongodb.event_manager');
+ $eventManagerDef->setPublic(false);
$container->setDefinition($eventManagerId, $eventManagerDef);
}
@@ -201,7 +156,11 @@ protected function loadDocumentManager(array $documentManager, ContainerBuilder
if ($documentManager['name'] == $defaultDocumentManager) {
$container->setAlias(
'doctrine.odm.mongodb.document_manager',
- sprintf('doctrine.odm.mongodb.%s_document_manager', $documentManager['name'])
+ new Alias(sprintf('doctrine.odm.mongodb.%s_document_manager', $documentManager['name']))
+ );
+ $container->setAlias(
+ 'doctrine.odm.mongodb.event_manager',
+ new Alias(sprintf('doctrine.odm.mongodb.%s_event_manager', $documentManager['name']))
);
}
}
View
13 DoctrineMongoDBBundle.php
@@ -2,7 +2,12 @@
namespace Symfony\Bundle\DoctrineMongoDBBundle;
+use Symfony\Component\DependencyInjection\Compiler\PassConfig;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
+use Symfony\Bundle\DoctrineMongoDBBundle\DependencyInjection\Compiler\CreateHydratorDirectoryPass;
+use Symfony\Bundle\DoctrineMongoDBBundle\DependencyInjection\Compiler\CreateProxyDirectoryPass;
+use Symfony\Bundle\DoctrineMongoDBBundle\DependencyInjection\Compiler\RegisterEventListenersAndSubscribersPass;
/**
* Doctrine MongoDB ODM bundle.
@@ -13,4 +18,12 @@
*/
class DoctrineMongoDBBundle extends Bundle
{
+ public function registerExtensions(ContainerBuilder $container)
+ {
+ parent::registerExtensions($container);
+
+ $container->addCompilerPass(new RegisterEventListenersAndSubscribersPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION);
+ $container->addCompilerPass(new CreateProxyDirectoryPass(), PassConfig::TYPE_BEFORE_REMOVING);
+ $container->addCompilerPass(new CreateHydratorDirectoryPass(), PassConfig::TYPE_BEFORE_REMOVING);
+ }
}
View
15 Resources/config/mongodb.xml
@@ -14,14 +14,16 @@
<parameter key="doctrine.odm.mongodb.document_manager_class">Doctrine\ODM\MongoDB\DocumentManager</parameter>
<parameter key="doctrine.odm.mongodb.logger_class">Symfony\Bundle\DoctrineMongoDBBundle\Logger\DoctrineMongoDBLogger</parameter>
<parameter key="doctrine.odm.mongodb.data_collector_class">Symfony\Bundle\DoctrineMongoDBBundle\DataCollector\DoctrineMongoDBDataCollector</parameter>
- <parameter key="doctrine.odm.mongodb.event_manager_class">Symfony\Bundle\DoctrineAbstractBundle\Event\EventManager</parameter>
+ <parameter key="doctrine.odm.mongodb.event_manager_class">Doctrine\Common\EventManager</parameter>
<!-- proxies -->
<parameter key="doctrine.odm.mongodb.proxy_namespace">Proxies</parameter>
+ <parameter key="doctrine.odm.mongodb.proxy_dir">%kernel.cache_dir%/doctrine/odm/mongodb/Proxies</parameter>
<parameter key="doctrine.odm.mongodb.auto_generate_proxy_classes">false</parameter>
<!-- hydrators -->
<parameter key="doctrine.odm.mongodb.hydrator_namespace">Proxies</parameter>
+ <parameter key="doctrine.odm.mongodb.hydrator_dir">%kernel.cache_dir%/doctrine/odm/mongodb/Hydrators</parameter>
<parameter key="doctrine.odm.mongodb.auto_generate_hydrator_classes">false</parameter>
<!-- cache -->
@@ -85,16 +87,5 @@
<service id="security.user.document_manager" alias="doctrine.odm.mongodb.default_document_manager" />
- <!-- events -->
- <service id="doctrine.odm.mongodb.event_manager" class="%doctrine.odm.mongodb.event_manager_class%">
- <call method="loadTaggedEventListeners">
- <argument type="service" id="service_container" />
- <argument>doctrine.odm.mongodb.event_listener</argument>
- </call>
- <call method="loadTaggedEventSubscribers">
- <argument type="service" id="service_container" />
- <argument>doctrine.odm.mongodb.event_listener</argument>
- </call>
- </service>
</services>
</container>
View
2 Tests/ContainerTest.php
@@ -55,6 +55,6 @@ public function testContainer()
$this->assertInstanceOf('Doctrine\Common\Cache\ArrayCache', $container->get('doctrine.odm.mongodb.cache'));
$this->assertInstanceOf('Doctrine\ODM\MongoDB\DocumentManager', $container->get('doctrine.odm.mongodb.document_manager'));
$this->assertInstanceof('Doctrine\Common\Annotations\AnnotationReader', $container->get('doctrine.odm.mongodb.metadata.annotation_reader'));
- $this->assertInstanceof('Symfony\Bundle\DoctrineAbstractBundle\Event\EventManager', $container->get('doctrine.odm.mongodb.event_manager'));
+ $this->assertInstanceof('Doctrine\Common\EventManager', $container->get('doctrine.odm.mongodb.event_manager'));
}
}

0 comments on commit 9a8c63a

Please sign in to comment.
Something went wrong with that request. Please try again.