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

Skip to content

Commit be7b7fe

Browse files
committed
feature #31334 [Messenger] Add clear Entity Manager middleware (Koc)
This PR was merged into the 4.4 branch. Discussion ---------- [Messenger] Add clear Entity Manager middleware | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #29662 | License | MIT | Doc PR | TBD General purpose of this middleware: * avoid memory leaks during messages handling * prevent unexpected side effects when entities that already stored in identity map not refreshed between messages Commits ------- 6e690a6 Add clear Entity Manager middleware (closes #29662)
2 parents 957a0b8 + 6e690a6 commit be7b7fe

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

src/Symfony/Bridge/Doctrine/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
4.4.0
5+
-----
6+
7+
* added `DoctrineClearEntityManagerMiddleware`
8+
9+
410
4.3.0
511
-----
612

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Messenger\Envelope;
16+
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
17+
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
18+
use Symfony\Component\Messenger\Middleware\StackInterface;
19+
20+
/**
21+
* Clears entity manager after calling all handlers.
22+
*
23+
* @author Konstantin Myakshin <[email protected]>
24+
*/
25+
class DoctrineClearEntityManagerMiddleware implements MiddlewareInterface
26+
{
27+
private $managerRegistry;
28+
private $entityManagerName;
29+
30+
public function __construct(ManagerRegistry $managerRegistry, string $entityManagerName = null)
31+
{
32+
$this->managerRegistry = $managerRegistry;
33+
$this->entityManagerName = $entityManagerName;
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function handle(Envelope $envelope, StackInterface $stack): Envelope
40+
{
41+
try {
42+
$entityManager = $this->managerRegistry->getManager($this->entityManagerName);
43+
} catch (\InvalidArgumentException $e) {
44+
throw new UnrecoverableMessageHandlingException($e->getMessage(), 0, $e);
45+
}
46+
47+
try {
48+
return $stack->next()->handle($envelope, $stack);
49+
} finally {
50+
$entityManager->clear();
51+
}
52+
}
53+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\DoctrineClearEntityManagerMiddleware;
17+
use Symfony\Component\Messenger\Envelope;
18+
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
19+
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;
20+
21+
class DoctrineClearEntityManagerMiddlewareTest extends MiddlewareTestCase
22+
{
23+
public function testMiddlewareClearEntityManager()
24+
{
25+
$entityManager = $this->createMock(EntityManagerInterface::class);
26+
$entityManager->expects($this->once())
27+
->method('clear');
28+
29+
$managerRegistry = $this->createMock(ManagerRegistry::class);
30+
$managerRegistry
31+
->method('getManager')
32+
->with('default')
33+
->willReturn($entityManager);
34+
35+
$middleware = new DoctrineClearEntityManagerMiddleware($managerRegistry, 'default');
36+
37+
$middleware->handle(new Envelope(new \stdClass()), $this->getStackMock());
38+
}
39+
40+
public function testInvalidEntityManagerThrowsException()
41+
{
42+
$managerRegistry = $this->createMock(ManagerRegistry::class);
43+
$managerRegistry
44+
->method('getManager')
45+
->with('unknown_manager')
46+
->will($this->throwException(new \InvalidArgumentException()));
47+
48+
$middleware = new DoctrineClearEntityManagerMiddleware($managerRegistry, 'unknown_manager');
49+
50+
$this->expectException(UnrecoverableMessageHandlingException::class);
51+
52+
$middleware->handle(new Envelope(new \stdClass()), $this->getStackMock(false));
53+
}
54+
}

0 commit comments

Comments
 (0)