vendor/symfony/doctrine-bridge/DependencyInjection/AbstractDoctrineExtension.php line 181

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bridge\Doctrine\DependencyInjection;
  11. use Symfony\Component\DependencyInjection\Alias;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Definition;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. use Symfony\Component\HttpKernel\DependencyInjection\Extension;
  16. /**
  17.  * This abstract classes groups common code that Doctrine Object Manager extensions (ORM, MongoDB, CouchDB) need.
  18.  *
  19.  * @author Benjamin Eberlei <kontakt@beberlei.de>
  20.  */
  21. abstract class AbstractDoctrineExtension extends Extension
  22. {
  23.     /**
  24.      * Used inside metadata driver method to simplify aggregation of data.
  25.      */
  26.     protected $aliasMap = [];
  27.     /**
  28.      * Used inside metadata driver method to simplify aggregation of data.
  29.      */
  30.     protected $drivers = [];
  31.     /**
  32.      * @param array $objectManager A configured object manager
  33.      *
  34.      * @throws \InvalidArgumentException
  35.      */
  36.     protected function loadMappingInformation(array $objectManagerContainerBuilder $container)
  37.     {
  38.         if ($objectManager['auto_mapping']) {
  39.             // automatically register bundle mappings
  40.             foreach (array_keys($container->getParameter('kernel.bundles')) as $bundle) {
  41.                 if (!isset($objectManager['mappings'][$bundle])) {
  42.                     $objectManager['mappings'][$bundle] = [
  43.                         'mapping' => true,
  44.                         'is_bundle' => true,
  45.                     ];
  46.                 }
  47.             }
  48.         }
  49.         foreach ($objectManager['mappings'] as $mappingName => $mappingConfig) {
  50.             if (null !== $mappingConfig && false === $mappingConfig['mapping']) {
  51.                 continue;
  52.             }
  53.             $mappingConfig array_replace([
  54.                 'dir' => false,
  55.                 'type' => false,
  56.                 'prefix' => false,
  57.             ], (array) $mappingConfig);
  58.             $mappingConfig['dir'] = $container->getParameterBag()->resolveValue($mappingConfig['dir']);
  59.             // a bundle configuration is detected by realizing that the specified dir is not absolute and existing
  60.             if (!isset($mappingConfig['is_bundle'])) {
  61.                 $mappingConfig['is_bundle'] = !is_dir($mappingConfig['dir']);
  62.             }
  63.             if ($mappingConfig['is_bundle']) {
  64.                 $bundle null;
  65.                 foreach ($container->getParameter('kernel.bundles') as $name => $class) {
  66.                     if ($mappingName === $name) {
  67.                         $bundle = new \ReflectionClass($class);
  68.                         break;
  69.                     }
  70.                 }
  71.                 if (null === $bundle) {
  72.                     throw new \InvalidArgumentException(sprintf('Bundle "%s" does not exist or it is not enabled.'$mappingName));
  73.                 }
  74.                 $mappingConfig $this->getMappingDriverBundleConfigDefaults($mappingConfig$bundle$container);
  75.                 if (!$mappingConfig) {
  76.                     continue;
  77.                 }
  78.             }
  79.             $this->assertValidMappingConfiguration($mappingConfig$objectManager['name']);
  80.             $this->setMappingDriverConfig($mappingConfig$mappingName);
  81.             $this->setMappingDriverAlias($mappingConfig$mappingName);
  82.         }
  83.     }
  84.     /**
  85.      * Register the alias for this mapping driver.
  86.      *
  87.      * Aliases can be used in the Query languages of all the Doctrine object managers to simplify writing tasks.
  88.      *
  89.      * @param array  $mappingConfig
  90.      * @param string $mappingName
  91.      */
  92.     protected function setMappingDriverAlias($mappingConfig$mappingName)
  93.     {
  94.         if (isset($mappingConfig['alias'])) {
  95.             $this->aliasMap[$mappingConfig['alias']] = $mappingConfig['prefix'];
  96.         } else {
  97.             $this->aliasMap[$mappingName] = $mappingConfig['prefix'];
  98.         }
  99.     }
  100.     /**
  101.      * Register the mapping driver configuration for later use with the object managers metadata driver chain.
  102.      *
  103.      * @param string $mappingName
  104.      *
  105.      * @throws \InvalidArgumentException
  106.      */
  107.     protected function setMappingDriverConfig(array $mappingConfig$mappingName)
  108.     {
  109.         $mappingDirectory $mappingConfig['dir'];
  110.         if (!is_dir($mappingDirectory)) {
  111.             throw new \InvalidArgumentException(sprintf('Invalid Doctrine mapping path given. Cannot load Doctrine mapping/bundle named "%s".'$mappingName));
  112.         }
  113.         $this->drivers[$mappingConfig['type']][$mappingConfig['prefix']] = realpath($mappingDirectory) ?: $mappingDirectory;
  114.     }
  115.     /**
  116.      * If this is a bundle controlled mapping all the missing information can be autodetected by this method.
  117.      *
  118.      * Returns false when autodetection failed, an array of the completed information otherwise.
  119.      *
  120.      * @return array|false
  121.      */
  122.     protected function getMappingDriverBundleConfigDefaults(array $bundleConfig, \ReflectionClass $bundleContainerBuilder $container)
  123.     {
  124.         $bundleDir = \dirname($bundle->getFileName());
  125.         if (!$bundleConfig['type']) {
  126.             $bundleConfig['type'] = $this->detectMetadataDriver($bundleDir$container);
  127.         }
  128.         if (!$bundleConfig['type']) {
  129.             // skip this bundle, no mapping information was found.
  130.             return false;
  131.         }
  132.         if (!$bundleConfig['dir']) {
  133.             if (\in_array($bundleConfig['type'], ['annotation''staticphp'])) {
  134.                 $bundleConfig['dir'] = $bundleDir.'/'.$this->getMappingObjectDefaultName();
  135.             } else {
  136.                 $bundleConfig['dir'] = $bundleDir.'/'.$this->getMappingResourceConfigDirectory();
  137.             }
  138.         } else {
  139.             $bundleConfig['dir'] = $bundleDir.'/'.$bundleConfig['dir'];
  140.         }
  141.         if (!$bundleConfig['prefix']) {
  142.             $bundleConfig['prefix'] = $bundle->getNamespaceName().'\\'.$this->getMappingObjectDefaultName();
  143.         }
  144.         return $bundleConfig;
  145.     }
  146.     /**
  147.      * Register all the collected mapping information with the object manager by registering the appropriate mapping drivers.
  148.      *
  149.      * @param array $objectManager
  150.      */
  151.     protected function registerMappingDrivers($objectManagerContainerBuilder $container)
  152.     {
  153.         // configure metadata driver for each bundle based on the type of mapping files found
  154.         if ($container->hasDefinition($this->getObjectManagerElementName($objectManager['name'].'_metadata_driver'))) {
  155.             $chainDriverDef $container->getDefinition($this->getObjectManagerElementName($objectManager['name'].'_metadata_driver'));
  156.         } else {
  157.             $chainDriverDef = new Definition($this->getMetadataDriverClass('driver_chain'));
  158.             $chainDriverDef->setPublic(false);
  159.         }
  160.         foreach ($this->drivers as $driverType => $driverPaths) {
  161.             $mappingService $this->getObjectManagerElementName($objectManager['name'].'_'.$driverType.'_metadata_driver');
  162.             if ($container->hasDefinition($mappingService)) {
  163.                 $mappingDriverDef $container->getDefinition($mappingService);
  164.                 $args $mappingDriverDef->getArguments();
  165.                 if ('annotation' == $driverType) {
  166.                     $args[1] = array_merge(array_values($driverPaths), $args[1]);
  167.                 } else {
  168.                     $args[0] = array_merge(array_values($driverPaths), $args[0]);
  169.                 }
  170.                 $mappingDriverDef->setArguments($args);
  171.             } elseif ('annotation' == $driverType) {
  172.                 $mappingDriverDef = new Definition($this->getMetadataDriverClass($driverType), [
  173.                     new Reference($this->getObjectManagerElementName('metadata.annotation_reader')),
  174.                     array_values($driverPaths),
  175.                 ]);
  176.             } else {
  177.                 $mappingDriverDef = new Definition($this->getMetadataDriverClass($driverType), [
  178.                     array_values($driverPaths),
  179.                 ]);
  180.             }
  181.             $mappingDriverDef->setPublic(false);
  182.             if (false !== strpos($mappingDriverDef->getClass(), 'yml') || false !== strpos($mappingDriverDef->getClass(), 'xml')) {
  183.                 $mappingDriverDef->setArguments([array_flip($driverPaths)]);
  184.                 $mappingDriverDef->addMethodCall('setGlobalBasename', ['mapping']);
  185.             }
  186.             $container->setDefinition($mappingService$mappingDriverDef);
  187.             foreach ($driverPaths as $prefix => $driverPath) {
  188.                 $chainDriverDef->addMethodCall('addDriver', [new Reference($mappingService), $prefix]);
  189.             }
  190.         }
  191.         $container->setDefinition($this->getObjectManagerElementName($objectManager['name'].'_metadata_driver'), $chainDriverDef);
  192.     }
  193.     /**
  194.      * Assertion if the specified mapping information is valid.
  195.      *
  196.      * @param string $objectManagerName
  197.      *
  198.      * @throws \InvalidArgumentException
  199.      */
  200.     protected function assertValidMappingConfiguration(array $mappingConfig$objectManagerName)
  201.     {
  202.         if (!$mappingConfig['type'] || !$mappingConfig['dir'] || !$mappingConfig['prefix']) {
  203.             throw new \InvalidArgumentException(sprintf('Mapping definitions for Doctrine manager "%s" require at least the "type", "dir" and "prefix" options.'$objectManagerName));
  204.         }
  205.         if (!is_dir($mappingConfig['dir'])) {
  206.             throw new \InvalidArgumentException(sprintf('Specified non-existing directory "%s" as Doctrine mapping source.'$mappingConfig['dir']));
  207.         }
  208.         if (!\in_array($mappingConfig['type'], ['xml''yml''annotation''php''staticphp'])) {
  209.             throw new \InvalidArgumentException(sprintf('Can only configure "xml", "yml", "annotation", "php" or '.
  210.                 '"staticphp" through the DoctrineBundle. Use your own bundle to configure other metadata drivers. '.
  211.                 'You can register them by adding a new driver to the '.
  212.                 '"%s" service definition.'$this->getObjectManagerElementName($objectManagerName.'_metadata_driver')
  213.             ));
  214.         }
  215.     }
  216.     /**
  217.      * Detects what metadata driver to use for the supplied directory.
  218.      *
  219.      * @param string $dir A directory path
  220.      *
  221.      * @return string|null A metadata driver short name, if one can be detected
  222.      */
  223.     protected function detectMetadataDriver($dirContainerBuilder $container)
  224.     {
  225.         $configPath $this->getMappingResourceConfigDirectory();
  226.         $extension $this->getMappingResourceExtension();
  227.         if (glob($dir.'/'.$configPath.'/*.'.$extension.'.xml'GLOB_NOSORT)) {
  228.             $driver 'xml';
  229.         } elseif (glob($dir.'/'.$configPath.'/*.'.$extension.'.yml'GLOB_NOSORT)) {
  230.             $driver 'yml';
  231.         } elseif (glob($dir.'/'.$configPath.'/*.'.$extension.'.php'GLOB_NOSORT)) {
  232.             $driver 'php';
  233.         } else {
  234.             // add the closest existing directory as a resource
  235.             $resource $dir.'/'.$configPath;
  236.             while (!is_dir($resource)) {
  237.                 $resource = \dirname($resource);
  238.             }
  239.             $container->fileExists($resourcefalse);
  240.             return $container->fileExists($dir.'/'.$this->getMappingObjectDefaultName(), false) ? 'annotation' null;
  241.         }
  242.         $container->fileExists($dir.'/'.$configPathfalse);
  243.         return $driver;
  244.     }
  245.     /**
  246.      * Loads a configured object manager metadata, query or result cache driver.
  247.      *
  248.      * @param array  $objectManager A configured object manager
  249.      * @param string $cacheName
  250.      *
  251.      * @throws \InvalidArgumentException in case of unknown driver type
  252.      */
  253.     protected function loadObjectManagerCacheDriver(array $objectManagerContainerBuilder $container$cacheName)
  254.     {
  255.         $this->loadCacheDriver($cacheName$objectManager['name'], $objectManager[$cacheName.'_driver'], $container);
  256.     }
  257.     /**
  258.      * Loads a cache driver.
  259.      *
  260.      * @param string $cacheName         The cache driver name
  261.      * @param string $objectManagerName The object manager name
  262.      * @param array  $cacheDriver       The cache driver mapping
  263.      *
  264.      * @return string
  265.      *
  266.      * @throws \InvalidArgumentException
  267.      */
  268.     protected function loadCacheDriver($cacheName$objectManagerName, array $cacheDriverContainerBuilder $container)
  269.     {
  270.         $cacheDriverServiceId $this->getObjectManagerElementName($objectManagerName.'_'.$cacheName);
  271.         switch ($cacheDriver['type']) {
  272.             case 'service':
  273.                 $container->setAlias($cacheDriverServiceId, new Alias($cacheDriver['id'], false));
  274.                 return $cacheDriverServiceId;
  275.             case 'memcached':
  276.                 $memcachedClass = !empty($cacheDriver['class']) ? $cacheDriver['class'] : '%'.$this->getObjectManagerElementName('cache.memcached.class').'%';
  277.                 $memcachedInstanceClass = !empty($cacheDriver['instance_class']) ? $cacheDriver['instance_class'] : '%'.$this->getObjectManagerElementName('cache.memcached_instance.class').'%';
  278.                 $memcachedHost = !empty($cacheDriver['host']) ? $cacheDriver['host'] : '%'.$this->getObjectManagerElementName('cache.memcached_host').'%';
  279.                 $memcachedPort = !empty($cacheDriver['port']) ? $cacheDriver['port'] : '%'.$this->getObjectManagerElementName('cache.memcached_port').'%';
  280.                 $cacheDef = new Definition($memcachedClass);
  281.                 $memcachedInstance = new Definition($memcachedInstanceClass);
  282.                 $memcachedInstance->setPrivate(true);
  283.                 $memcachedInstance->addMethodCall('addServer', [
  284.                     $memcachedHost$memcachedPort,
  285.                 ]);
  286.                 $container->setDefinition($this->getObjectManagerElementName(sprintf('%s_memcached_instance'$objectManagerName)), $memcachedInstance);
  287.                 $cacheDef->addMethodCall('setMemcached', [new Reference($this->getObjectManagerElementName(sprintf('%s_memcached_instance'$objectManagerName)))]);
  288.                 break;
  289.              case 'redis':
  290.                 $redisClass = !empty($cacheDriver['class']) ? $cacheDriver['class'] : '%'.$this->getObjectManagerElementName('cache.redis.class').'%';
  291.                 $redisInstanceClass = !empty($cacheDriver['instance_class']) ? $cacheDriver['instance_class'] : '%'.$this->getObjectManagerElementName('cache.redis_instance.class').'%';
  292.                 $redisHost = !empty($cacheDriver['host']) ? $cacheDriver['host'] : '%'.$this->getObjectManagerElementName('cache.redis_host').'%';
  293.                 $redisPort = !empty($cacheDriver['port']) ? $cacheDriver['port'] : '%'.$this->getObjectManagerElementName('cache.redis_port').'%';
  294.                 $cacheDef = new Definition($redisClass);
  295.                 $redisInstance = new Definition($redisInstanceClass);
  296.                 $redisInstance->setPrivate(true);
  297.                 $redisInstance->addMethodCall('connect', [
  298.                     $redisHost$redisPort,
  299.                 ]);
  300.                 $container->setDefinition($this->getObjectManagerElementName(sprintf('%s_redis_instance'$objectManagerName)), $redisInstance);
  301.                 $cacheDef->addMethodCall('setRedis', [new Reference($this->getObjectManagerElementName(sprintf('%s_redis_instance'$objectManagerName)))]);
  302.                 break;
  303.             case 'apc':
  304.             case 'apcu':
  305.             case 'array':
  306.             case 'xcache':
  307.             case 'wincache':
  308.             case 'zenddata':
  309.                 $cacheDef = new Definition('%'.$this->getObjectManagerElementName(sprintf('cache.%s.class'$cacheDriver['type'])).'%');
  310.                 break;
  311.             default:
  312.                 throw new \InvalidArgumentException(sprintf('"%s" is an unrecognized Doctrine cache driver.'$cacheDriver['type']));
  313.         }
  314.         $cacheDef->setPublic(false);
  315.         if (!isset($cacheDriver['namespace'])) {
  316.             // generate a unique namespace for the given application
  317.             if ($container->hasParameter('cache.prefix.seed')) {
  318.                 $seed '.'.$container->getParameterBag()->resolveValue($container->getParameter('cache.prefix.seed'));
  319.             } else {
  320.                 $seed '_'.$container->getParameter('kernel.project_dir');
  321.             }
  322.             $seed .= '.'.$container->getParameter('kernel.container_class');
  323.             $namespace 'sf_'.$this->getMappingResourceExtension().'_'.$objectManagerName.'_'.ContainerBuilder::hash($seed);
  324.             $cacheDriver['namespace'] = $namespace;
  325.         }
  326.         $cacheDef->addMethodCall('setNamespace', [$cacheDriver['namespace']]);
  327.         $container->setDefinition($cacheDriverServiceId$cacheDef);
  328.         return $cacheDriverServiceId;
  329.     }
  330.     /**
  331.      * Returns a modified version of $managerConfigs.
  332.      *
  333.      * The manager called $autoMappedManager will map all bundles that are not mapped by other managers.
  334.      *
  335.      * @return array The modified version of $managerConfigs
  336.      */
  337.     protected function fixManagersAutoMappings(array $managerConfigs, array $bundles)
  338.     {
  339.         if ($autoMappedManager $this->validateAutoMapping($managerConfigs)) {
  340.             foreach (array_keys($bundles) as $bundle) {
  341.                 foreach ($managerConfigs as $manager) {
  342.                     if (isset($manager['mappings'][$bundle])) {
  343.                         continue 2;
  344.                     }
  345.                 }
  346.                 $managerConfigs[$autoMappedManager]['mappings'][$bundle] = [
  347.                     'mapping' => true,
  348.                     'is_bundle' => true,
  349.                 ];
  350.             }
  351.             $managerConfigs[$autoMappedManager]['auto_mapping'] = false;
  352.         }
  353.         return $managerConfigs;
  354.     }
  355.     /**
  356.      * Prefixes the relative dependency injection container path with the object manager prefix.
  357.      *
  358.      * @example $name is 'entity_manager' then the result would be 'doctrine.orm.entity_manager'
  359.      *
  360.      * @param string $name
  361.      *
  362.      * @return string
  363.      */
  364.     abstract protected function getObjectManagerElementName($name);
  365.     /**
  366.      * Noun that describes the mapped objects such as Entity or Document.
  367.      *
  368.      * Will be used for autodetection of persistent objects directory.
  369.      *
  370.      * @return string
  371.      */
  372.     abstract protected function getMappingObjectDefaultName();
  373.     /**
  374.      * Relative path from the bundle root to the directory where mapping files reside.
  375.      *
  376.      * @return string
  377.      */
  378.     abstract protected function getMappingResourceConfigDirectory();
  379.     /**
  380.      * Extension used by the mapping files.
  381.      *
  382.      * @return string
  383.      */
  384.     abstract protected function getMappingResourceExtension();
  385.     /**
  386.      * The class name used by the various mapping drivers.
  387.      */
  388.     protected function getMetadataDriverClass(string $driverType): string
  389.     {
  390.         @trigger_error(sprintf('Not declaring the "%s" method in class "%s" is deprecated since Symfony 4.4. This method will be abstract in Symfony 5.0.'__METHOD__, static::class), E_USER_DEPRECATED);
  391.         return '%'.$this->getObjectManagerElementName('metadata.'.$driverType.'.class%');
  392.     }
  393.     /**
  394.      * Search for a manager that is declared as 'auto_mapping' = true.
  395.      *
  396.      * @throws \LogicException
  397.      */
  398.     private function validateAutoMapping(array $managerConfigs): ?string
  399.     {
  400.         $autoMappedManager null;
  401.         foreach ($managerConfigs as $name => $manager) {
  402.             if (!$manager['auto_mapping']) {
  403.                 continue;
  404.             }
  405.             if (null !== $autoMappedManager) {
  406.                 throw new \LogicException(sprintf('You cannot enable "auto_mapping" on more than one manager at the same time (found in "%s" and %s").'$autoMappedManager$name));
  407.             }
  408.             $autoMappedManager $name;
  409.         }
  410.         return $autoMappedManager;
  411.     }
  412. }