Yaffs site version 1.1
[yaffs-website] / vendor / symfony / routing / Loader / ObjectRouteLoader.php
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 ObjectRouteLoader 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      * @param string $id
32      *
33      * @return object
34      */
35     abstract protected function getServiceObject($id);
36
37     /**
38      * Calls the service that will load the routes.
39      *
40      * @param mixed       $resource Some value that will resolve to a callable
41      * @param string|null $type     The resource type
42      *
43      * @return RouteCollection
44      */
45     public function load($resource, $type = null)
46     {
47         $parts = explode(':', $resource);
48         if (count($parts) != 2) {
49             throw new \InvalidArgumentException(sprintf('Invalid resource "%s" passed to the "service" route loader: use the format "service_name:methodName"', $resource));
50         }
51
52         $serviceString = $parts[0];
53         $method = $parts[1];
54
55         $loaderObject = $this->getServiceObject($serviceString);
56
57         if (!is_object($loaderObject)) {
58             throw new \LogicException(sprintf('%s:getServiceObject() must return an object: %s returned', get_class($this), gettype($loaderObject)));
59         }
60
61         if (!method_exists($loaderObject, $method)) {
62             throw new \BadMethodCallException(sprintf('Method "%s" not found on "%s" when importing routing resource "%s"', $method, get_class($loaderObject), $resource));
63         }
64
65         $routeCollection = call_user_func(array($loaderObject, $method), $this);
66
67         if (!$routeCollection instanceof RouteCollection) {
68             $type = is_object($routeCollection) ? get_class($routeCollection) : gettype($routeCollection);
69
70             throw new \LogicException(sprintf('The %s::%s method must return a RouteCollection: %s returned', get_class($loaderObject), $method, $type));
71         }
72
73         // make the service file tracked so that if it changes, the cache rebuilds
74         $this->addClassResource(new \ReflectionClass($loaderObject), $routeCollection);
75
76         return $routeCollection;
77     }
78
79     /**
80      * {@inheritdoc}
81      */
82     public function supports($resource, $type = null)
83     {
84         return 'service' === $type;
85     }
86
87     private function addClassResource(\ReflectionClass $class, RouteCollection $collection)
88     {
89         do {
90             if (is_file($class->getFileName())) {
91                 $collection->addResource(new FileResource($class->getFileName()));
92             }
93         } while ($class = $class->getParentClass());
94     }
95 }