vendor/jms/di-extra-bundle/DependencyInjection/JMSDiExtraExtension.php line 75

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  * http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. namespace JMS\DiExtraBundle\DependencyInjection;
  18. use CG\Core\DefaultNamingStrategy;
  19. use CG\Proxy\Enhancer;
  20. use JMS\DiExtraBundle\Exception\RuntimeException;
  21. use JMS\DiExtraBundle\Generator\RepositoryInjectionGenerator;
  22. use Symfony\Component\Config\Definition\Processor;
  23. use Symfony\Component\Config\FileLocator;
  24. use Symfony\Component\DependencyInjection\Alias;
  25. use Symfony\Component\DependencyInjection\ContainerBuilder;
  26. use Symfony\Component\DependencyInjection\Definition;
  27. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  28. use Symfony\Component\Filesystem\Filesystem;
  29. use Symfony\Component\HttpKernel\DependencyInjection\Extension;
  30. class JMSDiExtraExtension extends Extension
  31. {
  32.     /**
  33.      * Controller blacklist, ie. php files names for controllers that should not be analyzed.
  34.      *
  35.      * @var array
  36.      */
  37.     private $blackListedControllerFiles = array();
  38.     public function load(array $configsContainerBuilder $container)
  39.     {
  40.         $config $this->mergeConfigs($configs);
  41.         $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
  42.         $loader->load('services.xml');
  43.         $container->setParameter('jms_di_extra.all_bundles'$config['locations']['all_bundles']);
  44.         $container->setParameter('jms_di_extra.bundles'$config['locations']['bundles']);
  45.         $container->setParameter('jms_di_extra.directories'$config['locations']['directories']);
  46.         $container->setParameter('jms_di_extra.cache_dir'$config['cache_dir']);
  47.         $container->setParameter('jms_di_extra.disable_grep'$config['disable_grep']);
  48.         $container->setParameter('jms_di_extra.doctrine_integration'$config['doctrine_integration']);
  49.         $container->setParameter('jms_di_extra.annotation_patterns'$config['annotation_patterns']);
  50.         if ($config['cache_warmer']['enabled']) {
  51.             foreach ($config['cache_warmer']['controller_file_blacklist'] as $filename) {
  52.                 $this->blackListControllerFile($filename);
  53.             }
  54.             $container->setParameter('jms_di_extra.cache_warmer.controller_file_blacklist'$this->blackListedControllerFiles);
  55.         } else {
  56.             $container->removeDefinition('jms_di_extra.controller_injectors_warmer');
  57.         }
  58.         $this->configureMetadata($config['metadata'], $container$config['cache_dir'].'/metadata');
  59.         $this->configureAutomaticControllerInjections($config$container);
  60.         if ($config['doctrine_integration']) {
  61.             $this->generateEntityManagerProxyClass($config$container);
  62.         }
  63.         $this->addClassesToCompile(array(
  64.             'JMS\\DiExtraBundle\\HttpKernel\ControllerResolver',
  65.         ));
  66.     }
  67.     public function blackListControllerFile($filename)
  68.     {
  69.         $this->blackListedControllerFiles[] = realpath($filename);
  70.     }
  71.     private function generateEntityManagerProxyClass(array $configContainerBuilder $container)
  72.     {
  73.         $cacheDir $container->getParameterBag()->resolveValue($config['cache_dir']);
  74.         if (!is_dir($cacheDir.'/doctrine')) {
  75.             if (false === @mkdir($cacheDir.'/doctrine'0777true)) {
  76.                 throw new \RuntimeException(sprintf('Could not create cache directory "%s".'$cacheDir.'/doctrine'));
  77.             }
  78.         }
  79.         $enhancer = new Enhancer($ref = new \ReflectionClass('Doctrine\ORM\EntityManager'), array(), array(new RepositoryInjectionGenerator()));
  80.         $uniqid uniqid(); // We do have to use a non-static id to avoid problems with cache:clear.
  81.         if (strtoupper(PHP_OS) == 'CYGWIN') {
  82.             $uniqid preg_replace('/\./''_'$uniqid); // replace dot; cygwin always generates uniqid's with more_entropy
  83.         }
  84.         $enhancer->setNamingStrategy(new DefaultNamingStrategy('EntityManager'.$uniqid));
  85.         $enhancer->writeClass($file $cacheDir.'/doctrine/EntityManager_'.$uniqid.'.php');
  86.         $container->setParameter('jms_di_extra.doctrine_integration.entity_manager.file'$file);
  87.         $container->setParameter('jms_di_extra.doctrine_integration.entity_manager.class'$enhancer->getClassName($ref));
  88.     }
  89.     private function configureAutomaticControllerInjections(array $configContainerBuilder $container)
  90.     {
  91.         if (!isset($config['automatic_controller_injections'])) {
  92.             $container->setAlias('jms_di_extra.metadata_driver''jms_di_extra.metadata.driver.annotation_driver');
  93.             return;
  94.         }
  95.         $propertyInjections = array();
  96.         foreach ($config['automatic_controller_injections']['properties'] as $name => $value) {
  97.             $propertyInjections[$name] = $this->convertValue($value);
  98.         }
  99.         $methodInjections = array();
  100.         foreach ($config['automatic_controller_injections']['method_calls'] as $name => $args) {
  101.             foreach ($args as $i => $arg) {
  102.                 $args[$i] = $this->convertValue($arg);
  103.             }
  104.             $methodInjections[$name] = $args;
  105.         }
  106.         $container->getDefinition('jms_di_extra.metadata.driver.configured_controller_injections')
  107.             ->addArgument($propertyInjections)
  108.             ->addArgument($methodInjections);
  109.     }
  110.     private function convertValue($value)
  111.     {
  112.         if (is_string($value) && '@' === $value[0]) {
  113.             $def = new Definition('Symfony\Component\DependencyInjection\Reference');
  114.             $def->addArgument(substr($value1));
  115.             return $def;
  116.         }
  117.         return $value;
  118.     }
  119.     private function configureMetadata(array $config$container$cacheDir)
  120.     {
  121.         if ('none' === $config['cache']) {
  122.             $container->removeAlias('jms_di_extra.metadata.cache');
  123.             return;
  124.         }
  125.         if ('file' === $config['cache']) {
  126.             $cacheDir $container->getParameterBag()->resolveValue($cacheDir);
  127.             // clear the cache if container is re-build, needed for correct controller injections
  128.             $fs = new Filesystem();
  129.             if ($fs->exists($cacheDir)) {
  130.                 $fs->remove($cacheDir);
  131.             }
  132.             if (!file_exists($cacheDir)) {
  133.                 if (false === @mkdir($cacheDir0777true)) {
  134.                     throw new RuntimeException(sprintf('The cache dir "%s" could not be created.'$cacheDir));
  135.                 }
  136.             }
  137.             if (!is_writable($cacheDir)) {
  138.                 throw new RuntimeException(sprintf('The cache dir "%s" is not writable.'$cacheDir));
  139.             }
  140.             $container
  141.                 ->getDefinition('jms_di_extra.metadata.cache.file_cache')
  142.                 ->replaceArgument(0$cacheDir)
  143.             ;
  144.         } else {
  145.             $container->setAlias('jms_di_extra.metadata.cache', new Alias($config['cache'], false));
  146.         }
  147.     }
  148.     private function mergeConfigs(array $configs)
  149.     {
  150.         $processor = new Processor();
  151.         $configuration = new Configuration();
  152.         return $processor->process($configuration->getConfigTreeBuilder()->buildTree(), $configs);
  153.     }
  154. }