Skip to content

Commit 1a25291

Browse files
committed
Add clear Entity Manager middleware (closes #29662)
1 parent e69551f commit 1a25291

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-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 <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 <molodchick@gmail.com>
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: 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