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

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 Doctrine\Deprecations\Deprecation;
  14. use function array_merge;
  15. use function defined;
  16. use function is_subclass_of;
  17. use function trigger_deprecation;
  18. use const PHP_EOL;
  19. /** @psalm-import-type Params from DriverManager */
  20. class ConnectionFactory
  21. {
  22.     /** @var mixed[][] */
  23.     private $typesConfig = [];
  24.     /** @var bool */
  25.     private $initialized false;
  26.     /** @param mixed[][] $typesConfig */
  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 array<string, string> $mappingTypes
  36.      * @psalm-param Params $params
  37.      *
  38.      * @return Connection
  39.      */
  40.     public function createConnection(array $params, ?Configuration $config null, ?EventManager $eventManager null, array $mappingTypes = [])
  41.     {
  42.         if (! $this->initialized) {
  43.             $this->initializeTypes();
  44.         }
  45.         $overriddenOptions = [];
  46.         if (isset($params['connection_override_options'])) {
  47.             trigger_deprecation('doctrine/doctrine-bundle''2.4''The "connection_override_options" connection parameter is deprecated');
  48.             $overriddenOptions $params['connection_override_options'];
  49.             unset($params['connection_override_options']);
  50.         }
  51.         if (! isset($params['pdo']) && (! isset($params['charset']) || $overriddenOptions || isset($params['dbname_suffix']))) {
  52.             $wrapperClass null;
  53.             if (isset($params['wrapperClass'])) {
  54.                 if (! is_subclass_of($params['wrapperClass'], Connection::class)) {
  55.                     throw DBALException::invalidWrapperClass($params['wrapperClass']);
  56.                 }
  57.                 $wrapperClass           $params['wrapperClass'];
  58.                 $params['wrapperClass'] = null;
  59.             }
  60.             $connection DriverManager::getConnection($params$config$eventManager);
  61.             $params     $this->addDatabaseSuffix(array_merge($connection->getParams(), $overriddenOptions));
  62.             $driver     $connection->getDriver();
  63.             $platform   $driver->getDatabasePlatform();
  64.             if (! isset($params['charset'])) {
  65.                 /** @psalm-suppress UndefinedClass AbstractMySQLPlatform exists since DBAL 3.x only */
  66.                 if ($platform instanceof AbstractMySQLPlatform || $platform instanceof MySqlPlatform) {
  67.                     $params['charset'] = 'utf8mb4';
  68.                     /* PARAM_ASCII_STR_ARRAY is defined since doctrine/dbal 3.3
  69.                        doctrine/dbal 3.3.2 adds support for the option "collation"
  70.                        Checking for that constant will no longer be necessary
  71.                        after dropping support for doctrine/dbal 2, since this
  72.                        package requires doctrine/dbal 3.3.2 or higher. */
  73.                     if (isset($params['defaultTableOptions']['collate']) && defined('Doctrine\DBAL\Connection::PARAM_ASCII_STR_ARRAY')) {
  74.                         Deprecation::trigger(
  75.                             'doctrine/doctrine-bundle',
  76.                             'https://github.com/doctrine/dbal/issues/5214',
  77.                             'The "collate" default table option is deprecated in favor of "collation" and will be removed in doctrine/doctrine-bundle 3.0. '
  78.                         );
  79.                         $params['defaultTableOptions']['collation'] = $params['defaultTableOptions']['collate'];
  80.                         unset($params['defaultTableOptions']['collate']);
  81.                     }
  82.                     $collationOption defined('Doctrine\DBAL\Connection::PARAM_ASCII_STR_ARRAY') ? 'collation' 'collate';
  83.                     if (! isset($params['defaultTableOptions'][$collationOption])) {
  84.                         $params['defaultTableOptions'][$collationOption] = 'utf8mb4_unicode_ci';
  85.                     }
  86.                 } else {
  87.                     $params['charset'] = 'utf8';
  88.                 }
  89.             }
  90.             if ($wrapperClass !== null) {
  91.                 $params['wrapperClass'] = $wrapperClass;
  92.             } else {
  93.                 $wrapperClass Connection::class;
  94.             }
  95.             $connection = new $wrapperClass($params$driver$config$eventManager);
  96.         } else {
  97.             $connection DriverManager::getConnection($params$config$eventManager);
  98.         }
  99.         if (! empty($mappingTypes)) {
  100.             $platform $this->getDatabasePlatform($connection);
  101.             foreach ($mappingTypes as $dbType => $doctrineType) {
  102.                 $platform->registerDoctrineTypeMapping($dbType$doctrineType);
  103.             }
  104.         }
  105.         return $connection;
  106.     }
  107.     /**
  108.      * Try to get the database platform.
  109.      *
  110.      * This could fail if types should be registered to an predefined/unused connection
  111.      * and the platform version is unknown.
  112.      * For details have a look at DoctrineBundle issue #673.
  113.      *
  114.      * @throws DBALException
  115.      */
  116.     private function getDatabasePlatform(Connection $connection): AbstractPlatform
  117.     {
  118.         try {
  119.             return $connection->getDatabasePlatform();
  120.         } catch (DriverException $driverException) {
  121.             throw new DBALException(
  122.                 'An exception occurred while establishing a connection to figure out your platform version.' PHP_EOL .
  123.                 "You can circumvent this by setting a 'server_version' configuration value" PHP_EOL PHP_EOL .
  124.                 'For further information have a look at:' PHP_EOL .
  125.                 'https://github.com/doctrine/DoctrineBundle/issues/673',
  126.                 0,
  127.                 $driverException
  128.             );
  129.         }
  130.     }
  131.     /**
  132.      * initialize the types
  133.      */
  134.     private function initializeTypes(): void
  135.     {
  136.         foreach ($this->typesConfig as $typeName => $typeConfig) {
  137.             if (Type::hasType($typeName)) {
  138.                 Type::overrideType($typeName$typeConfig['class']);
  139.             } else {
  140.                 Type::addType($typeName$typeConfig['class']);
  141.             }
  142.         }
  143.         $this->initialized true;
  144.     }
  145.     /**
  146.      * @param array<string, mixed> $params
  147.      *
  148.      * @return array<string, mixed>
  149.      */
  150.     private function addDatabaseSuffix(array $params): array
  151.     {
  152.         if (isset($params['dbname']) && isset($params['dbname_suffix'])) {
  153.             $params['dbname'] .= $params['dbname_suffix'];
  154.         }
  155.         foreach ($params['replica'] ?? [] as $key => $replicaParams) {
  156.             if (! isset($replicaParams['dbname'], $replicaParams['dbname_suffix'])) {
  157.                 continue;
  158.             }
  159.             $params['replica'][$key]['dbname'] .= $replicaParams['dbname_suffix'];
  160.         }
  161.         if (isset($params['primary']['dbname'], $params['primary']['dbname_suffix'])) {
  162.             $params['primary']['dbname'] .= $params['primary']['dbname_suffix'];
  163.         }
  164.         return $params;
  165.     }
  166. }