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