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

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. use VisualMedia\LisaBundle\Controller\BaseController;
  18. use VisualMedia\FileBundle\Model\BaseUploadModel;
  19. /**
  20. * Compiler Pass
  21. */
  22. abstract class CompilerPass implements CompilerPassInterface
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function process(ContainerBuilder $container): void
  28. {
  29. $projectDir = $container->getParameter('kernel.project_dir');
  30. if (file_exists("{$projectDir}/vendor/visualmedia/lisa/lib/composer/LisaBundle")) {
  31. $this->processSingleBundle($container);
  32. }
  33. else {
  34. $this->processMultipleBundles($container);
  35. }
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function processSingleBundle(ContainerBuilder $container): void
  41. {
  42. $projectDir = $container->getParameter('kernel.project_dir');
  43. $bundles = $container->getParameter(Bundle::PARAMETER_BUNDLES);
  44. $fs = new Filesystem();
  45. foreach ($bundles as $name=>$namespace) {
  46. $bundleDir = str_replace('VisualMedia', '', $name);
  47. $namespaceDir = str_replace('\\', '/', $namespace);
  48. $bundlePath = sprintf('%s/vendor/visualmedia/lisa/lib/composer/%s/src/%s/', $projectDir, $bundleDir, $namespaceDir);
  49. $this->processBundle($container, $namespace, $bundlePath);
  50. }
  51. $bundlePath = sprintf('%s/src/', $projectDir);
  52. $this->processBundle($container, 'App', $bundlePath);
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function processMultipleBundles(ContainerBuilder $container): void
  58. {
  59. $projectDir = $container->getParameter('kernel.project_dir');
  60. $bundles = $container->getParameter(Bundle::PARAMETER_BUNDLES);
  61. $fs = new Filesystem();
  62. foreach ($bundles as $name=>$namespace) {
  63. $reflection = new ReflectionClass("{$namespace}\\{$name}");
  64. $bundleDir = $reflection->getFileName();
  65. $bundleDir = explode('/', $bundleDir);
  66. $bundleDir = $bundleDir[count($bundleDir) - 5];
  67. $bundleDir = strtolower(preg_replace('/\B([A-Z])/', '_$1', $bundleDir));
  68. $namespaceDir = str_replace('\\', '/', $namespace);
  69. $bundlePath = sprintf('%s/vendor/visualmedia/%s/src/%s/', $projectDir, $bundleDir, $namespaceDir);
  70. $this->processBundle($container, $namespace, $bundlePath);
  71. }
  72. $bundlePath = sprintf('%s/src/', $projectDir);
  73. $this->processBundle($container, 'App', $bundlePath);
  74. }
  75. /**
  76. * @param string $namespace
  77. * @param string $bundlePath
  78. * @return void
  79. */
  80. protected function processBundle(ContainerBuilder $container, $namespace, $bundlePath): void
  81. {
  82. // ...
  83. }
  84. /**
  85. * @param ContainerBuilder $container
  86. * @param string $namespace
  87. * @param string $bundlePath
  88. * @param string $component
  89. * @param Closure $callback
  90. * @return void
  91. */
  92. protected function loadComponents(ContainerBuilder $container, $namespace, $bundlePath, $component, $callback, callable $aliasBuilder = null): void
  93. {
  94. $fs = new Filesystem();
  95. if ($fs->exists($path = $bundlePath . $component)) {
  96. $finder = new Finder();
  97. foreach ($finder->files()->in($path)->depth('== 0') as $file) {
  98. $class = $this->getClass($namespace, $component, $file->getFilename());
  99. $definition = $container->hasDefinition($class) ? $container->getDefinition($class) : new Definition($class);
  100. $definition->setPublic(true);
  101. $definition->setAutowired(true);
  102. if (defined(sprintf('%s::SERVICE_ALIAS_ID', $class))) {
  103. $container->setAlias($class::SERVICE_ALIAS_ID, $class);
  104. $alias = $container->getAlias($class::SERVICE_ALIAS_ID);
  105. $alias->setPublic(true);
  106. }
  107. $callback($definition);
  108. $container->setDefinition($class, $definition);
  109. if($aliasBuilder !== null) {
  110. if(null !== ($alias = $aliasBuilder($definition, $class) ?: null)) {
  111. $container->setAlias($alias, $class);
  112. $aliasFromContainer = $container->getAlias($alias);
  113. $aliasFromContainer->setPublic($alias instanceof Alias ? $alias->isPublic() : true);
  114. }
  115. }
  116. }
  117. }
  118. }
  119. /**
  120. * @param ContainerBuilder $container
  121. * @param string $tag
  122. * @return array
  123. */
  124. protected function getServicesByTag(ContainerBuilder $container, $tag): array
  125. {
  126. $services = array();
  127. foreach ($container->findTaggedServiceIds($tag) as $serviceName=>$tags) {
  128. $definition = $container->getDefinition($serviceName);
  129. $services[$definition->getClass()] = new Reference($definition->getClass());
  130. }
  131. return $services;
  132. }
  133. /**
  134. * @param ContainerBuilder $container
  135. * @param string $tag
  136. * @param string $interfaceClass
  137. * @return array
  138. */
  139. protected function getServicesByTagAndInterface(ContainerBuilder $container, $tag, $interfaceClass): array
  140. {
  141. $services = array();
  142. foreach ($container->findTaggedServiceIds($tag) as $serviceName=>$tags) {
  143. $definition = $container->getDefinition($serviceName);
  144. $reflection = new ReflectionClass($definition->getClass());
  145. if ($reflection->implementsInterface($interfaceClass)) {
  146. $services[$definition->getClass()] = new Reference($definition->getClass());
  147. }
  148. }
  149. return $services;
  150. }
  151. /**
  152. * @param string $namespace
  153. * @param string $component
  154. * @param string $filename
  155. * @return string
  156. */
  157. protected function getClass($namespace, $component, $filename): string
  158. {
  159. return str_replace('/', '\\', $namespace . '\\' . $component . '\\' . str_replace('.php', '', $filename));
  160. }
  161. /**
  162. * @param string $filename
  163. * @return string
  164. */
  165. protected function getKey($filename): string
  166. {
  167. $splitted = preg_split('/(?=[A-Z])/', $filename);
  168. array_shift($splitted);
  169. return strtolower(str_replace('.php', '', implode('_', $splitted)));
  170. }
  171. }