Skip to content

Commit 6e690a6

Browse files
committed
Add clear Entity Manager middleware (closes #29662)
1 parent 693cbff commit 6e690a6

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 <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: 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 <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+
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)
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