You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
4.6 KiB

3 years ago
  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. /**
  25. * @throws \RuntimeException
  26. */
  27. public function __construct(FileLocatorInterface $locator, AnnotationClassLoader $loader)
  28. {
  29. if (!\function_exists('token_get_all')) {
  30. throw new \LogicException('The Tokenizer extension is required for the routing annotation loaders.');
  31. }
  32. parent::__construct($locator);
  33. $this->loader = $loader;
  34. }
  35. /**
  36. * Loads from annotations from a file.
  37. *
  38. * @param string $file A PHP file path
  39. * @param string|null $type The resource type
  40. *
  41. * @return RouteCollection|null A RouteCollection instance
  42. *
  43. * @throws \InvalidArgumentException When the file does not exist or its routes cannot be parsed
  44. */
  45. public function load($file, string $type = null)
  46. {
  47. $path = $this->locator->locate($file);
  48. $collection = new RouteCollection();
  49. if ($class = $this->findClass($path)) {
  50. $refl = new \ReflectionClass($class);
  51. if ($refl->isAbstract()) {
  52. return null;
  53. }
  54. $collection->addResource(new FileResource($path));
  55. $collection->addCollection($this->loader->load($class, $type));
  56. }
  57. gc_mem_caches();
  58. return $collection;
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function supports($resource, string $type = null)
  64. {
  65. return \is_string($resource) && 'php' === pathinfo($resource, \PATHINFO_EXTENSION) && (!$type || 'annotation' === $type);
  66. }
  67. /**
  68. * Returns the full class name for the first class in the file.
  69. *
  70. * @return string|false Full class name if found, false otherwise
  71. */
  72. protected function findClass(string $file)
  73. {
  74. $class = false;
  75. $namespace = false;
  76. $tokens = token_get_all(file_get_contents($file));
  77. if (1 === \count($tokens) && \T_INLINE_HTML === $tokens[0][0]) {
  78. 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));
  79. }
  80. $nsTokens = [\T_NS_SEPARATOR => true, \T_STRING => true];
  81. if (\defined('T_NAME_QUALIFIED')) {
  82. $nsTokens[\T_NAME_QUALIFIED] = true;
  83. }
  84. for ($i = 0; isset($tokens[$i]); ++$i) {
  85. $token = $tokens[$i];
  86. if (!isset($token[1])) {
  87. continue;
  88. }
  89. if (true === $class && \T_STRING === $token[0]) {
  90. return $namespace.'\\'.$token[1];
  91. }
  92. if (true === $namespace && isset($nsTokens[$token[0]])) {
  93. $namespace = $token[1];
  94. while (isset($tokens[++$i][1], $nsTokens[$tokens[$i][0]])) {
  95. $namespace .= $tokens[$i][1];
  96. }
  97. $token = $tokens[$i];
  98. }
  99. if (\T_CLASS === $token[0]) {
  100. // Skip usage of ::class constant and anonymous classes
  101. $skipClassToken = false;
  102. for ($j = $i - 1; $j > 0; --$j) {
  103. if (!isset($tokens[$j][1])) {
  104. if ('(' === $tokens[$j] || ',' === $tokens[$j]) {
  105. $skipClassToken = true;
  106. }
  107. break;
  108. }
  109. if (\T_DOUBLE_COLON === $tokens[$j][0] || \T_NEW === $tokens[$j][0]) {
  110. $skipClassToken = true;
  111. break;
  112. } elseif (!\in_array($tokens[$j][0], [\T_WHITESPACE, \T_DOC_COMMENT, \T_COMMENT])) {
  113. break;
  114. }
  115. }
  116. if (!$skipClassToken) {
  117. $class = true;
  118. }
  119. }
  120. if (\T_NAMESPACE === $token[0]) {
  121. $namespace = true;
  122. }
  123. }
  124. return false;
  125. }
  126. }