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@0
|
16 use Symfony\Component\Routing\RouteCollection;
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * PhpFileLoader loads routes from a PHP file.
|
Chris@0
|
20 *
|
Chris@0
|
21 * The file must return a RouteCollection instance.
|
Chris@0
|
22 *
|
Chris@0
|
23 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
24 */
|
Chris@0
|
25 class PhpFileLoader extends FileLoader
|
Chris@0
|
26 {
|
Chris@0
|
27 /**
|
Chris@0
|
28 * Loads a PHP file.
|
Chris@0
|
29 *
|
Chris@0
|
30 * @param string $file A PHP file path
|
Chris@0
|
31 * @param string|null $type The resource type
|
Chris@0
|
32 *
|
Chris@0
|
33 * @return RouteCollection A RouteCollection instance
|
Chris@0
|
34 */
|
Chris@0
|
35 public function load($file, $type = null)
|
Chris@0
|
36 {
|
Chris@0
|
37 $path = $this->locator->locate($file);
|
Chris@0
|
38 $this->setCurrentDir(dirname($path));
|
Chris@0
|
39
|
Chris@0
|
40 $collection = self::includeFile($path, $this);
|
Chris@0
|
41 $collection->addResource(new FileResource($path));
|
Chris@0
|
42
|
Chris@0
|
43 return $collection;
|
Chris@0
|
44 }
|
Chris@0
|
45
|
Chris@0
|
46 /**
|
Chris@0
|
47 * {@inheritdoc}
|
Chris@0
|
48 */
|
Chris@0
|
49 public function supports($resource, $type = null)
|
Chris@0
|
50 {
|
Chris@0
|
51 return is_string($resource) && 'php' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'php' === $type);
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 /**
|
Chris@0
|
55 * Safe include. Used for scope isolation.
|
Chris@0
|
56 *
|
Chris@0
|
57 * @param string $file File to include
|
Chris@0
|
58 * @param PhpFileLoader $loader the loader variable is exposed to the included file below
|
Chris@0
|
59 *
|
Chris@0
|
60 * @return RouteCollection
|
Chris@0
|
61 */
|
Chris@0
|
62 private static function includeFile($file, PhpFileLoader $loader)
|
Chris@0
|
63 {
|
Chris@0
|
64 return include $file;
|
Chris@0
|
65 }
|
Chris@0
|
66 }
|