vendor/doctrine/doctrine-cache-bundle/DependencyInjection/CacheProviderLoader.php line 117

Open in your IDE?
  1. <?php
  2. namespace Doctrine\Bundle\DoctrineCacheBundle\DependencyInjection;
  3. use Doctrine\Common\Inflector\Inflector;
  4. use Symfony\Component\DependencyInjection\Alias;
  5. use Symfony\Component\DependencyInjection\ContainerBuilder;
  6. /**
  7.  * Cache provider loader
  8.  *
  9.  * @author Fabio B. Silva <fabio.bat.silva@gmail.com>
  10.  */
  11. class CacheProviderLoader
  12. {
  13.     /**
  14.      * @param string                                                    $name
  15.      * @param array                                                     $config
  16.      * @param \Symfony\Component\DependencyInjection\ContainerBuilder   $container
  17.      */
  18.     public function loadCacheProvider($name, array $configContainerBuilder $container)
  19.     {
  20.         $serviceId  'doctrine_cache.providers.' $name;
  21.         $decorator  $this->getProviderDecorator($container$config);
  22.         $service    $container->setDefinition($serviceId$decorator);
  23.         $type       = ($config['type'] === 'custom_provider')
  24.             ? $config['custom_provider']['type']
  25.             : $config['type'];
  26.         if ($config['namespace']) {
  27.             $service->addMethodCall('setNamespace', array($config['namespace']));
  28.         }
  29.         $service->setPublic(true);
  30.         foreach ($config['aliases'] as $alias) {
  31.             $container->setAlias($alias, new Alias($serviceIdtrue));
  32.         }
  33.         if ($this->definitionClassExists($type$container)) {
  34.             $this->getCacheDefinition($type$container)->configure($name$config$service$container);
  35.         }
  36.     }
  37.     /**
  38.      * @param \Symfony\Component\DependencyInjection\ContainerBuilder   $container
  39.      * @param array                                                     $config
  40.      *
  41.      * @return \Symfony\Component\DependencyInjection\DefinitionDecorator
  42.      */
  43.     protected function getProviderDecorator(ContainerBuilder $container, array $config)
  44.     {
  45.         $type $config['type'];
  46.         $id   'doctrine_cache.abstract.' $type;
  47.         static $childDefinition;
  48.         if (null === $childDefinition) {
  49.             $childDefinition class_exists('Symfony\Component\DependencyInjection\ChildDefinition') ? 'Symfony\Component\DependencyInjection\ChildDefinition' 'Symfony\Component\DependencyInjection\DefinitionDecorator';
  50.         }
  51.         if ($type === 'custom_provider') {
  52.             $type  $config['custom_provider']['type'];
  53.             $param $this->getCustomProviderParameter($type);
  54.             if ($container->hasParameter($param)) {
  55.                 return new $childDefinition($container->getParameter($param));
  56.             }
  57.         }
  58.         if ($container->hasDefinition($id)) {
  59.             return new $childDefinition($id);
  60.         }
  61.         throw new \InvalidArgumentException(sprintf('"%s" is an unrecognized Doctrine cache driver.'$type));
  62.     }
  63.     /**
  64.      * @param string                                                    $type
  65.      * @param \Symfony\Component\DependencyInjection\ContainerBuilder   $container
  66.      *
  67.      * @return \Doctrine\Bundle\DoctrineCacheBundle\DependencyInjection\Definition\CacheDefinition
  68.      */
  69.     private function getCacheDefinition($typeContainerBuilder $container)
  70.     {
  71.         $class  $this->getDefinitionClass($type$container);
  72.         $object = new $class($type);
  73.         return $object;
  74.     }
  75.     /**
  76.      * @param string                                                    $type
  77.      * @param \Symfony\Component\DependencyInjection\ContainerBuilder   $container
  78.      *
  79.      * @return boolean
  80.      */
  81.     private function definitionClassExists($typeContainerBuilder $container)
  82.     {
  83.         if ($container->hasParameter($this->getCustomDefinitionClassParameter($type))) {
  84.             return true;
  85.         }
  86.         return class_exists($this->getDefinitionClass($type$container));
  87.     }
  88.     /**
  89.      * @param string                                                    $type
  90.      * @param \Symfony\Component\DependencyInjection\ContainerBuilder   $container
  91.      *
  92.      * @return string
  93.      */
  94.     protected function getDefinitionClass($typeContainerBuilder $container)
  95.     {
  96.         if ($container->hasParameter($this->getCustomDefinitionClassParameter($type))) {
  97.             return $container->getParameter($this->getCustomDefinitionClassParameter($type));
  98.         }
  99.         $name  Inflector::classify($type) . 'Definition';
  100.         $class sprintf('%s\Definition\%s'__NAMESPACE__$name);
  101.         return $class;
  102.     }
  103.     /**
  104.      * @param string $type
  105.      *
  106.      * @return string
  107.      */
  108.     public function getCustomProviderParameter($type)
  109.     {
  110.         return 'doctrine_cache.custom_provider.' $type;
  111.     }
  112.     /**
  113.      * @param string $type
  114.      *
  115.      * @return string
  116.      */
  117.     public function getCustomDefinitionClassParameter($type)
  118.     {
  119.         return 'doctrine_cache.custom_definition_class.' $type;
  120.     }
  121. }