AttributeFileLoader.php 4.7 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\Routing\Loader;
  11. use Symfony\Component\Config\FileLocatorInterface;
  12. use Symfony\Component\Config\Loader\FileLoader;
  13. use Symfony\Component\Config\Resource\FileResource;
  14. use Symfony\Component\Routing\RouteCollection;
  15. /**
  16. * AttributeFileLoader loads routing information from attributes set
  17. * on a PHP class and its methods.
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. * @author Alexandre Daubois <alex.daubois@gmail.com>
  21. */
  22. class AttributeFileLoader extends FileLoader
  23. {
  24. protected $loader;
  25. public function __construct(FileLocatorInterface $locator, AttributeClassLoader $loader)
  26. {
  27. if (!\function_exists('token_get_all')) {
  28. throw new \LogicException('The Tokenizer extension is required for the routing attribute loader.');
  29. }
  30. parent::__construct($locator);
  31. $this->loader = $loader;
  32. }
  33. /**
  34. * Loads from attributes from a file.
  35. *
  36. * @throws \InvalidArgumentException When the file does not exist or its routes cannot be parsed
  37. */
  38. public function load(mixed $file, ?string $type = null): ?RouteCollection
  39. {
  40. $path = $this->locator->locate($file);
  41. $collection = new RouteCollection();
  42. if ($class = $this->findClass($path)) {
  43. $refl = new \ReflectionClass($class);
  44. if ($refl->isAbstract()) {
  45. return null;
  46. }
  47. $collection->addResource(new FileResource($path));
  48. $collection->addCollection($this->loader->load($class, $type));
  49. }
  50. gc_mem_caches();
  51. return $collection;
  52. }
  53. public function supports(mixed $resource, ?string $type = null): bool
  54. {
  55. if ('annotation' === $type) {
  56. trigger_deprecation('symfony/routing', '6.4', 'The "annotation" route type is deprecated, use the "attribute" route type instead.');
  57. }
  58. return \is_string($resource) && 'php' === pathinfo($resource, \PATHINFO_EXTENSION) && (!$type || \in_array($type, ['annotation', 'attribute'], true));
  59. }
  60. /**
  61. * Returns the full class name for the first class in the file.
  62. */
  63. protected function findClass(string $file): string|false
  64. {
  65. $class = false;
  66. $namespace = false;
  67. $tokens = token_get_all(file_get_contents($file));
  68. if (1 === \count($tokens) && \T_INLINE_HTML === $tokens[0][0]) {
  69. throw new \InvalidArgumentException(\sprintf('The file "%s" does not contain PHP code. Did you forget to add the "<?php" start tag at the beginning of the file?', $file));
  70. }
  71. $nsTokens = [\T_NS_SEPARATOR => true, \T_STRING => true];
  72. if (\defined('T_NAME_QUALIFIED')) {
  73. $nsTokens[\T_NAME_QUALIFIED] = true;
  74. }
  75. for ($i = 0; isset($tokens[$i]); ++$i) {
  76. $token = $tokens[$i];
  77. if (!isset($token[1])) {
  78. continue;
  79. }
  80. if (true === $class && \T_STRING === $token[0]) {
  81. return $namespace.'\\'.$token[1];
  82. }
  83. if (true === $namespace && isset($nsTokens[$token[0]])) {
  84. $namespace = $token[1];
  85. while (isset($tokens[++$i][1], $nsTokens[$tokens[$i][0]])) {
  86. $namespace .= $tokens[$i][1];
  87. }
  88. $token = $tokens[$i];
  89. }
  90. if (\T_CLASS === $token[0]) {
  91. // Skip usage of ::class constant and anonymous classes
  92. $skipClassToken = false;
  93. for ($j = $i - 1; $j > 0; --$j) {
  94. if (!isset($tokens[$j][1])) {
  95. if ('(' === $tokens[$j] || ',' === $tokens[$j]) {
  96. $skipClassToken = true;
  97. }
  98. break;
  99. }
  100. if (\T_DOUBLE_COLON === $tokens[$j][0] || \T_NEW === $tokens[$j][0]) {
  101. $skipClassToken = true;
  102. break;
  103. } elseif (!\in_array($tokens[$j][0], [\T_WHITESPACE, \T_DOC_COMMENT, \T_COMMENT])) {
  104. break;
  105. }
  106. }
  107. if (!$skipClassToken) {
  108. $class = true;
  109. }
  110. }
  111. if (\T_NAMESPACE === $token[0]) {
  112. $namespace = true;
  113. }
  114. }
  115. return false;
  116. }
  117. }
  118. if (!class_exists(AnnotationFileLoader::class, false)) {
  119. class_alias(AttributeFileLoader::class, AnnotationFileLoader::class);
  120. }