4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Routing\Loader;
14 use Symfony\Component\Config\Loader\FileLoader;
15 use Symfony\Component\Config\Resource\FileResource;
16 use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
17 use Symfony\Component\Routing\RouteCollection;
20 * PhpFileLoader loads routes from a PHP file.
22 * The file must return a RouteCollection instance.
24 * @author Fabien Potencier <fabien@symfony.com>
26 class PhpFileLoader extends FileLoader
31 * @param string $file A PHP file path
32 * @param string|null $type The resource type
34 * @return RouteCollection A RouteCollection instance
36 public function load($file, $type = null)
38 $path = $this->locator->locate($file);
39 $this->setCurrentDir(\dirname($path));
41 // the closure forbids access to the private scope in the included file
43 $load = \Closure::bind(function ($file) use ($loader) {
45 }, null, ProtectedPhpFileLoader::class);
47 $result = $load($path);
49 if ($result instanceof \Closure) {
50 $collection = new RouteCollection();
51 $result(new RoutingConfigurator($collection, $this, $path, $file), $this);
53 $collection = $result;
56 $collection->addResource(new FileResource($path));
64 public function supports($resource, $type = null)
66 return \is_string($resource) && 'php' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'php' === $type);
73 final class ProtectedPhpFileLoader extends PhpFileLoader