AnnotationFileLoader.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 \RuntimeException('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 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, $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;
  53. }
  54. $collection->addResource(new FileResource($path));
  55. $collection->addCollection($this->loader->load($class, $type));
  56. }
  57. if (\PHP_VERSION_ID >= 70000) {
  58. // PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098
  59. gc_mem_caches();
  60. }
  61. return $collection;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function supports($resource, $type = null)
  67. {
  68. return \is_string($resource) && 'php' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'annotation' === $type);
  69. }
  70. /**
  71. * Returns the full class name for the first class in the file.
  72. *
  73. * @param string $file A PHP file path
  74. *
  75. * @return string|false Full class name if found, false otherwise
  76. */
  77. protected function findClass($file)
  78. {
  79. $class = false;
  80. $namespace = false;
  81. $tokens = token_get_all(file_get_contents($file));
  82. if (1 === \count($tokens) && T_INLINE_HTML === $tokens[0][0]) {
  83. 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));
  84. }
  85. for ($i = 0; isset($tokens[$i]); ++$i) {
  86. $token = $tokens[$i];
  87. if (!isset($token[1])) {
  88. continue;
  89. }
  90. if (true === $class && T_STRING === $token[0]) {
  91. return $namespace.'\\'.$token[1];
  92. }
  93. if (true === $namespace && T_STRING === $token[0]) {
  94. $namespace = $token[1];
  95. while (isset($tokens[++$i][1]) && \in_array($tokens[$i][0], [T_NS_SEPARATOR, T_STRING])) {
  96. $namespace .= $tokens[$i][1];
  97. }
  98. $token = $tokens[$i];
  99. }
  100. if (T_CLASS === $token[0]) {
  101. // Skip usage of ::class constant and anonymous classes
  102. $skipClassToken = false;
  103. for ($j = $i - 1; $j > 0; --$j) {
  104. if (!isset($tokens[$j][1])) {
  105. break;
  106. }
  107. if (T_DOUBLE_COLON === $tokens[$j][0] || T_NEW === $tokens[$j][0]) {
  108. $skipClassToken = true;
  109. break;
  110. } elseif (!\in_array($tokens[$j][0], [T_WHITESPACE, T_DOC_COMMENT, T_COMMENT])) {
  111. break;
  112. }
  113. }
  114. if (!$skipClassToken) {
  115. $class = true;
  116. }
  117. }
  118. if (T_NAMESPACE === $token[0]) {
  119. $namespace = true;
  120. }
  121. }
  122. return false;
  123. }
  124. }