PhpFileLoader.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. // the loader variable is exposed to the included file below
  38. $loader = $this;
  39. $path = $this->locator->locate($file);
  40. $this->setCurrentDir(dirname($path));
  41. $collection = include $path;
  42. $collection->addResource(new FileResource($path));
  43. return $collection;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. *
  48. * @api
  49. */
  50. public function supports($resource, $type = null)
  51. {
  52. return is_string($resource) && 'php' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'php' === $type);
  53. }
  54. }