No Description

AnnotationFileLoader.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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\Routing\RouteCollection;
  12. use Symfony\Component\Config\Resource\FileResource;
  13. use Symfony\Component\Config\Loader\FileLoader;
  14. use Symfony\Component\Config\FileLocatorInterface;
  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. * Constructor.
  26. *
  27. * @param FileLocatorInterface $locator A FileLocator instance
  28. * @param AnnotationClassLoader $loader An AnnotationClassLoader instance
  29. *
  30. * @throws \RuntimeException
  31. */
  32. public function __construct(FileLocatorInterface $locator, AnnotationClassLoader $loader)
  33. {
  34. if (!function_exists('token_get_all')) {
  35. throw new \RuntimeException('The Tokenizer extension is required for the routing annotation loaders.');
  36. }
  37. parent::__construct($locator);
  38. $this->loader = $loader;
  39. }
  40. /**
  41. * Loads from annotations from a file.
  42. *
  43. * @param string $file A PHP file path
  44. * @param string|null $type The resource type
  45. *
  46. * @return RouteCollection A RouteCollection instance
  47. *
  48. * @throws \InvalidArgumentException When the file does not exist or its routes cannot be parsed
  49. */
  50. public function load($file, $type = null)
  51. {
  52. $path = $this->locator->locate($file);
  53. $collection = new RouteCollection();
  54. if ($class = $this->findClass($path)) {
  55. $collection->addResource(new FileResource($path));
  56. $collection->addCollection($this->loader->load($class, $type));
  57. }
  58. return $collection;
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function supports($resource, $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. * @param string $file A PHP file path
  71. *
  72. * @return string|false Full class name if found, false otherwise
  73. */
  74. protected function findClass($file)
  75. {
  76. $class = false;
  77. $namespace = false;
  78. $tokens = token_get_all(file_get_contents($file));
  79. for ($i = 0, $count = count($tokens); $i < $count; $i++) {
  80. $token = $tokens[$i];
  81. if (!is_array($token)) {
  82. continue;
  83. }
  84. if (true === $class && T_STRING === $token[0]) {
  85. return $namespace.'\\'.$token[1];
  86. }
  87. if (true === $namespace && T_STRING === $token[0]) {
  88. $namespace = '';
  89. do {
  90. $namespace .= $token[1];
  91. $token = $tokens[++$i];
  92. } while ($i < $count && is_array($token) && in_array($token[0], array(T_NS_SEPARATOR, T_STRING)));
  93. }
  94. if (T_CLASS === $token[0]) {
  95. $class = true;
  96. }
  97. if (T_NAMESPACE === $token[0]) {
  98. $namespace = true;
  99. }
  100. }
  101. return false;
  102. }
  103. }