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

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\DriverManager;
  8. use Doctrine\DBAL\Exception\DriverException;
  9. use Doctrine\DBAL\Platforms\AbstractPlatform;
  10. use Doctrine\DBAL\Types\Type;
  11. use const E_USER_DEPRECATED;
  12. use function get_class;
  13. use function trigger_error;
  14. class ConnectionFactory
  15. {
  16.     /** @var mixed[][] */
  17.     private $typesConfig = [];
  18.     /** @var bool */
  19.     private $initialized false;
  20.     /**
  21.      * @param mixed[][] $typesConfig
  22.      */
  23.     public function __construct(array $typesConfig)
  24.     {
  25.         $this->typesConfig $typesConfig;
  26.     }
  27.     /**
  28.      * Create a connection by name.
  29.      *
  30.      * @param mixed[]         $params
  31.      * @param string[]|Type[] $mappingTypes
  32.      *
  33.      * @return Connection
  34.      */
  35.     public function createConnection(array $paramsConfiguration $config nullEventManager $eventManager null, array $mappingTypes = [])
  36.     {
  37.         if (! $this->initialized) {
  38.             $this->initializeTypes();
  39.         }
  40.         $connection DriverManager::getConnection($params$config$eventManager);
  41.         if (! empty($mappingTypes)) {
  42.             $platform $this->getDatabasePlatform($connection);
  43.             foreach ($mappingTypes as $dbType => $doctrineType) {
  44.                 $platform->registerDoctrineTypeMapping($dbType$doctrineType);
  45.             }
  46.         }
  47.         if (! empty($this->typesConfig)) {
  48.             $this->markTypesCommented($this->getDatabasePlatform($connection));
  49.         }
  50.         return $connection;
  51.     }
  52.     /**
  53.      * Try to get the database platform.
  54.      *
  55.      * This could fail if types should be registered to an predefined/unused connection
  56.      * and the platform version is unknown.
  57.      * For details have a look at DoctrineBundle issue #673.
  58.      *
  59.      * @return AbstractPlatform
  60.      *
  61.      * @throws DBALException
  62.      */
  63.     private function getDatabasePlatform(Connection $connection)
  64.     {
  65.         try {
  66.             return $connection->getDatabasePlatform();
  67.         } catch (DriverException $driverException) {
  68.             throw new DBALException(
  69.                 'An exception occured while establishing a connection to figure out your platform version.' PHP_EOL .
  70.                 "You can circumvent this by setting a 'server_version' configuration value" PHP_EOL PHP_EOL .
  71.                 'For further information have a look at:' PHP_EOL .
  72.                 'https://github.com/doctrine/DoctrineBundle/issues/673',
  73.                 0,
  74.                 $driverException
  75.             );
  76.         }
  77.     }
  78.     /**
  79.      * initialize the types
  80.      */
  81.     private function initializeTypes()
  82.     {
  83.         foreach ($this->typesConfig as $typeName => $typeConfig) {
  84.             if (Type::hasType($typeName)) {
  85.                 Type::overrideType($typeName$typeConfig['class']);
  86.             } else {
  87.                 Type::addType($typeName$typeConfig['class']);
  88.             }
  89.         }
  90.         $this->initialized true;
  91.     }
  92.     private function markTypesCommented(AbstractPlatform $platform) : void
  93.     {
  94.         foreach ($this->typesConfig as $typeName => $typeConfig) {
  95.             $type                   Type::getType($typeName);
  96.             $requiresSQLCommentHint $type->requiresSQLCommentHint($platform);
  97.             // Attribute is missing, make sure a type that doesn't require a comment is marked as commented
  98.             // This is deprecated behaviour that will be dropped in 2.0.
  99.             if ($typeConfig['commented'] === null) {
  100.                 if (! $requiresSQLCommentHint) {
  101.                     @trigger_error(
  102.                         sprintf(
  103.                             'The type "%s" was implicitly marked as commented due to the configuration. This is deprecated and will be removed in DoctrineBundle 2.0. Either set the "commented" attribute in the configuration to "false" or mark the type as commented in "%s::requiresSQLCommentHint()."',
  104.                             $typeName,
  105.                             get_class($type)
  106.                         ),
  107.                         E_USER_DEPRECATED
  108.                     );
  109.                     $platform->markDoctrineTypeCommented($type);
  110.                 }
  111.                 continue;
  112.             }
  113.             // The following logic generates appropriate deprecation notices telling the user how to update their type configuration.
  114.             if ($typeConfig['commented']) {
  115.                 if (! $requiresSQLCommentHint) {
  116.                     @trigger_error(
  117.                         sprintf(
  118.                             'The type "%s" was marked as commented in its configuration but not in the type itself. This is deprecated and will be removed in DoctrineBundle 2.0. Please update the return value of "%s::requiresSQLCommentHint()."',
  119.                             $typeName,
  120.                             get_class($type)
  121.                         ),
  122.                         E_USER_DEPRECATED
  123.                     );
  124.                     $platform->markDoctrineTypeCommented($type);
  125.                     continue;
  126.                 }
  127.                 @trigger_error(
  128.                     sprintf(
  129.                         'The type "%s" was explicitly marked as commented in its configuration. This is no longer necessary and will be removed in DoctrineBundle 2.0. Please remove the "commented" attribute from the type configuration.',
  130.                         $typeName
  131.                     ),
  132.                     E_USER_DEPRECATED
  133.                 );
  134.                 continue;
  135.             }
  136.             if (! $requiresSQLCommentHint) {
  137.                 continue;
  138.             }
  139.             @trigger_error(
  140.                 sprintf(
  141.                     'The type "%s" was marked as uncommented in its configuration but commented in the type itself. This is deprecated and will be removed in DoctrineBundle 2.0. Please update the return value of "%s::requiresSQLCommentHint()" or remove the "commented" attribute from the type configuration.',
  142.                     $typeName,
  143.                     get_class($type)
  144.                 ),
  145.                 E_USER_DEPRECATED
  146.             );
  147.         }
  148.     }
  149. }