Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 42ac426

Browse filesBrowse files
committed
Add clear Entity Manager middleware (closes #29662)
1 parent b2f5b8a commit 42ac426
Copy full SHA for 42ac426

File tree

3 files changed

+127
-0
lines changed
Filter options

3 files changed

+127
-0
lines changed

‎src/Symfony/Bridge/Doctrine/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/CHANGELOG.md
+6Lines 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

+55Lines changed: 55 additions & 0 deletions
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\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 <koc-dp@yandex.ru>
24+
*
25+
* @experimental in 4.4
26+
*/
27+
class DoctrineClearEntityManagerMiddleware implements MiddlewareInterface
28+
{
29+
private $managerRegistry;
30+
private $entityManagerName;
31+
32+
public function __construct(ManagerRegistry $managerRegistry, string $entityManagerName = null)
33+
{
34+
$this->managerRegistry = $managerRegistry;
35+
$this->entityManagerName = $entityManagerName;
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function handle(Envelope $envelope, StackInterface $stack): Envelope
42+
{
43+
try {
44+
$entityManager = $this->managerRegistry->getManager($this->entityManagerName);
45+
} catch (\InvalidArgumentException $e) {
46+
throw new UnrecoverableMessageHandlingException($e->getMessage(), 0, $e);
47+
}
48+
49+
try {
50+
return $stack->next()->handle($envelope, $stack);
51+
} finally {
52+
$entityManager->clear();
53+
}
54+
}
55+
}
+66Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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\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+
private $entityManager;
24+
private $managerRegistry;
25+
private $middleware;
26+
private $entityManagerName = 'default';
27+
28+
protected function setUp()
29+
{
30+
$this->entityManager = $this->createMock(EntityManagerInterface::class);
31+
32+
$this->managerRegistry = $this->createMock(ManagerRegistry::class);
33+
$this->managerRegistry
34+
->method('getManager')
35+
->with($this->entityManagerName)
36+
->willReturn($this->entityManager);
37+
38+
$this->middleware = new DoctrineClearEntityManagerMiddleware(
39+
$this->managerRegistry,
40+
$this->entityManagerName
41+
);
42+
}
43+
44+
public function testMiddlewareClearEntityManager()
45+
{
46+
$this->entityManager->expects($this->once())
47+
->method('clear');
48+
49+
$this->middleware->handle(new Envelope(new \stdClass()), $this->getStackMock());
50+
}
51+
52+
public function testInvalidEntityManagerThrowsException()
53+
{
54+
$managerRegistry = $this->createMock(ManagerRegistry::class);
55+
$managerRegistry
56+
->method('getManager')
57+
->with('unknown_manager')
58+
->will($this->throwException(new \InvalidArgumentException()));
59+
60+
$middleware = new DoctrineClearEntityManagerMiddleware($managerRegistry, 'unknown_manager');
61+
62+
$this->expectException(UnrecoverableMessageHandlingException::class);
63+
64+
$middleware->handle(new Envelope(new \stdClass()), $this->getStackMock(false));
65+
}
66+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.