AnnotationConfigurationPass.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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\Compiler;
  18. use Symfony\Component\DependencyInjection\Alias;
  19. use JMS\DiExtraBundle\Exception\RuntimeException;
  20. use JMS\DiExtraBundle\Config\ServiceFilesResource;
  21. use Symfony\Component\Config\Resource\FileResource;
  22. use Symfony\Component\DependencyInjection\DefinitionDecorator;
  23. use Symfony\Component\DependencyInjection\Definition;
  24. use JMS\DiExtraBundle\Finder\PatternFinder;
  25. use Symfony\Component\HttpKernel\KernelInterface;
  26. use Symfony\Component\DependencyInjection\ContainerBuilder;
  27. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  28. class AnnotationConfigurationPass implements CompilerPassInterface
  29. {
  30. private $kernel;
  31. private $finder;
  32. public function __construct(KernelInterface $kernel)
  33. {
  34. $this->kernel = $kernel;
  35. $this->finder = new PatternFinder('JMS\DiExtraBundle\Annotation');
  36. }
  37. public function process(ContainerBuilder $container)
  38. {
  39. $reader = $container->get('annotation_reader');
  40. $factory = $container->get('jms_di_extra.metadata.metadata_factory');
  41. $converter = $container->get('jms_di_extra.metadata.converter');
  42. $directories = $this->getScanDirectories($container);
  43. if (!$directories) {
  44. $container->getCompiler()->addLogMessage('No directories configured for AnnotationConfigurationPass.');
  45. return;
  46. }
  47. $files = $this->finder->findFiles($directories);
  48. $container->addResource(new ServiceFilesResource($files, $directories));
  49. foreach ($files as $file) {
  50. $container->addResource(new FileResource($file));
  51. require_once $file;
  52. $className = $this->getClassName($file);
  53. if (null === $metadata = $factory->getMetadataForClass($className)) {
  54. continue;
  55. }
  56. if (null === $metadata->getOutsideClassMetadata()->id) {
  57. continue;
  58. }
  59. foreach ($converter->convert($metadata) as $id => $definition) {
  60. $container->setDefinition($id, $definition);
  61. }
  62. }
  63. }
  64. private function getScanDirectories(ContainerBuilder $c)
  65. {
  66. $bundles = $this->kernel->getBundles();
  67. $scanBundles = $c->getParameter('jms_di_extra.bundles');
  68. $scanAllBundles = $c->getParameter('jms_di_extra.all_bundles');
  69. $directories = $c->getParameter('jms_di_extra.directories');
  70. foreach ($bundles as $name => $bundle) {
  71. if (!$scanAllBundles && !in_array($name, $scanBundles, true)) {
  72. continue;
  73. }
  74. if ('JMSDiExtraBundle' === $name) {
  75. continue;
  76. }
  77. $directories[] = $bundle->getPath();
  78. }
  79. return $directories;
  80. }
  81. /**
  82. * Only supports one namespaced class per file
  83. *
  84. * @throws \RuntimeException if the class name cannot be extracted
  85. * @param string $filename
  86. * @return string the fully qualified class name
  87. */
  88. private function getClassName($filename)
  89. {
  90. $src = file_get_contents($filename);
  91. if (!preg_match('/\bnamespace\s+([^;]+);/s', $src, $match)) {
  92. throw new RuntimeException(sprintf('Namespace could not be determined for file "%s".', $filename));
  93. }
  94. $namespace = $match[1];
  95. if (!preg_match('/\bclass\s+([^\s]+)\s+(?:extends|implements|{)/s', $src, $match)) {
  96. throw new RuntimeException(sprintf('Could not extract class name from file "%s".', $filename));
  97. }
  98. return $namespace.'\\'.$match[1];
  99. }
  100. }