1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 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: 20: 21: 22:
23: abstract class ObjectRouteLoader extends Loader
24: {
25: 26: 27: 28: 29: 30: 31: 32: 33: 34:
35: abstract protected function getServiceObject($id);
36:
37: 38: 39: 40: 41: 42: 43: 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:
74: $this->addClassResource(new \ReflectionClass($loaderObject), $routeCollection);
75:
76: return $routeCollection;
77: }
78:
79: 80: 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: }
96: