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