PhpFileLoader.php 1.6 KB

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