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