vendor/jackalope/jackalope-doctrine-dbal/src/Jackalope/RepositoryFactoryDoctrineDBAL.php line 94

  1. <?php
  2. namespace Jackalope;
  3. use Doctrine\DBAL\ColumnCase;
  4. use Doctrine\DBAL\Connection;
  5. use Doctrine\DBAL\Platforms\OraclePlatform;
  6. use Doctrine\DBAL\Portability\Middleware as PortabilityMiddleware;
  7. use Doctrine\DBAL\Portability\Connection as PortabilityConnection;
  8. use Jackalope\Transport\DoctrineDBAL\CachedClient;
  9. use Jackalope\Transport\DoctrineDBAL\Client;
  10. use Jackalope\Transport\DoctrineDBAL\LoggingClient;
  11. use PHPCR\ConfigurationException;
  12. use PHPCR\RepositoryFactoryInterface;
  13. /**
  14.  * This factory creates repositories with the Doctrine DBAL transport
  15.  *
  16.  * Use repository factory based on parameters (the parameters below are examples):
  17.  *
  18.  * <pre>
  19.  *    $parameters = array('jackalope.doctrine_dbal_connection' => $dbConn);
  20.  *    $factory = new \Jackalope\RepositoryFactoryDoctrineDBAL();
  21.  *    $repository = $factory->getRepository($parameters);
  22.  * </pre>
  23.  *
  24.  * @license http://www.apache.org/licenses Apache License Version 2.0, January 2004
  25.  * @license http://opensource.org/licenses/MIT MIT License
  26.  *
  27.  * @api
  28.  */
  29. class RepositoryFactoryDoctrineDBAL implements RepositoryFactoryInterface
  30. {
  31.     /**
  32.      * List of required parameters for doctrine dbal.
  33.      *
  34.      * TODO: would be nice if alternatively one could also specify the parameters to let the factory build the connection
  35.      *
  36.      * @var array
  37.      */
  38.     private static $required = [
  39.         'jackalope.doctrine_dbal_connection' => 'Doctrine\\DBAL\\Connection (required): connection instance',
  40.     ];
  41.     /**
  42.      * List of optional parameters for doctrine dbal.
  43.      *
  44.      * @var array
  45.      */
  46.     private static $optional = [
  47.         'jackalope.factory' => 'string or object: Use a custom factory class for Jackalope objects',
  48.         'jackalope.check_login_on_server' => 'boolean: if set to empty or false, skip initial check whether repository exists. Enabled by default, disable to gain a few milliseconds off each repository instantiation.',
  49.         'jackalope.disable_transactions' => 'boolean: if set and not empty, transactions are disabled, otherwise transactions are enabled. If transactions are enabled but not actively used, every save operation is wrapped into a transaction.',
  50.         'jackalope.disable_stream_wrapper' => 'boolean: if set and not empty, stream wrapper is disabled, otherwise the stream wrapper is enabled and streams are only fetched when reading from for the first time. If your code always uses all binary properties it reads, you can disable this for a small performance gain.',
  51.         'jackalope.data_caches' => 'array: an array of \Doctrine\Common\Cache\Cache instances. keys can be "meta" and "nodes", should be separate namespaces for best performance.',
  52.         'jackalope.logger' => 'Psr\Log\LoggerInterface: Use the LoggingClient to wrap the default transport Client',
  53.         Session::OPTION_AUTO_LASTMODIFIED => 'boolean: Whether to automatically update nodes having mix:lastModified. Defaults to true.',
  54.     ];
  55.     /**
  56.      * Get a repository connected to the backend with the provided doctrine
  57.      * dbal connection.
  58.      *
  59.      * {@inheritDoc}
  60.      *
  61.      * DoctrineDBAL repositories have no default repository, passing null as
  62.      * parameters will always return null.
  63.      *
  64.      * @api
  65.      */
  66.     public function getRepository(array $parameters null)
  67.     {
  68.         if (null === $parameters) {
  69.             throw new ConfigurationException('Jackalope-doctrine-dbal needs parameters');
  70.         }
  71.         if (count(array_diff_key(self::$required$parameters))) {
  72.             throw new ConfigurationException('A required parameter is missing: ' implode(', 'array_keys(array_diff_key(self::$required$parameters))));
  73.         }
  74.         if (count(array_diff_key($parametersself::$requiredself::$optional))) {
  75.             throw new ConfigurationException('Additional unknown parameters found: ' implode(', 'array_keys(array_diff_key($parametersself::$requiredself::$optional))));
  76.         }
  77.         if (isset($parameters['jackalope.factory'])) {
  78.             $factory $parameters['jackalope.factory'] instanceof FactoryInterface
  79.                 $parameters['jackalope.factory'] : new $parameters['jackalope.factory'];
  80.         } else {
  81.             $factory = new Factory();
  82.         }
  83.         $dbConn $parameters['jackalope.doctrine_dbal_connection'];
  84.         \assert($dbConn instanceof Connection);
  85.         if ($dbConn->getDatabasePlatform() instanceof OraclePlatform) {
  86.             $this->ensureLowerCaseMiddleware($dbConn);
  87.         }
  88.         $transport = isset($parameters['jackalope.data_caches'])
  89.             ? $factory->get(CachedClient::class, [$dbConn$parameters['jackalope.data_caches']])
  90.             : $factory->get(Client::class, [$dbConn]);
  91.         if (isset($parameters['jackalope.check_login_on_server'])) {
  92.             $transport->setCheckLoginOnServer($parameters['jackalope.check_login_on_server']);
  93.         }
  94.         if (isset($parameters['jackalope.uuid_generator'])) {
  95.             $transport->setUuidGenerator($parameters['jackalope.uuid_generator']);
  96.         }
  97.         if (isset($parameters['jackalope.logger'])) {
  98.             $transport $factory->get(LoggingClient::class, [$transport$parameters['jackalope.logger']]);
  99.         }
  100.         $options['transactions'] = empty($parameters['jackalope.disable_transactions']);
  101.         $options['stream_wrapper'] = empty($parameters['jackalope.disable_stream_wrapper']);
  102.         if (isset($parameters[Session::OPTION_AUTO_LASTMODIFIED])) {
  103.             $options[Session::OPTION_AUTO_LASTMODIFIED] = $parameters[Session::OPTION_AUTO_LASTMODIFIED];
  104.         }
  105.         return new Repository($factory$transport$options);
  106.     }
  107.     /**
  108.      * {@inheritDoc}
  109.      *
  110.      * @api
  111.      */
  112.     public function getConfigurationKeys()
  113.     {
  114.         return array_merge(self::$requiredself::$optional);
  115.     }
  116.     /**
  117.      * Add the lowercase portability middleware if it is not already part of the configuration.
  118.      */
  119.     private function ensureLowerCaseMiddleware(Connection $dbConn): void
  120.     {
  121.         $configuration $dbConn->getConfiguration();
  122.         if (!$configuration) {
  123.             return;
  124.         }
  125.         foreach ($configuration->getMiddlewares() as $middleware) {
  126.             if ($middleware instanceof PortabilityMiddleware) {
  127.                 return;
  128.             }
  129.         }
  130.         $middlewares $configuration->getMiddlewares();
  131.         $middlewares[] = new PortabilityMiddleware(PortabilityConnection::PORTABILITY_FIX_CASEColumnCase::LOWER);
  132.         $configuration->setMiddlewares($middlewares);
  133.     }
  134. }