Extract Abstract Doctrine Middleware
This commit is contained in:
parent
a2b79e15e1
commit
184ce667d3
@ -0,0 +1,49 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* @author Konstantin Myakshin <molodchick@gmail.com>
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
abstract class AbstractDoctrineMiddleware implements MiddlewareInterface
|
||||
{
|
||||
protected $managerRegistry;
|
||||
protected $entityManagerName;
|
||||
|
||||
public function __construct(ManagerRegistry $managerRegistry, string $entityManagerName = null)
|
||||
{
|
||||
$this->managerRegistry = $managerRegistry;
|
||||
$this->entityManagerName = $entityManagerName;
|
||||
}
|
||||
|
||||
final 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);
|
||||
}
|
||||
|
||||
return $this->handleForManager($entityManager, $envelope, $stack);
|
||||
}
|
||||
|
||||
abstract protected function handleForManager(EntityManagerInterface $entityManager, Envelope $envelope, StackInterface $stack): Envelope;
|
||||
}
|
@ -11,10 +11,8 @@
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
@ -22,28 +20,10 @@ use Symfony\Component\Messenger\Middleware\StackInterface;
|
||||
*
|
||||
* @author Konstantin Myakshin <molodchick@gmail.com>
|
||||
*/
|
||||
class DoctrineClearEntityManagerMiddleware implements MiddlewareInterface
|
||||
class DoctrineClearEntityManagerMiddleware extends AbstractDoctrineMiddleware
|
||||
{
|
||||
private $managerRegistry;
|
||||
private $entityManagerName;
|
||||
|
||||
public function __construct(ManagerRegistry $managerRegistry, string $entityManagerName = null)
|
||||
protected function handleForManager(EntityManagerInterface $entityManager, Envelope $envelope, StackInterface $stack): Envelope
|
||||
{
|
||||
$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 {
|
||||
|
@ -11,10 +11,8 @@
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
@ -24,28 +22,10 @@ use Symfony\Component\Messenger\Middleware\StackInterface;
|
||||
*
|
||||
* @experimental in 4.3
|
||||
*/
|
||||
class DoctrineCloseConnectionMiddleware implements MiddlewareInterface
|
||||
class DoctrineCloseConnectionMiddleware extends AbstractDoctrineMiddleware
|
||||
{
|
||||
private $managerRegistry;
|
||||
private $entityManagerName;
|
||||
|
||||
public function __construct(ManagerRegistry $managerRegistry, string $entityManagerName = null)
|
||||
protected function handleForManager(EntityManagerInterface $entityManager, Envelope $envelope, StackInterface $stack): Envelope
|
||||
{
|
||||
$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 {
|
||||
$connection = $entityManager->getConnection();
|
||||
|
||||
|
@ -11,10 +11,8 @@
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
@ -24,28 +22,10 @@ use Symfony\Component\Messenger\Middleware\StackInterface;
|
||||
*
|
||||
* @experimental in 4.3
|
||||
*/
|
||||
class DoctrinePingConnectionMiddleware implements MiddlewareInterface
|
||||
class DoctrinePingConnectionMiddleware extends AbstractDoctrineMiddleware
|
||||
{
|
||||
private $managerRegistry;
|
||||
private $entityManagerName;
|
||||
|
||||
public function __construct(ManagerRegistry $managerRegistry, string $entityManagerName = null)
|
||||
protected function handleForManager(EntityManagerInterface $entityManager, Envelope $envelope, StackInterface $stack): Envelope
|
||||
{
|
||||
$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);
|
||||
}
|
||||
|
||||
$connection = $entityManager->getConnection();
|
||||
|
||||
if (!$connection->ping()) {
|
||||
|
@ -11,10 +11,8 @@
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
@ -24,28 +22,10 @@ use Symfony\Component\Messenger\Middleware\StackInterface;
|
||||
*
|
||||
* @experimental in 4.3
|
||||
*/
|
||||
class DoctrineTransactionMiddleware implements MiddlewareInterface
|
||||
class DoctrineTransactionMiddleware extends AbstractDoctrineMiddleware
|
||||
{
|
||||
private $managerRegistry;
|
||||
private $entityManagerName;
|
||||
|
||||
public function __construct(ManagerRegistry $managerRegistry, string $entityManagerName = null)
|
||||
protected function handleForManager(EntityManagerInterface $entityManager, Envelope $envelope, StackInterface $stack): Envelope
|
||||
{
|
||||
$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);
|
||||
}
|
||||
|
||||
$entityManager->getConnection()->beginTransaction();
|
||||
try {
|
||||
$envelope = $stack->next()->handle($envelope, $stack);
|
||||
|
Reference in New Issue
Block a user