PhpFileLoader.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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\Loader\LoaderResolver;
  13. use Symfony\Component\Config\Resource\FileResource;
  14. use Symfony\Component\Routing\Exception\InvalidArgumentException;
  15. use Symfony\Component\Routing\Loader\Configurator\Routes;
  16. use Symfony\Component\Routing\Loader\Configurator\RoutesReference;
  17. use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
  18. use Symfony\Component\Routing\RouteCollection;
  19. /**
  20. * PhpFileLoader loads routes from a PHP file.
  21. *
  22. * The file must return a RouteCollection instance.
  23. *
  24. * @author Fabien Potencier <fabien@symfony.com>
  25. * @author Nicolas grekas <p@tchwork.com>
  26. * @author Jules Pietri <jules@heahprod.com>
  27. */
  28. class PhpFileLoader extends FileLoader
  29. {
  30. /**
  31. * Loads a PHP file.
  32. */
  33. public function load(mixed $file, ?string $type = null): RouteCollection
  34. {
  35. $path = $this->locator->locate($file);
  36. $this->setCurrentDir(\dirname($path));
  37. // Expose RoutesReference::config() as Routes::config()
  38. if (!class_exists(Routes::class)) {
  39. class_alias(RoutesReference::class, Routes::class);
  40. }
  41. // the closure forbids access to the private scope in the included file
  42. $loader = $this;
  43. $load = \Closure::bind(static function ($file) use ($loader) {
  44. return include $file;
  45. }, null, null);
  46. if (1 === $result = $load($path)) {
  47. $result = null;
  48. }
  49. if (\is_object($result) && \is_callable($result)) {
  50. $collection = $this->callConfigurator($result, $path, $file);
  51. } elseif (\is_array($result)) {
  52. $collection = new RouteCollection();
  53. $loader = new YamlFileLoader($this->locator, $this->env);
  54. $loader->setResolver($this->resolver ?? new LoaderResolver([$this]));
  55. (new \ReflectionMethod(YamlFileLoader::class, 'loadContent'))->invoke($loader, $collection, $result, $path, $file);
  56. } elseif (!($collection = $result) instanceof RouteCollection) {
  57. throw new InvalidArgumentException(\sprintf('The return value in config file "%s" is expected to be a RouteCollection, an array or a configurator callable, but got "%s".', $path, get_debug_type($result)));
  58. }
  59. $collection->addResource(new FileResource($path));
  60. return $collection;
  61. }
  62. public function supports(mixed $resource, ?string $type = null): bool
  63. {
  64. return \is_string($resource) && 'php' === pathinfo($resource, \PATHINFO_EXTENSION) && (!$type || 'php' === $type);
  65. }
  66. protected function callConfigurator(callable $callback, string $path, string $file): RouteCollection
  67. {
  68. $collection = new RouteCollection();
  69. $callback(new RoutingConfigurator($collection, $this, $path, $file, $this->env));
  70. return $collection;
  71. }
  72. }