PhpFileLoader.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. try {
  47. if (1 === $result = $load($path)) {
  48. $result = null;
  49. }
  50. } catch (\Error $e) {
  51. $load = \Closure::bind(static function ($file) use ($loader) {
  52. return include $file;
  53. }, null, ProtectedPhpFileLoader::class);
  54. if (1 === $result = $load($path)) {
  55. $result = null;
  56. }
  57. trigger_deprecation('symfony/routing', '7.4', 'Accessing the internal scope of the loader in config files is deprecated, use only its public API instead in "%s" on line %d.', $e->getFile(), $e->getLine());
  58. }
  59. if (\is_object($result) && \is_callable($result)) {
  60. $collection = $this->callConfigurator($result, $path, $file);
  61. } elseif (\is_array($result)) {
  62. $collection = new RouteCollection();
  63. $loader = new YamlFileLoader($this->locator, $this->env);
  64. $loader->setResolver($this->resolver ?? new LoaderResolver([$this]));
  65. (new \ReflectionMethod(YamlFileLoader::class, 'loadContent'))->invoke($loader, $collection, $result, $path, $file);
  66. } elseif (!($collection = $result) instanceof RouteCollection) {
  67. 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)));
  68. }
  69. $collection->addResource(new FileResource($path));
  70. return $collection;
  71. }
  72. public function supports(mixed $resource, ?string $type = null): bool
  73. {
  74. return \is_string($resource) && 'php' === pathinfo($resource, \PATHINFO_EXTENSION) && (!$type || 'php' === $type);
  75. }
  76. protected function callConfigurator(callable $callback, string $path, string $file): RouteCollection
  77. {
  78. $collection = new RouteCollection();
  79. $callback(new RoutingConfigurator($collection, $this, $path, $file, $this->env));
  80. return $collection;
  81. }
  82. }
  83. /**
  84. * @internal
  85. */
  86. final class ProtectedPhpFileLoader extends PhpFileLoader
  87. {
  88. }