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

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\DriverManager;
  7. use Doctrine\DBAL\Exception as DBALException;
  8. use Doctrine\DBAL\Exception\DriverException;
  9. use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
  10. use Doctrine\DBAL\Platforms\AbstractPlatform;
  11. use Doctrine\DBAL\Platforms\MySqlPlatform;
  12. use Doctrine\DBAL\Types\Type;
  13. use function array_merge;
  14. use function defined;
  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.                     throw DBALException::invalidWrapperClass($params['wrapperClass']);
  55.                 }
  56.                 $wrapperClass           $params['wrapperClass'];
  57.                 $params['wrapperClass'] = null;
  58.             }
  59.             $connection DriverManager::getConnection($params$config$eventManager);
  60.             $params     $this->addDatabaseSuffix(array_merge($connection->getParams(), $overriddenOptions));
  61.             $driver     $connection->getDriver();
  62.             $platform   $driver->getDatabasePlatform();
  63.             if (! isset($params['charset'])) {
  64.                 /** @psalm-suppress UndefinedClass AbstractMySQLPlatform exists since DBAL 3.x only */
  65.                 if ($platform instanceof AbstractMySQLPlatform || $platform instanceof MySqlPlatform) {
  66.                     $params['charset'] = 'utf8mb4';
  67.                     /* PARAM_ASCII_STR_ARRAY is defined since doctrine/dbal 3.3
  68.                        doctrine/dbal 3.3.2 adds support for the option "collation"
  69.                        Checking for that constant will no longer be necessary
  70.                        after dropping support for doctrine/dbal 2, since this
  71.                        package requires doctrine/dbal 3.3.2 or higher. */
  72.                     if (isset($params['defaultTableOptions']['collate']) && defined('Doctrine\DBAL\Connection::PARAM_ASCII_STR_ARRAY')) {
  73.                         $params['defaultTableOptions']['collation'] = $params['defaultTableOptions']['collate'];
  74.                         unset($params['defaultTableOptions']['collate']);
  75.                     }
  76.                     $collationOption defined('Doctrine\DBAL\Connection::PARAM_ASCII_STR_ARRAY') ? 'collation' 'collate';
  77.                     if (! isset($params['defaultTableOptions'][$collationOption])) {
  78.                         $params['defaultTableOptions'][$collationOption] = 'utf8mb4_unicode_ci';
  79.                     }
  80.                 } else {
  81.                     $params['charset'] = 'utf8';
  82.                 }
  83.             }
  84.             if ($wrapperClass !== null) {
  85.                 $params['wrapperClass'] = $wrapperClass;
  86.             } else {
  87.                 $wrapperClass Connection::class;
  88.             }
  89.             $connection = new $wrapperClass($params$driver$config$eventManager);
  90.         } else {
  91.             $connection DriverManager::getConnection($params$config$eventManager);
  92.         }
  93.         if (! empty($mappingTypes)) {
  94.             $platform $this->getDatabasePlatform($connection);
  95.             foreach ($mappingTypes as $dbType => $doctrineType) {
  96.                 $platform->registerDoctrineTypeMapping($dbType$doctrineType);
  97.             }
  98.         }
  99.         return $connection;
  100.     }
  101.     /**
  102.      * Try to get the database platform.
  103.      *
  104.      * This could fail if types should be registered to an predefined/unused connection
  105.      * and the platform version is unknown.
  106.      * For details have a look at DoctrineBundle issue #673.
  107.      *
  108.      * @throws DBALException
  109.      */
  110.     private function getDatabasePlatform(Connection $connection): AbstractPlatform
  111.     {
  112.         try {
  113.             return $connection->getDatabasePlatform();
  114.         } catch (DriverException $driverException) {
  115.             throw new DBALException(
  116.                 'An exception occurred while establishing a connection to figure out your platform version.' PHP_EOL .
  117.                 "You can circumvent this by setting a 'server_version' configuration value" PHP_EOL PHP_EOL .
  118.                 'For further information have a look at:' PHP_EOL .
  119.                 'https://github.com/doctrine/DoctrineBundle/issues/673',
  120.                 0,
  121.                 $driverException
  122.             );
  123.         }
  124.     }
  125.     /**
  126.      * initialize the types
  127.      */
  128.     private function initializeTypes(): void
  129.     {
  130.         foreach ($this->typesConfig as $typeName => $typeConfig) {
  131.             if (Type::hasType($typeName)) {
  132.                 Type::overrideType($typeName$typeConfig['class']);
  133.             } else {
  134.                 Type::addType($typeName$typeConfig['class']);
  135.             }
  136.         }
  137.         $this->initialized true;
  138.     }
  139.     /**
  140.      * @param array<string, mixed> $params
  141.      *
  142.      * @return array<string, mixed>
  143.      */
  144.     private function addDatabaseSuffix(array $params): array
  145.     {
  146.         if (isset($params['dbname']) && isset($params['dbname_suffix'])) {
  147.             $params['dbname'] .= $params['dbname_suffix'];
  148.         }
  149.         foreach ($params['replica'] ?? [] as $key => $replicaParams) {
  150.             if (! isset($replicaParams['dbname'], $replicaParams['dbname_suffix'])) {
  151.                 continue;
  152.             }
  153.             $params['replica'][$key]['dbname'] .= $replicaParams['dbname_suffix'];
  154.         }
  155.         if (isset($params['primary']['dbname'], $params['primary']['dbname_suffix'])) {
  156.             $params['primary']['dbname'] .= $params['primary']['dbname_suffix'];
  157.         }
  158.         return $params;
  159.     }
  160. }