Skip to content

Commit

Permalink
Refactor Doctrine Bundle to use Symfony DIC Enabled EventManager.
Browse files Browse the repository at this point in the history
  • Loading branch information
beberlei authored and fabpot committed Jan 3, 2011
1 parent fa7fded commit 302dbd1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ protected function loadDbalConnection(array $connection, ContainerBuilder $conta
$containerDef->addMethodCall('setSqlLogger', array(new Reference('doctrine.dbal.logger')));
$container->setDefinition(sprintf('doctrine.dbal.%s_connection.configuration', $connection['name']), $containerDef);

$eventManagerDef = new Definition(isset($connection['event-manager-class']) ? $connection['event-manager-class'] : $connection['event_manager_class']);
$container->setDefinition(sprintf('doctrine.dbal.%s_connection.event_manager', $connection['name']), $eventManagerDef);

$driverOptions = array();
$driverDef = new Definition('Doctrine\DBAL\DriverManager');
$driverDef->setFactoryMethod('getConnection');
Expand Down Expand Up @@ -147,6 +144,32 @@ protected function loadDbalConnection(array $connection, ContainerBuilder $conta
}
}

// event manager
$eventManagerName = isset($connection['event_manager']) ? $connection['event_manager'] : $connection['name'];
$eventManagerId = sprintf('doctrine.dbal.%s_connection.event_manager', $eventManagerName);
if (!$container->hasDefinition($eventManagerId)) {
$eventManagerDef = new Definition('%doctrine.dbal.event_manager_class%');
$eventManagerDef->addMethodCall('loadTaggedEventListeners', array(
new Reference('service_container'),
));
$eventManagerDef->addMethodCall('loadTaggedEventListeners', array(
new Reference('service_container'),
sprintf('doctrine.dbal.%s_event_listener', $eventManagerName),
));
$eventManagerDef->addMethodCall('loadTaggedEventSubscribers', array(
new Reference('service_container'),
));
$eventManagerDef->addMethodCall('loadTaggedEventSubscribers', array(
new Reference('service_container'),
sprintf('doctrine.dbal.%s_event_subscriber', $eventManagerName),
));
$container->setDefinition($eventManagerId, $eventManagerDef);
}

if ($container->getParameter('doctrine.dbal.default_connection') == $connection['name']) {
$container->setAlias('doctrine.dbal.event_manager', sprintf('doctrine.dbal.%s_connection.event_manager', $connection['name']));
}

$driverDef->setArguments(array(
$driverOptions,
new Reference(sprintf('doctrine.dbal.%s_connection.configuration', $connection['name'])),
Expand Down Expand Up @@ -305,8 +328,10 @@ protected function loadOrmEntityManager(array $entityManager, ContainerBuilder $
$entityManagerService = sprintf('doctrine.orm.%s_entity_manager', $entityManager['name']);

if (!$container->hasDefinition($entityManagerService) || isset($entityManager['connection'])) {
$connectionName = isset($entityManager['connection']) ? $entityManager['connection'] : $entityManager['name'];

$ormEmArgs = array(
new Reference(sprintf('doctrine.dbal.%s_connection', isset($entityManager['connection']) ? $entityManager['connection'] : $entityManager['name'])),
new Reference(sprintf('doctrine.dbal.%s_connection', $connectionName)),
new Reference(sprintf('doctrine.orm.%s_configuration', $entityManager['name']))
);
$ormEmDef = new Definition('%doctrine.orm.entity_manager_class%', $ormEmArgs);
Expand All @@ -320,6 +345,10 @@ protected function loadOrmEntityManager(array $entityManager, ContainerBuilder $
sprintf('doctrine.orm.%s_entity_manager', $entityManager['name'])
);
}
$container->setAlias(
sprintf('doctrine.orm.%s_entity_manager.event_manager', $entityManager['name']),
sprintf('doctrine.dbal.%s_connection.event_manager', $connectionName)
);
}
}

Expand Down
12 changes: 0 additions & 12 deletions src/Symfony/Bundle/DoctrineBundle/Resources/config/dbal.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,5 @@
<tag name="data_collector" template="DoctrineBundle:Collector:db" />
<argument type="service" id="doctrine.dbal.logger" />
</service>

<!-- events -->
<service id="doctrine.dbal.event_manager" class="%doctrine.dbal.event_manager_class%">
<call method="loadTaggedEventListeners">
<argument type="service" id="service_container" />
<argument>doctrine.dbal.event_listener</argument>
</call>
<call method="loadTaggedEventSubscribers">
<argument type="service" id="service_container" />
<argument>doctrine.dbal.event_listener</argument>
</call>
</service>
</services>
</container>
3 changes: 2 additions & 1 deletion src/Symfony/Bundle/DoctrineBundle/Tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function testContainer()
$this->assertInstanceOf('Doctrine\DBAL\Logging\DebugStack', $container->get('doctrine.dbal.logger'));
$this->assertInstanceOf('Symfony\Bundle\DoctrineBundle\DataCollector\DoctrineDataCollector', $container->get('doctrine.data_collector'));
$this->assertInstanceOf('Doctrine\DBAL\Configuration', $container->get('doctrine.dbal.default_connection.configuration'));
$this->assertInstanceOf('Doctrine\Common\EventManager', $container->get('doctrine.dbal.default_connection.event_manager'));
$this->assertInstanceOf('Symfony\Bundle\DoctrineAbstractBundle\Event\EventManager', $container->get('doctrine.dbal.default_connection.event_manager'));
$this->assertInstanceOf('Doctrine\DBAL\Connection', $container->get('doctrine.dbal.default_connection'));
$this->assertInstanceOf('Doctrine\Common\Annotations\AnnotationReader', $container->get('doctrine.orm.metadata.annotation_reader'));
$this->assertInstanceOf('Doctrine\ORM\Configuration', $container->get('doctrine.orm.default_configuration'));
Expand All @@ -60,6 +60,7 @@ public function testContainer()
$this->assertInstanceOf('Doctrine\ORM\EntityManager', $container->get('doctrine.orm.default_entity_manager'));
$this->assertInstanceOf('Doctrine\DBAL\Connection', $container->get('database_connection'));
$this->assertInstanceOf('Doctrine\ORM\EntityManager', $container->get('doctrine.orm.entity_manager'));
$this->assertInstanceOf('Symfony\Bundle\DoctrineAbstractBundle\Event\EventManager', $container->get('doctrine.orm.default_entity_manager.event_manager'));
$this->assertInstanceOf('Symfony\Bundle\DoctrineAbstractBundle\Event\EventManager', $container->get('doctrine.dbal.event_manager'));
}
}

0 comments on commit 302dbd1

Please sign in to comment.