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

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