Skip to content

Commit

Permalink
Add clear Entity Manager middleware (closes symfony#29662)
Browse files Browse the repository at this point in the history
  • Loading branch information
Koc committed May 7, 2019
1 parent b2f5b8a commit d06af69
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Symfony/Bridge/Doctrine/CHANGELOG.md
@@ -1,6 +1,12 @@
CHANGELOG
=========

4.4.0
-----

* added `DoctrineClearEntityManagerMiddleware`


4.3.0
-----

Expand Down
@@ -0,0 +1,56 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 Doctrine\ORM\EntityManagerInterface;
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 Konstanton Myakshin <koc-dp@yandex.ru>
*
* @experimental in 4.4
*/
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
{
$entityManager = $this->managerRegistry->getManager($this->entityManagerName);

if (!$entityManager instanceof EntityManagerInterface) {
throw new UnrecoverableMessageHandlingException(sprintf('The ObjectManager with name "%s" must be an instance of EntityManagerInterface', $this->entityManagerName));
}

try {
return $stack->next()->handle($envelope, $stack);
} finally {
$entityManager->clear();
}
}
}
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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\Test\Middleware\MiddlewareTestCase;

class DoctrineClearEntityManagerMiddlewareTest extends MiddlewareTestCase
{
private $entityManager;
private $managerRegistry;
private $middleware;
private $entityManagerName = 'default';

protected function setUp()
{
$this->entityManager = $this->createMock(EntityManagerInterface::class);

$this->managerRegistry = $this->createMock(ManagerRegistry::class);
$this->managerRegistry
->method('getManager')
->with($this->entityManagerName)
->willReturn($this->entityManager);

$this->middleware = new DoctrineClearEntityManagerMiddleware(
$this->managerRegistry,
$this->entityManagerName
);
}

public function testMiddlewareClearEntityManager()
{
$this->entityManager->expects($this->once())
->method('clear');

$this->middleware->handle(new Envelope(new \stdClass()), $this->getStackMock());
}
}

0 comments on commit d06af69

Please sign in to comment.