Skip to content

Commit 42ac426

Browse files
committed
Add clear Entity Manager middleware (closes #29662)
1 parent b2f5b8a commit 42ac426

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-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: 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+
}
Lines 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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy