AddAnnotatedClassesToCachePass.php 4.3 KB

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