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

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