lib/composer/LisaBundle/src/VisualMedia/LisaBundle/Component/CompilerPass.php line 91

Open in your IDE?
  1. <?php
  2. /**
  3.  * Compiler Pass
  4.  *
  5.  * @author Vincent van Waasbergen <v.vanwaasbergen@visualmedia.nl>
  6.  */
  7. namespace VisualMedia\LisaBundle\Component;
  8. use Closure;
  9. use ReflectionClass;
  10. use Symfony\Component\DependencyInjection\Alias;
  11. use Symfony\Component\DependencyInjection\ContainerBuilder;
  12. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  13. use Symfony\Component\DependencyInjection\Definition;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. use Symfony\Component\Filesystem\Filesystem;
  16. use Symfony\Component\Finder\Finder;
  17. /**
  18.  * Compiler Pass
  19.  */
  20. abstract class CompilerPass implements CompilerPassInterface
  21. {
  22.     /**
  23.      * {@inheritdoc}
  24.      */
  25.     public function process(ContainerBuilder $container): void
  26.     {
  27.         $projectDir $container->getParameter('kernel.project_dir');
  28.         $bundles $container->getParameter(Bundle::PARAMETER_BUNDLES);
  29.         $fs = new Filesystem();
  30.         foreach ($bundles as $name=>$namespace) {
  31.             $reflection = new ReflectionClass("{$namespace}\\{$name}");
  32.             $bundleDir $reflection->getFileName();
  33.             $bundleDir explode('/'$bundleDir);
  34.             $bundleDir $bundleDir[count($bundleDir) - 5];
  35.             $bundleDir strtolower(preg_replace('/\B([A-Z])/''_$1'$bundleDir));
  36.             $namespaceDir str_replace('\\''/'$namespace);
  37.             $bundlePath sprintf('%s/vendor/visualmedia/%s/src/%s/'$projectDir$bundleDir$namespaceDir);
  38.             $this->processBundle($container$namespace$bundlePath);
  39.         }
  40.         $bundlePath sprintf('%s/src/'$projectDir);
  41.         $this->processBundle($container'App'$bundlePath);
  42.     }
  43.     /**
  44.      * @param string $namespace
  45.      * @param string $bundlePath
  46.      * @return void
  47.      */
  48.     protected function processBundle(ContainerBuilder $container$namespace$bundlePath): void
  49.     {
  50.         // ...
  51.     }
  52.     /**
  53.      * @param ContainerBuilder $container
  54.      * @param string $namespace
  55.      * @param string $bundlePath
  56.      * @param string $component
  57.      * @param Closure $callback
  58.      * @return void
  59.      */
  60.     protected function loadComponents(ContainerBuilder $container$namespace$bundlePath$component$callback, callable $aliasBuilder null): void
  61.     {
  62.         $fs = new Filesystem();
  63.         
  64.         if ($fs->exists($path $bundlePath $component)) {
  65.             $finder = new Finder();
  66.             foreach ($finder->files()->in($path)->depth('== 0') as $file) {
  67.                 $class $this->getClass($namespace$component$file->getFilename());
  68.                 $definition $container->hasDefinition($class) ? $container->getDefinition($class) : new Definition($class);
  69.                 $definition->setPublic(true);
  70.                 $definition->setAutowired(true);
  71.                 if (defined(sprintf('%s::SERVICE_ALIAS_ID'$class))) {
  72.                     $container->setAlias($class::SERVICE_ALIAS_ID$class);
  73.                     $alias $container->getAlias($class::SERVICE_ALIAS_ID);
  74.                     $alias->setPublic(true);
  75.                 }
  76.                 $callback($definition);
  77.                 $container->setDefinition($class$definition);
  78.                 if($aliasBuilder !== null) {
  79.                     if(null !== ($alias $aliasBuilder($definition$class) ?: null)) {
  80.                         $container->setAlias($alias$class);
  81.                         $aliasFromContainer $container->getAlias($alias);
  82.                         $aliasFromContainer->setPublic($alias instanceof Alias $alias->isPublic() : true);
  83.                     }
  84.                 }
  85.             }
  86.         }
  87.     }
  88.     /**
  89.      * @param ContainerBuilder $container
  90.      * @param string $tag
  91.      * @return array
  92.      */
  93.     protected function getServicesByTag(ContainerBuilder $container$tag): array
  94.     {
  95.         $services = array();
  96.         foreach ($container->findTaggedServiceIds($tag) as $serviceName=>$tags) {
  97.             $definition $container->getDefinition($serviceName);
  98.             $services[$definition->getClass()] = new Reference($definition->getClass());
  99.         }
  100.         return $services;
  101.     }
  102.     /**
  103.      * @param ContainerBuilder $container
  104.      * @param string $tag
  105.      * @param string $interfaceClass
  106.      * @return array
  107.      */
  108.     protected function getServicesByTagAndInterface(ContainerBuilder $container$tag$interfaceClass): array
  109.     {
  110.         $services = array();
  111.         foreach ($container->findTaggedServiceIds($tag) as $serviceName=>$tags) {
  112.             $definition $container->getDefinition($serviceName);
  113.             $reflection = new ReflectionClass($definition->getClass());
  114.             if ($reflection->implementsInterface($interfaceClass)) {
  115.                 $services[$definition->getClass()] = new Reference($definition->getClass());
  116.             }
  117.         }
  118.         return $services;
  119.     }
  120.     /**
  121.      * @param string $namespace
  122.      * @param string $component
  123.      * @param string $filename
  124.      * @return string
  125.      */
  126.     protected function getClass($namespace$component$filename): string
  127.     {
  128.         return str_replace('/''\\'$namespace '\\' $component '\\' str_replace('.php'''$filename));
  129.     }
  130.     /**
  131.      * @param string $filename
  132.      * @return string
  133.      */
  134.     protected function getKey($filename): string
  135.     {
  136.         $splitted preg_split('/(?=[A-Z])/'$filename);
  137.         array_shift($splitted);
  138.         return strtolower(str_replace('.php'''implode('_'$splitted)));
  139.     }
  140. }