Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DoctrineBridge] Fix circular loop with EntityManager #39799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions src/Symfony/Bridge/Doctrine/ContainerAwareEventManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Psr\Container\ContainerInterface;

/**
* Allows lazy loading of listener services.
* Allows lazy loading of listener and subscriber services.
*
* @author Johannes M. Schmitt <[email protected]>
*/
Expand All @@ -28,13 +28,16 @@ class ContainerAwareEventManager extends EventManager
* <event> => <listeners>
*/
private $listeners = [];
private $subscribers;
private $initialized = [];
private $initializedSubscribers = false;
private $methods = [];
private $container;

public function __construct(ContainerInterface $container)
public function __construct(ContainerInterface $container, array $subscriberIds = [])
{
$this->container = $container;
$this->subscribers = $subscriberIds;
}

/**
Expand All @@ -44,6 +47,9 @@ public function __construct(ContainerInterface $container)
*/
public function dispatchEvent($eventName, EventArgs $eventArgs = null)
{
if (!$this->initializedSubscribers) {
$this->initializeSubscribers();
}
if (!isset($this->listeners[$eventName])) {
return;
}
Expand All @@ -66,6 +72,9 @@ public function dispatchEvent($eventName, EventArgs $eventArgs = null)
*/
public function getListeners($event = null)
{
if (!$this->initializedSubscribers) {
$this->initializeSubscribers();
}
if (null !== $event) {
if (!isset($this->initialized[$event])) {
$this->initializeListeners($event);
Expand All @@ -90,6 +99,10 @@ public function getListeners($event = null)
*/
public function hasListeners($event)
{
if (!$this->initializedSubscribers) {
$this->initializeSubscribers();
}

return isset($this->listeners[$event]) && $this->listeners[$event];
}

Expand Down Expand Up @@ -138,14 +151,24 @@ public function removeEventListener($events, $listener)

private function initializeListeners(string $eventName)
{
$this->initialized[$eventName] = true;
foreach ($this->listeners[$eventName] as $hash => $listener) {
if (\is_string($listener)) {
$this->listeners[$eventName][$hash] = $listener = $this->container->get($listener);

$this->methods[$eventName][$hash] = $this->getMethod($listener, $eventName);
}
}
$this->initialized[$eventName] = true;
}

private function initializeSubscribers()
{
$this->initializedSubscribers = true;
foreach ($this->subscribers as $id => $subscriber) {
if (\is_string($subscriber)) {
parent::addEventSubscriber($this->subscribers[$id] = $this->container->get($subscriber));
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass;

use Symfony\Bridge\Doctrine\ContainerAwareEventManager;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -55,15 +57,24 @@ public function process(ContainerBuilder $container)
}

$this->connections = $container->getParameter($this->connections);
$this->addTaggedSubscribers($container);
$this->addTaggedListeners($container);
$listenerRefs = [];
$this->addTaggedSubscribers($container, $listenerRefs);
$this->addTaggedListeners($container, $listenerRefs);

// replace service container argument of event managers with smaller service locator
// so services can even remain private
foreach ($listenerRefs as $connection => $refs) {
$this->getEventManagerDef($container, $connection)
->replaceArgument(0, ServiceLocatorTagPass::register($container, $refs));
}
}

private function addTaggedSubscribers(ContainerBuilder $container)
private function addTaggedSubscribers(ContainerBuilder $container, array &$listenerRefs)
{
$subscriberTag = $this->tagPrefix.'.event_subscriber';
$taggedSubscribers = $this->findAndSortTags($subscriberTag, $container);

$managerDefs = [];
foreach ($taggedSubscribers as $taggedSubscriber) {
[$id, $tag] = $taggedSubscriber;
$connections = isset($tag['connection']) ? [$tag['connection']] : array_keys($this->connections);
Expand All @@ -72,16 +83,33 @@ private function addTaggedSubscribers(ContainerBuilder $container)
throw new RuntimeException(sprintf('The Doctrine connection "%s" referenced in service "%s" does not exist. Available connections names: "%s".', $con, $id, implode('", "', array_keys($this->connections))));
}

$this->getEventManagerDef($container, $con)->addMethodCall('addEventSubscriber', [new Reference($id)]);
if (!isset($managerDefs[$con])) {
$managerDef = $parentDef = $this->getEventManagerDef($container, $con);
while ($parentDef instanceof ChildDefinition) {
$parentDef = $container->findDefinition($parentDef->getParent());
}
$managerClass = $container->getParameterBag()->resolveValue($parentDef->getClass());
$managerDefs[$con] = [$managerDef, $managerClass];
} else {
[$managerDef, $managerClass] = $managerDefs[$con];
}

if (ContainerAwareEventManager::class === $managerClass) {
$listenerRefs[$con][$id] = new Reference($id);
$refs = $managerDef->getArguments()[1] ?? [];
$refs[] = $id;
$managerDef->setArgument(1, $refs);
} else {
$managerDef->addMethodCall('addEventSubscriber', [new Reference($id)]);
}
}
}
}

private function addTaggedListeners(ContainerBuilder $container)
private function addTaggedListeners(ContainerBuilder $container, array &$listenerRefs)
{
$listenerTag = $this->tagPrefix.'.event_listener';
$taggedListeners = $this->findAndSortTags($listenerTag, $container);
$listenerRefs = [];

foreach ($taggedListeners as $taggedListener) {
[$id, $tag] = $taggedListener;
Expand All @@ -100,13 +128,6 @@ private function addTaggedListeners(ContainerBuilder $container)
$this->getEventManagerDef($container, $con)->addMethodCall('addEventListener', [[$tag['event']], $id]);
}
}

// replace service container argument of event managers with smaller service locator
// so services can even remain private
foreach ($listenerRefs as $connection => $refs) {
$this->getEventManagerDef($container, $connection)
->replaceArgument(0, ServiceLocatorTagPass::register($container, $refs));
}
}

private function getEventManagerDef(ContainerBuilder $container, string $name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bridge\Doctrine\Tests;

use Doctrine\Common\EventSubscriber;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\ContainerAwareEventManager;
use Symfony\Component\DependencyInjection\Container;
Expand All @@ -28,6 +29,8 @@ protected function setUp(): void

public function testDispatchEvent()
{
$this->evm = new ContainerAwareEventManager($this->container, ['lazy4']);

$this->container->set('lazy1', $listener1 = new MyListener());
$this->evm->addEventListener('foo', 'lazy1');
$this->evm->addEventListener('foo', $listener2 = new MyListener());
Expand All @@ -37,10 +40,18 @@ public function testDispatchEvent()
$this->container->set('lazy3', $listener5 = new MyListener());
$this->evm->addEventListener('foo', $listener5 = new MyListener());
$this->evm->addEventListener('bar', $listener5);
$this->container->set('lazy4', $subscriber1 = new MySubscriber(['foo']));
$this->evm->addEventSubscriber($subscriber2 = new MySubscriber(['bar']));

$this->assertSame(0, $subscriber1->calledSubscribedEventsCount);
$this->assertSame(1, $subscriber2->calledSubscribedEventsCount);

$this->evm->dispatchEvent('foo');
$this->evm->dispatchEvent('bar');

$this->assertSame(1, $subscriber1->calledSubscribedEventsCount);
$this->assertSame(1, $subscriber2->calledSubscribedEventsCount);

$this->assertSame(0, $listener1->calledByInvokeCount);
$this->assertSame(1, $listener1->calledByEventNameCount);
$this->assertSame(0, $listener2->calledByInvokeCount);
Expand All @@ -51,10 +62,16 @@ public function testDispatchEvent()
$this->assertSame(0, $listener4->calledByEventNameCount);
$this->assertSame(1, $listener5->calledByInvokeCount);
$this->assertSame(1, $listener5->calledByEventNameCount);
$this->assertSame(0, $subscriber1->calledByInvokeCount);
$this->assertSame(1, $subscriber1->calledByEventNameCount);
$this->assertSame(1, $subscriber2->calledByInvokeCount);
$this->assertSame(0, $subscriber2->calledByEventNameCount);
}

public function testAddEventListenerAfterDispatchEvent()
public function testAddEventListenerAndSubscriberAfterDispatchEvent()
{
$this->evm = new ContainerAwareEventManager($this->container, ['lazy7']);

$this->container->set('lazy1', $listener1 = new MyListener());
$this->evm->addEventListener('foo', 'lazy1');
$this->evm->addEventListener('foo', $listener2 = new MyListener());
Expand All @@ -64,10 +81,18 @@ public function testAddEventListenerAfterDispatchEvent()
$this->container->set('lazy3', $listener5 = new MyListener());
$this->evm->addEventListener('foo', $listener5 = new MyListener());
$this->evm->addEventListener('bar', $listener5);
$this->container->set('lazy7', $subscriber1 = new MySubscriber(['foo']));
$this->evm->addEventSubscriber($subscriber2 = new MySubscriber(['bar']));

$this->assertSame(0, $subscriber1->calledSubscribedEventsCount);
$this->assertSame(1, $subscriber2->calledSubscribedEventsCount);

$this->evm->dispatchEvent('foo');
$this->evm->dispatchEvent('bar');

$this->assertSame(1, $subscriber1->calledSubscribedEventsCount);
$this->assertSame(1, $subscriber2->calledSubscribedEventsCount);

$this->container->set('lazy4', $listener6 = new MyListener());
$this->evm->addEventListener('foo', 'lazy4');
$this->evm->addEventListener('foo', $listener7 = new MyListener());
Expand All @@ -77,10 +102,19 @@ public function testAddEventListenerAfterDispatchEvent()
$this->container->set('lazy6', $listener10 = new MyListener());
$this->evm->addEventListener('foo', $listener10 = new MyListener());
$this->evm->addEventListener('bar', $listener10);
$this->evm->addEventSubscriber($subscriber3 = new MySubscriber(['bar']));

$this->assertSame(1, $subscriber1->calledSubscribedEventsCount);
$this->assertSame(1, $subscriber2->calledSubscribedEventsCount);
$this->assertSame(1, $subscriber3->calledSubscribedEventsCount);

$this->evm->dispatchEvent('foo');
$this->evm->dispatchEvent('bar');

$this->assertSame(1, $subscriber1->calledSubscribedEventsCount);
$this->assertSame(1, $subscriber2->calledSubscribedEventsCount);
$this->assertSame(1, $subscriber3->calledSubscribedEventsCount);

$this->assertSame(0, $listener1->calledByInvokeCount);
$this->assertSame(2, $listener1->calledByEventNameCount);
$this->assertSame(0, $listener2->calledByInvokeCount);
Expand All @@ -91,6 +125,10 @@ public function testAddEventListenerAfterDispatchEvent()
$this->assertSame(0, $listener4->calledByEventNameCount);
$this->assertSame(2, $listener5->calledByInvokeCount);
$this->assertSame(2, $listener5->calledByEventNameCount);
$this->assertSame(0, $subscriber1->calledByInvokeCount);
$this->assertSame(2, $subscriber1->calledByEventNameCount);
$this->assertSame(2, $subscriber2->calledByInvokeCount);
$this->assertSame(0, $subscriber2->calledByEventNameCount);

$this->assertSame(0, $listener6->calledByInvokeCount);
$this->assertSame(1, $listener6->calledByEventNameCount);
Expand All @@ -102,6 +140,8 @@ public function testAddEventListenerAfterDispatchEvent()
$this->assertSame(0, $listener9->calledByEventNameCount);
$this->assertSame(1, $listener10->calledByInvokeCount);
$this->assertSame(1, $listener10->calledByEventNameCount);
$this->assertSame(1, $subscriber3->calledByInvokeCount);
$this->assertSame(0, $subscriber3->calledByEventNameCount);
}

public function testGetListenersForEvent()
Expand Down Expand Up @@ -166,3 +206,21 @@ public function foo()
++$this->calledByEventNameCount;
}
}

class MySubscriber extends MyListener implements EventSubscriber
{
public $calledSubscribedEventsCount = 0;
private $listenedEvents;

public function __construct(array $listenedEvents)
{
$this->listenedEvents = $listenedEvents;
}

public function getSubscribedEvents(): array
{
++$this->calledSubscribedEventsCount;

return $this->listenedEvents;
}
}
Loading