src/EventSubscriber/DatabaseSwitcherSubscriber.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Doctrine\DBAL\Connection;
  4. use Doctrine\Persistence\ManagerRegistry;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. class DatabaseSwitcherSubscriber implements EventSubscriberInterface
  8. {
  9.     /**
  10.      * @var ManagerRegistry
  11.      */
  12.     protected $doctrine;
  13.     /**
  14.      * DatabaseSwitcherSubscriber constructor.
  15.      *
  16.      * @param ManagerRegistry $registry
  17.      */
  18.     public function __construct(ManagerRegistry $registry)
  19.     {
  20.         $this->doctrine $registry;
  21.     }
  22.     /**
  23.      * @return array
  24.      */
  25.     public static function getSubscribedEvents()
  26.     {
  27.         return [
  28.             KernelEvents::REQUEST => [
  29.                 ['onKernelRequest'1000],
  30.             ],
  31.         ];
  32.     }
  33.     /**
  34.      * @throws \Doctrine\DBAL\DBALException
  35.      */
  36.     public function onKernelRequest()
  37.     {
  38.         $testMode getenv('TEST_MODE');
  39.         if ($testMode) {
  40.             /** @var Connection $connection */
  41.             $connection $this->doctrine->getConnection();
  42.             /** @var Connection $testConnection */
  43.             $testConnection $this->doctrine->getConnection('test');
  44.             if (!$connection->isConnected()) {
  45.                 $connection->__construct(
  46.                     $testConnection->getParams(),
  47.                     $testConnection->getDriver(),
  48.                     $testConnection->getConfiguration(),
  49.                     $testConnection->getEventManager()
  50.                 );
  51.                 $connection->connect();
  52.             }
  53.         }
  54.     }
  55. }