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

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