Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 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
 * @author Nicolas grekas <p@tchwork.com>
26
 * @author Jules Pietri <jules@heahprod.com>
27
 */
28
class PhpFileLoader extends FileLoader
29
{
30
    /**
31
     * Loads a PHP file.
32
     *
33
     * @param string      $file A PHP file path
34
     * @param string|null $type The resource type
35
     *
36
     * @return RouteCollection A RouteCollection instance
37
     */
38
    public function load($file, string $type = null)
39
    {
40
        $path = $this->locator->locate($file);
41
        $this->setCurrentDir(\dirname($path));
42
 
43
        // the closure forbids access to the private scope in the included file
44
        $loader = $this;
45
        $load = \Closure::bind(static function ($file) use ($loader) {
46
            return include $file;
47
        }, null, ProtectedPhpFileLoader::class);
48
 
49
        $result = $load($path);
50
 
51
        if (\is_object($result) && \is_callable($result)) {
52
            $collection = $this->callConfigurator($result, $path, $file);
53
        } else {
54
            $collection = $result;
55
        }
56
 
57
        $collection->addResource(new FileResource($path));
58
 
59
        return $collection;
60
    }
61
 
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function supports($resource, string $type = null)
66
    {
67
        return \is_string($resource) && 'php' === pathinfo($resource, \PATHINFO_EXTENSION) && (!$type || 'php' === $type);
68
    }
69
 
70
    protected function callConfigurator(callable $result, string $path, string $file): RouteCollection
71
    {
72
        $collection = new RouteCollection();
73
 
74
        $result(new RoutingConfigurator($collection, $this, $path, $file));
75
 
76
        return $collection;
77
    }
78
}
79
 
80
/**
81
 * @internal
82
 */
83
final class ProtectedPhpFileLoader extends PhpFileLoader
84
{
85
}