ObjectRouteLoader.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Routing\Loader;
  11. use Symfony\Component\Config\Loader\Loader;
  12. use Symfony\Component\Config\Resource\FileResource;
  13. use Symfony\Component\Routing\RouteCollection;
  14. /**
  15. * A route loader that calls a method on an object to load the routes.
  16. *
  17. * @author Ryan Weaver <ryan@knpuniversity.com>
  18. */
  19. abstract class ObjectRouteLoader extends Loader
  20. {
  21. /**
  22. * Returns the object that the method will be called on to load routes.
  23. *
  24. * For example, if your application uses a service container,
  25. * the $id may be a service id.
  26. *
  27. * @param string $id
  28. *
  29. * @return object
  30. */
  31. abstract protected function getServiceObject($id);
  32. /**
  33. * Calls the service that will load the routes.
  34. *
  35. * @param mixed $resource Some value that will resolve to a callable
  36. * @param string|null $type The resource type
  37. *
  38. * @return RouteCollection
  39. */
  40. public function load($resource, $type = null)
  41. {
  42. $parts = explode(':', $resource);
  43. if (count($parts) != 2) {
  44. throw new \InvalidArgumentException(sprintf('Invalid resource "%s" passed to the "service" route loader: use the format "service_name:methodName"', $resource));
  45. }
  46. $serviceString = $parts[0];
  47. $method = $parts[1];
  48. $loaderObject = $this->getServiceObject($serviceString);
  49. if (!is_object($loaderObject)) {
  50. throw new \LogicException(sprintf('%s:getServiceObject() must return an object: %s returned', get_class($this), gettype($loaderObject)));
  51. }
  52. if (!method_exists($loaderObject, $method)) {
  53. throw new \BadMethodCallException(sprintf('Method "%s" not found on "%s" when importing routing resource "%s"', $method, get_class($loaderObject), $resource));
  54. }
  55. $routeCollection = call_user_func(array($loaderObject, $method), $this);
  56. if (!$routeCollection instanceof RouteCollection) {
  57. $type = is_object($routeCollection) ? get_class($routeCollection) : gettype($routeCollection);
  58. throw new \LogicException(sprintf('The %s::%s method must return a RouteCollection: %s returned', get_class($loaderObject), $method, $type));
  59. }
  60. // make the service file tracked so that if it changes, the cache rebuilds
  61. $this->addClassResource(new \ReflectionClass($loaderObject), $routeCollection);
  62. return $routeCollection;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function supports($resource, $type = null)
  68. {
  69. return 'service' === $type;
  70. }
  71. private function addClassResource(\ReflectionClass $class, RouteCollection $collection)
  72. {
  73. do {
  74. if (is_file($class->getFileName())) {
  75. $collection->addResource(new FileResource($class->getFileName()));
  76. }
  77. } while ($class = $class->getParentClass());
  78. }
  79. }