AnnotationFileLoader.php 4.3 KB

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