AnnotationFileLoader.php 3.5 KB

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