8000 Adding DoctrineClearEntityManagerWorkerSubscriber to reset entity man… · symfony/symfony@e7b9888 · GitHub
[go: up one dir, main page]

Skip to content

Commit e7b9888

Browse files
committed
Adding DoctrineClearEntityManagerWorkerSubscriber to reset entity manager in worker
1 parent cf10c02 commit e7b9888

File tree

5 files changed

+96
-113
lines changed

5 files changed

+96
-113
lines changed

src/Symfony/Bridge/Doctrine/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ CHANGELOG
44
4.4.0
55
-----
66

7-
* added `DoctrineClearEntityManagerMiddleware`
7+
* added `DoctrineClearEntityManagerWorkerSubscriber`
88
* deprecated `RegistryInterface`, use `Doctrine\Common\Persistence\ManagerRegistry`
99
* added support for invokable event listeners
1010
* added `getMetadataDriverClass` method to deprecate class parameters in service configuration files

src/Symfony/Bridge/Doctrine/Messenger/DoctrineClearEntityManagerMiddleware.php

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 55 additions & 0 deletions
8000
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Messenger;
13+
14+
use Doctrine\Common\Persistence\ManagerRegistry;
15+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16+
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
17+
use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent;
18+
19+
/**
20+
* Clears entity managers between messages being handled to avoid outdated data.
21+
*
22+
* @author Ryan Weaver <ryan@symfonycasts.com>
23+
*/
24+
class DoctrineClearEntityManagerWorkerSubscriber implements EventSubscriberInterface
25+
{
26+
private $managerRegistry;
27+
28+
public function __construct(ManagerRegistry $managerRegistry)
29+
{
30+
$this->managerRegistry = $managerRegistry;
31+
}
32+
33+
public function onWorkerMessageHandled()
34+
{
35+
$this->clearEntityManagers();
36+
}
37+
38+
public function onWorkerMessageFailed()
39+
{
40+
$this->clearEntityManagers();
41+
}
42+
43+
public static function getSubscribedEvents()
44+
{
45+
yield WorkerMessageHandledEvent::class => 'onWorkerMessageHandled';
46+
yield WorkerMessageFailedEvent::class => 'onWorkerMessageFailed';
47+
}
48+
49+
private function clearEntityManagers()
50+
{
51+
foreach ($this->managerRegistry->getManagers() as $manager) {
52+
$manager->clear();
53+
}
54+
}
55+
}

src/Symfony/Bridge/Doctrine/Tests/Messenger/DoctrineClearEntityManagerMiddlewareTest.php

Lines changed: 0 additions & 76 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\Messenger;
13+
14+
use Doctrine\Common\Persistence\ManagerRegistry;
15+
use Doctrine\ORM\EntityManagerInterface;
16+
use Symfony\Bridge\Doctrine\Messenger\DoctrineClearEntityManagerWorkerSubscriber;
17+
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;
18+
19+
class DoctrineClearEntityManagerWorkerSubscriberTest extends MiddlewareTestCase
20+
{
21+
public function testMiddlewareClearEntityManager()
22+
{
23+
$entityManager1 = $this->createMock(EntityManagerInterface::class);
24+
$entityManager1->expects($this->once())
25+
->method('clear');
26+
27+
$entityManager2 = $this->createMock(EntityManagerInterface::class);
28+
$entityManager2->expects($this->once())
29+
->method('clear');
30+
31+
$managerRegistry = $this->createMock(ManagerRegistry::class);
32+
$managerRegistry
33+
->method('getManagers')
34+
->with()
35+
->willReturn([$entityManager1, $entityManager2]);
36+
37+
$subscriber = new DoctrineClearEntityManagerWorkerSubscriber($managerRegistry);
38+
$subscriber->onWorkerMessageHandled();
39+
}
40+
}

0 commit comments

Comments
 (0)
0