9e009387dddd3d6f181a8d8c9b42a7a667ee98a6
[yaffs-website] / vendor / symfony / routing / Loader / PhpFileLoader.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Routing\Loader;
13
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;
18
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  */
26 class PhpFileLoader extends FileLoader
27 {
28     /**
29      * Loads a PHP file.
30      *
31      * @param string      $file A PHP file path
32      * @param string|null $type The resource type
33      *
34      * @return RouteCollection A RouteCollection instance
35      */
36     public function load($file, $type = null)
37     {
38         $path = $this->locator->locate($file);
39         $this->setCurrentDir(\dirname($path));
40
41         // the closure forbids access to the private scope in the included file
42         $loader = $this;
43         $load = \Closure::bind(function ($file) use ($loader) {
44             return include $file;
45         }, null, ProtectedPhpFileLoader::class);
46
47         $result = $load($path);
48
49         if ($result instanceof \Closure) {
50             $collection = new RouteCollection();
51             $result(new RoutingConfigurator($collection, $this, $path, $file), $this);
52         } else {
53             $collection = $result;
54         }
55
56         $collection->addResource(new FileResource($path));
57
58         return $collection;
59     }
60
61     /**
62      * {@inheritdoc}
63      */
64     public function supports($resource, $type = null)
65     {
66         return \is_string($resource) && 'php' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'php' === $type);
67     }
68 }
69
70 /**
71  * @internal
72  */
73 final class ProtectedPhpFileLoader extends PhpFileLoader
74 {
75 }