Skip to content

Commit 37f2c3d

Browse files
committed
bug #11160 [DoctrineBridge] Abstract Doctrine Subscribers with tags (merk)
This PR was merged into the 2.3 branch. Discussion ---------- [DoctrineBridge] Abstract Doctrine Subscribers with tags | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | this one | License | MIT | Doc PR | N/A I've hit a problem with some doctrine listeners, built by decorating an abstract definition. I want the abstract definition to hold the tag, however because the RegisterEventListenersAndSubscribersPass runs before abstract definitions are removed, they get added as method calls to the EventManager definition, which once the abstract service is removed, we end up with a method call that breaks the container. I don't know if this is the best approach, it might be better not to return abstract services when calling `findTaggedServiceIds` instead? Commits ------- cbcf513 Disallow abstract definitions from doctrine event listener registration
2 parents 8605c42 + cbcf513 commit 37f2c3d

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/Symfony/Bridge/Doctrine/DependencyInjection/CompilerPass/RegisterEventListenersAndSubscribersPass.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ public function process(ContainerBuilder $container)
6868

6969
uasort($subscribers, $sortFunc);
7070
foreach ($subscribers as $id => $instance) {
71+
if ($container->getDefinition($id)->isAbstract()) {
72+
throw new \InvalidArgumentException(sprintf('The abstract service "%s" cannot be tagged as a doctrine event subscriber.', $id));
73+
}
74+
7175
$em->addMethodCall('addEventSubscriber', array(new Reference($id)));
7276
}
7377
}
@@ -78,6 +82,10 @@ public function process(ContainerBuilder $container)
7882

7983
uasort($listeners, $sortFunc);
8084
foreach ($listeners as $id => $instance) {
85+
if ($container->getDefinition($id)->isAbstract()) {
86+
throw new \InvalidArgumentException(sprintf('The abstract service "%s" cannot be tagged as a doctrine event listener.', $id));
87+
}
88+
8189
$em->addMethodCall('addEventListener', array(
8290
array_unique($instance['event']),
8391
isset($instance['lazy']) && $instance['lazy'] ? $id : new Reference($id),

src/Symfony/Bridge/Doctrine/Tests/DependencyInjection/CompilerPass/RegisterEventListenersAndSubscribersPassTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterEventListenersAndSubscribersPass;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Definition;
1617

1718
class RegisterEventListenersAndSubscribersPassTest extends \PHPUnit_Framework_TestCase
1819
{
@@ -23,6 +24,38 @@ protected function setUp()
2324
}
2425
}
2526

27+
/**
28+
* @expectedException InvalidArgumentException
29+
*/
30+
public function testExceptionOnAbstractTaggedSubscriber()
31+
{
32+
$container = $this->createBuilder();
33+
34+
$abstractDefinition = new Definition('stdClass');
35+
$abstractDefinition->setAbstract(true);
36+
$abstractDefinition->addTag('doctrine.event_subscriber');
37+
38+
$container->setDefinition('a', $abstractDefinition);
39+
40+
$this->process($container);
41+
}
42+
43+
/**
44+
* @expectedException InvalidArgumentException
45+
*/
46+
public function testExceptionOnAbstractTaggedListener()
47+
{
48+
$container = $this->createBuilder();
49+
50+
$abstractDefinition = new Definition('stdClass');
51+
$abstractDefinition->setAbstract(true);
52+
$abstractDefinition->addTag('doctrine.event_listener', array('event' => 'test'));
53+
54+
$container->setDefinition('a', $abstractDefinition);
55+
56+
$this->process($container);
57+
}
58+
2659
public function testProcessEventListenersWithPriorities()
2760
{
2861
$container = $this->createBuilder();

0 commit comments

Comments
 (0)