Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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\Loader;
15
use Symfony\Component\Config\Resource\FileResource;
16
use Symfony\Component\Routing\RouteCollection;
17
 
18
/**
19
 * A route loader that calls a method on an object to load the routes.
20
 *
21
 * @author Ryan Weaver <ryan@knpuniversity.com>
22
 */
23
abstract class ObjectLoader extends Loader
24
{
25
    /**
26
     * Returns the object that the method will be called on to load routes.
27
     *
28
     * For example, if your application uses a service container,
29
     * the $id may be a service id.
30
     */
31
    abstract protected function getObject(string $id): object;
32
 
33
    /**
34
     * Calls the object method that will load the routes.
35
     */
36
    public function load(mixed $resource, string $type = null): RouteCollection
37
    {
38
        if (!preg_match('/^[^\:]+(?:::(?:[^\:]+))?$/', $resource)) {
39
            throw new \InvalidArgumentException(sprintf('Invalid resource "%s" passed to the %s route loader: use the format "object_id::method" or "object_id" if your object class has an "__invoke" method.', $resource, \is_string($type) ? '"'.$type.'"' : 'object'));
40
        }
41
 
42
        $parts = explode('::', $resource);
43
        $method = $parts[1] ?? '__invoke';
44
 
45
        $loaderObject = $this->getObject($parts[0]);
46
 
47
        if (!\is_object($loaderObject)) {
48
            throw new \TypeError(sprintf('"%s:getObject()" must return an object: "%s" returned.', static::class, get_debug_type($loaderObject)));
49
        }
50
 
51
        if (!\is_callable([$loaderObject, $method])) {
52
            throw new \BadMethodCallException(sprintf('Method "%s" not found on "%s" when importing routing resource "%s".', $method, get_debug_type($loaderObject), $resource));
53
        }
54
 
55
        $routeCollection = $loaderObject->$method($this, $this->env);
56
 
57
        if (!$routeCollection instanceof RouteCollection) {
58
            $type = get_debug_type($routeCollection);
59
 
60
            throw new \LogicException(sprintf('The "%s::%s()" method must return a RouteCollection: "%s" returned.', get_debug_type($loaderObject), $method, $type));
61
        }
62
 
63
        // make the object file tracked so that if it changes, the cache rebuilds
64
        $this->addClassResource(new \ReflectionClass($loaderObject), $routeCollection);
65
 
66
        return $routeCollection;
67
    }
68
 
69
    private function addClassResource(\ReflectionClass $class, RouteCollection $collection)
70
    {
71
        do {
72
            if (is_file($class->getFileName())) {
73
                $collection->addResource(new FileResource($class->getFileName()));
74
            }
75
        } while ($class = $class->getParentClass());
76
    }
77
}