merged branch hacfi/2.3_doctrine_compiler_pass (PR #8409)

This PR was submitted for the 2.3 branch but it was merged into the master branch instead (closes #8409).

Discussion
----------

[DoctrineBridge] Only load event managers if tagged event subscribers are found

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes

This PR only wraps the code that adds event subscribers / listeners in the Doctrine compiler pass RegisterEventListenersAndSubscribersPass so it doesn't run if no tagged services are found. So in general it doesn't change anything besides not running a few lines of code if it's not necessary. The reason I want this change is that it will allow DoctrinePHPCRBundle to be installed without Doctrine2 PHPCR ODM (I just want the PHPCR session..I don't need the ODM). This doesn't work currently as it tries to load the entity managers resulting in an error (Error: The service definition "doctrine_phpcr.odm.default_session.event_manager" does not exist.).

Commits
-------

63bc18c [DoctrineBridge] Only load event managers if tagged event subscribers are found
This commit is contained in:
Fabien Potencier 2013-07-21 21:51:05 +02:00
commit ecc2f2b626
2 changed files with 38 additions and 15 deletions

View File

@ -53,6 +53,13 @@ class RegisterEventListenersAndSubscribersPass implements CompilerPassInterface
return;
}
$taggedSubscribers = $container->findTaggedServiceIds($this->tagPrefix.'.event_subscriber');
$taggedListeners = $container->findTaggedServiceIds($this->tagPrefix.'.event_listener');
if (empty($taggedSubscribers) && empty($taggedListeners)) {
return;
}
$this->container = $container;
$this->connections = $container->getParameter($this->connections);
$sortFunc = function($a, $b) {
@ -62,26 +69,31 @@ class RegisterEventListenersAndSubscribersPass implements CompilerPassInterface
return $a > $b ? -1 : 1;
};
$subscribersPerCon = $this->groupByConnection($container->findTaggedServiceIds($this->tagPrefix.'.event_subscriber'));
foreach ($subscribersPerCon as $con => $subscribers) {
$em = $this->getEventManager($con);
uasort($subscribers, $sortFunc);
foreach ($subscribers as $id => $instance) {
$em->addMethodCall('addEventSubscriber', array(new Reference($id)));
if (!empty($taggedSubscribers)) {
$subscribersPerCon = $this->groupByConnection($taggedSubscribers);
foreach ($subscribersPerCon as $con => $subscribers) {
$em = $this->getEventManager($con);
uasort($subscribers, $sortFunc);
foreach ($subscribers as $id => $instance) {
$em->addMethodCall('addEventSubscriber', array(new Reference($id)));
}
}
}
$listenersPerCon = $this->groupByConnection($container->findTaggedServiceIds($this->tagPrefix.'.event_listener'), true);
foreach ($listenersPerCon as $con => $listeners) {
$em = $this->getEventManager($con);
if (!empty($taggedListeners)) {
$listenersPerCon = $this->groupByConnection($taggedListeners, true);
foreach ($listenersPerCon as $con => $listeners) {
$em = $this->getEventManager($con);
uasort($listeners, $sortFunc);
foreach ($listeners as $id => $instance) {
$em->addMethodCall('addEventListener', array(
array_unique($instance['event']),
isset($instance['lazy']) && $instance['lazy'] ? $id : new Reference($id),
));
uasort($listeners, $sortFunc);
foreach ($listeners as $id => $instance) {
$em->addMethodCall('addEventListener', array(
array_unique($instance['event']),
isset($instance['lazy']) && $instance['lazy'] ? $id : new Reference($id),
));
}
}
}
}

View File

@ -109,6 +109,17 @@ class RegisterEventListenersAndSubscribersPassTest extends \PHPUnit_Framework_Te
$this->assertEquals(array('c', 'd', 'e', 'b', 'a'), $this->getServiceOrder($container, 'addEventSubscriber'));
}
public function testProcessNoTaggedServices()
{
$container = $this->createBuilder(true);
$this->process($container);
$this->assertEquals(array(), $container->getDefinition('doctrine.dbal.default_connection.event_manager')->getMethodCalls());
$this->assertEquals(array(), $container->getDefinition('doctrine.dbal.second_connection.event_manager')->getMethodCalls());
}
private function process(ContainerBuilder $container)
{
$pass = new RegisterEventListenersAndSubscribersPass('doctrine.connections', 'doctrine.dbal.%s_connection.event_manager', 'doctrine');