PhpFileLoader.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. class PhpFileLoader extends FileLoader
  22. {
  23. /**
  24. * Loads a PHP file.
  25. *
  26. * @param string $file A PHP file path
  27. * @param string|null $type The resource type
  28. *
  29. * @return RouteCollection A RouteCollection instance
  30. */
  31. public function load($file, $type = null)
  32. {
  33. $path = $this->locator->locate($file);
  34. $this->setCurrentDir(dirname($path));
  35. $collection = self::includeFile($path, $this);
  36. $collection->addResource(new FileResource($path));
  37. return $collection;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function supports($resource, $type = null)
  43. {
  44. return is_string($resource) && 'php' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'php' === $type);
  45. }
  46. /**
  47. * Safe include. Used for scope isolation.
  48. *
  49. * @param string $file File to include
  50. * @param PhpFileLoader $loader the loader variable is exposed to the included file below
  51. *
  52. * @return RouteCollection
  53. */
  54. private static function includeFile($file, PhpFileLoader $loader)
  55. {
  56. return include $file;
  57. }
  58. }