No Description

AddAnnotatedClassesToCachePass.php 4.2KB

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