diff --git a/src/Symfony/Bridge/Doctrine/CHANGELOG.md b/src/Symfony/Bridge/Doctrine/CHANGELOG.md index b9baff3763..7123b527eb 100644 --- a/src/Symfony/Bridge/Doctrine/CHANGELOG.md +++ b/src/Symfony/Bridge/Doctrine/CHANGELOG.md @@ -1,6 +1,12 @@ CHANGELOG ========= +4.4.0 +----- + + * added `DoctrineClearEntityManagerMiddleware` + + 4.3.0 ----- diff --git a/src/Symfony/Bridge/Doctrine/Messenger/DoctrineClearEntityManagerMiddleware.php b/src/Symfony/Bridge/Doctrine/Messenger/DoctrineClearEntityManagerMiddleware.php new file mode 100644 index 0000000000..2bae26c15a --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Messenger/DoctrineClearEntityManagerMiddleware.php @@ -0,0 +1,53 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Messenger; + +use Doctrine\Common\Persistence\ManagerRegistry; +use Symfony\Component\Messenger\Envelope; +use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException; +use Symfony\Component\Messenger\Middleware\MiddlewareInterface; +use Symfony\Component\Messenger\Middleware\StackInterface; + +/** + * Clears entity manager after calling all handlers. + * + * @author Konstantin Myakshin + */ +class DoctrineClearEntityManagerMiddleware implements MiddlewareInterface +{ + private $managerRegistry; + private $entityManagerName; + + public function __construct(ManagerRegistry $managerRegistry, string $entityManagerName = null) + { + $this->managerRegistry = $managerRegistry; + $this->entityManagerName = $entityManagerName; + } + + /** + * {@inheritdoc} + */ + public function handle(Envelope $envelope, StackInterface $stack): Envelope + { + try { + $entityManager = $this->managerRegistry->getManager($this->entityManagerName); + } catch (\InvalidArgumentException $e) { + throw new UnrecoverableMessageHandlingException($e->getMessage(), 0, $e); + } + + try { + return $stack->next()->handle($envelope, $stack); + } finally { + $entityManager->clear(); + } + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Messenger/DoctrineClearEntityManagerMiddlewareTest.php b/src/Symfony/Bridge/Doctrine/Tests/Messenger/DoctrineClearEntityManagerMiddlewareTest.php new file mode 100644 index 0000000000..d20c9cfb50 --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/Messenger/DoctrineClearEntityManagerMiddlewareTest.php @@ -0,0 +1,54 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\Messenger; + +use Doctrine\Common\Persistence\ManagerRegistry; +use Doctrine\ORM\EntityManagerInterface; +use Symfony\Bridge\Doctrine\Messenger\DoctrineClearEntityManagerMiddleware; +use Symfony\Component\Messenger\Envelope; +use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException; +use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase; + +class DoctrineClearEntityManagerMiddlewareTest extends MiddlewareTestCase +{ + public function testMiddlewareClearEntityManager() + { + $entityManager = $this->createMock(EntityManagerInterface::class); + $entityManager->expects($this->once()) + ->method('clear'); + + $managerRegistry = $this->createMock(ManagerRegistry::class); + $managerRegistry + ->method('getManager') + ->with('default') + ->willReturn($entityManager); + + $middleware = new DoctrineClearEntityManagerMiddleware($managerRegistry, 'default'); + + $middleware->handle(new Envelope(new \stdClass()), $this->getStackMock()); + } + + public function testInvalidEntityManagerThrowsException() + { + $managerRegistry = $this->createMock(ManagerRegistry::class); + $managerRegistry + ->method('getManager') + ->with('unknown_manager') + ->will($this->throwException(new \InvalidArgumentException())); + + $middleware = new DoctrineClearEntityManagerMiddleware($managerRegistry, 'unknown_manager'); + + $this->expectException(UnrecoverableMessageHandlingException::class); + + $middleware->handle(new Envelope(new \stdClass()), $this->getStackMock(false)); + } +}