AddAnnotatedClassesToCachePass.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\DependencyInjection;
  11. use Composer\Autoload\ClassLoader;
  12. use Symfony\Component\Debug\DebugClassLoader;
  13. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. use Symfony\Component\HttpKernel\Kernel;
  16. /**
  17. * Sets the classes to compile in the cache for the container.
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. */
  21. class AddAnnotatedClassesToCachePass implements CompilerPassInterface
  22. {
  23. private $kernel;
  24. public function __construct(Kernel $kernel)
  25. {
  26. $this->kernel = $kernel;
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function process(ContainerBuilder $container)
  32. {
  33. $annotatedClasses = $this->kernel->getAnnotatedClassesToCompile();
  34. foreach ($container->getExtensions() as $extension) {
  35. if ($extension instanceof Extension) {
  36. $annotatedClasses = array_merge($annotatedClasses, $extension->getAnnotatedClassesToCompile());
  37. }
  38. }
  39. $existingClasses = $this->getClassesInComposerClassMaps();
  40. $annotatedClasses = $container->getParameterBag()->resolveValue($annotatedClasses);
  41. $this->kernel->setAnnotatedClassCache($this->expandClasses($annotatedClasses, $existingClasses));
  42. }
  43. /**
  44. * Expands the given class patterns using a list of existing classes.
  45. *
  46. * @param array $patterns The class patterns to expand
  47. * @param array $classes The existing classes to match against the patterns
  48. *
  49. * @return array A list of classes derived from the patterns
  50. */
  51. private function expandClasses(array $patterns, array $classes)
  52. {
  53. $expanded = [];
  54. // Explicit classes declared in the patterns are returned directly
  55. foreach ($patterns as $key => $pattern) {
  56. if ('\\' !== substr($pattern, -1) && false === strpos($pattern, '*')) {
  57. unset($patterns[$key]);
  58. $expanded[] = ltrim($pattern, '\\');
  59. }
  60. }
  61. // Match patterns with the classes list
  62. $regexps = $this->patternsToRegexps($patterns);
  63. foreach ($classes as $class) {
  64. $class = ltrim($class, '\\');
  65. if ($this->matchAnyRegexps($class, $regexps)) {
  66. $expanded[] = $class;
  67. }
  68. }
  69. return array_unique($expanded);
  70. }
  71. private function getClassesInComposerClassMaps()
  72. {
  73. $classes = [];
  74. foreach (spl_autoload_functions() as $function) {
  75. if (!\is_array($function)) {
  76. continue;
  77. }
  78. if ($function[0] instanceof DebugClassLoader) {
  79. $function = $function[0]->getClassLoader();
  80. }
  81. if (\is_array($function) && $function[0] instanceof ClassLoader) {
  82. $classes += array_filter($function[0]->getClassMap());
  83. }
  84. }
  85. return array_keys($classes);
  86. }
  87. private function patternsToRegexps($patterns)
  88. {
  89. $regexps = [];
  90. foreach ($patterns as $pattern) {
  91. // Escape user input
  92. $regex = preg_quote(ltrim($pattern, '\\'));
  93. // Wildcards * and **
  94. $regex = strtr($regex, ['\\*\\*' => '.*?', '\\*' => '[^\\\\]*?']);
  95. // If this class does not end by a slash, anchor the end
  96. if ('\\' !== substr($regex, -1)) {
  97. $regex .= '$';
  98. }
  99. $regexps[] = '{^\\\\'.$regex.'}';
  100. }
  101. return $regexps;
  102. }
  103. private function matchAnyRegexps($class, $regexps)
  104. {
  105. $blacklisted = false !== strpos($class, 'Test');
  106. foreach ($regexps as $regex) {
  107. if ($blacklisted && false === strpos($regex, 'Test')) {
  108. continue;
  109. }
  110. if (preg_match($regex, '\\'.$class)) {
  111. return true;
  112. }
  113. }
  114. return false;
  115. }
  116. }