vendor/doctrine/doctrine-bundle/ConnectionFactory.php line 63

Open in your IDE?
  1. <?php
  2. namespace Doctrine\Bundle\DoctrineBundle;
  3. use Doctrine\Common\EventManager;
  4. use Doctrine\DBAL\Configuration;
  5. use Doctrine\DBAL\Connection;
  6. use Doctrine\DBAL\DBALException;
  7. use Doctrine\DBAL\DriverManager;
  8. use Doctrine\DBAL\Exception\DriverException;
  9. use Doctrine\DBAL\Platforms\AbstractPlatform;
  10. use Doctrine\DBAL\Types\Type;
  11. /**
  12.  * Connection
  13.  */
  14. class ConnectionFactory
  15. {
  16.     /** @var mixed[][] */
  17.     private $typesConfig = [];
  18.     /** @var string[] */
  19.     private $commentedTypes = [];
  20.     /** @var bool */
  21.     private $initialized false;
  22.     /**
  23.      * Construct.
  24.      *
  25.      * @param mixed[][] $typesConfig
  26.      */
  27.     public function __construct(array $typesConfig)
  28.     {
  29.         $this->typesConfig $typesConfig;
  30.     }
  31.     /**
  32.      * Create a connection by name.
  33.      *
  34.      * @param mixed[]         $params
  35.      * @param string[]|Type[] $mappingTypes
  36.      *
  37.      * @return Connection
  38.      */
  39.     public function createConnection(array $paramsConfiguration $config nullEventManager $eventManager null, array $mappingTypes = [])
  40.     {
  41.         if (! $this->initialized) {
  42.             $this->initializeTypes();
  43.         }
  44.         $connection DriverManager::getConnection($params$config$eventManager);
  45.         if (! empty($mappingTypes)) {
  46.             $platform $this->getDatabasePlatform($connection);
  47.             foreach ($mappingTypes as $dbType => $doctrineType) {
  48.                 $platform->registerDoctrineTypeMapping($dbType$doctrineType);
  49.             }
  50.         }
  51.         if (! empty($this->commentedTypes)) {
  52.             $platform $this->getDatabasePlatform($connection);
  53.             foreach ($this->commentedTypes as $type) {
  54.                 $platform->markDoctrineTypeCommented(Type::getType($type));
  55.             }
  56.         }
  57.         return $connection;
  58.     }
  59.     /**
  60.      * Try to get the database platform.
  61.      *
  62.      * This could fail if types should be registered to an predefined/unused connection
  63.      * and the platform version is unknown.
  64.      * For details have a look at DoctrineBundle issue #673.
  65.      *
  66.      * @return AbstractPlatform
  67.      *
  68.      * @throws DBALException
  69.      */
  70.     private function getDatabasePlatform(Connection $connection)
  71.     {
  72.         try {
  73.             return $connection->getDatabasePlatform();
  74.         } catch (DriverException $driverException) {
  75.             throw new DBALException(
  76.                 'An exception occured while establishing a connection to figure out your platform version.' PHP_EOL .
  77.                 "You can circumvent this by setting a 'server_version' configuration value" PHP_EOL PHP_EOL .
  78.                 'For further information have a look at:' PHP_EOL .
  79.                 'https://github.com/doctrine/DoctrineBundle/issues/673',
  80.                 0,
  81.                 $driverException
  82.             );
  83.         }
  84.     }
  85.     /**
  86.      * initialize the types
  87.      */
  88.     private function initializeTypes()
  89.     {
  90.         foreach ($this->typesConfig as $type => $typeConfig) {
  91.             if (Type::hasType($type)) {
  92.                 Type::overrideType($type$typeConfig['class']);
  93.             } else {
  94.                 Type::addType($type$typeConfig['class']);
  95.             }
  96.             if (! $typeConfig['commented']) {
  97.                 continue;
  98.             }
  99.             $this->commentedTypes[] = $type;
  100.         }
  101.         $this->initialized true;
  102.     }
  103. }