No Description

PhpFileLoader.php 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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\Loader\FileLoader;
  12. use Symfony\Component\Config\Resource\FileResource;
  13. use Symfony\Component\Routing\RouteCollection;
  14. /**
  15. * PhpFileLoader loads routes from a PHP file.
  16. *
  17. * The file must return a RouteCollection instance.
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. *
  21. * @api
  22. */
  23. class PhpFileLoader extends FileLoader
  24. {
  25. /**
  26. * Loads a PHP file.
  27. *
  28. * @param string $file A PHP file path
  29. * @param string|null $type The resource type
  30. *
  31. * @return RouteCollection A RouteCollection instance
  32. *
  33. * @api
  34. */
  35. public function load($file, $type = null)
  36. {
  37. $path = $this->locator->locate($file);
  38. $this->setCurrentDir(dirname($path));
  39. $collection = self::includeFile($path, $this);
  40. $collection->addResource(new FileResource($path));
  41. return $collection;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. *
  46. * @api
  47. */
  48. public function supports($resource, $type = null)
  49. {
  50. return is_string($resource) && 'php' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'php' === $type);
  51. }
  52. /**
  53. * Safe include. Used for scope isolation.
  54. *
  55. * @param string $file File to include
  56. * @param PhpFileLoader $loader the loader variable is exposed to the included file below
  57. *
  58. * @return RouteCollection
  59. */
  60. private static function includeFile($file, PhpFileLoader $loader)
  61. {
  62. return include $file;
  63. }
  64. }