vendor/dama/doctrine-test-bundle/src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticDriver.php line 37

Open in your IDE?
  1. <?php
  2. namespace DAMA\DoctrineTestBundle\Doctrine\DBAL;
  3. use Doctrine\DBAL\Driver;
  4. use Doctrine\DBAL\Driver\API\ExceptionConverter;
  5. use Doctrine\DBAL\Driver\Connection;
  6. use Doctrine\DBAL\Driver\Connection as DriverConnection;
  7. use Doctrine\DBAL\Platforms\AbstractPlatform;
  8. use Doctrine\DBAL\Schema\AbstractSchemaManager;
  9. class StaticDriver implements Driver
  10. {
  11.     /**
  12.      * @var Connection[]
  13.      */
  14.     protected static $connections = [];
  15.     /**
  16.      * @var bool
  17.      */
  18.     protected static $keepStaticConnections false;
  19.     /**
  20.      * @var Driver
  21.      */
  22.     protected $underlyingDriver;
  23.     public function __construct(Driver $underlyingDriver)
  24.     {
  25.         $this->underlyingDriver $underlyingDriver;
  26.     }
  27.     public function connect(array $params): DriverConnection
  28.     {
  29.         if (!self::$keepStaticConnections) {
  30.             return $this->underlyingDriver->connect($params);
  31.         }
  32.         $key sha1(json_encode($params));
  33.         if (!isset(self::$connections[$key])) {
  34.             self::$connections[$key] = $this->underlyingDriver->connect($params);
  35.             self::$connections[$key]->beginTransaction();
  36.         }
  37.         return new StaticConnection(self::$connections[$key]);
  38.     }
  39.     public function getSchemaManager(\Doctrine\DBAL\Connection $connAbstractPlatform $platform): AbstractSchemaManager
  40.     {
  41.         return $this->underlyingDriver->getSchemaManager($conn$platform);
  42.     }
  43.     public function getExceptionConverter(): ExceptionConverter
  44.     {
  45.         return $this->underlyingDriver->getExceptionConverter();
  46.     }
  47.     public function getDatabasePlatform(): AbstractPlatform
  48.     {
  49.         return $this->underlyingDriver->getDatabasePlatform();
  50.     }
  51.     public static function setKeepStaticConnections(bool $keepStaticConnections): void
  52.     {
  53.         self::$keepStaticConnections $keepStaticConnections;
  54.     }
  55.     public static function isKeepStaticConnections(): bool
  56.     {
  57.         return self::$keepStaticConnections;
  58.     }
  59.     public static function beginTransaction(): void
  60.     {
  61.         foreach (self::$connections as $con) {
  62.             $con->beginTransaction();
  63.         }
  64.     }
  65.     public static function rollBack(): void
  66.     {
  67.         foreach (self::$connections as $con) {
  68.             $con->rollBack();
  69.         }
  70.     }
  71.     public static function commit(): void
  72.     {
  73.         foreach (self::$connections as $con) {
  74.             $con->commit();
  75.         }
  76.     }
  77. }