No Description

ObjectLoader.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 ObjectLoader 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. * @return object
  28. */
  29. abstract protected function getObject(string $id);
  30. /**
  31. * Calls the object method that will load the routes.
  32. *
  33. * @param string $resource object_id::method
  34. * @param string|null $type The resource type
  35. *
  36. * @return RouteCollection
  37. */
  38. public function load($resource, $type = null)
  39. {
  40. if (!preg_match('/^[^\:]+(?:::?(?:[^\:]+))?$/', $resource)) {
  41. 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'));
  42. }
  43. if (1 === substr_count($resource, ':')) {
  44. $resource = str_replace(':', '::', $resource);
  45. @trigger_error(sprintf('Referencing object route loaders with a single colon is deprecated since Symfony 4.1. Use %s instead.', $resource), \E_USER_DEPRECATED);
  46. }
  47. $parts = explode('::', $resource);
  48. $method = $parts[1] ?? '__invoke';
  49. $loaderObject = $this->getObject($parts[0]);
  50. if (!\is_object($loaderObject)) {
  51. throw new \TypeError(sprintf('"%s:getObject()" must return an object: "%s" returned.', static::class, \gettype($loaderObject)));
  52. }
  53. if (!\is_callable([$loaderObject, $method])) {
  54. throw new \BadMethodCallException(sprintf('Method "%s" not found on "%s" when importing routing resource "%s".', $method, \get_class($loaderObject), $resource));
  55. }
  56. $routeCollection = $loaderObject->$method($this);
  57. if (!$routeCollection instanceof RouteCollection) {
  58. $type = \is_object($routeCollection) ? \get_class($routeCollection) : \gettype($routeCollection);
  59. throw new \LogicException(sprintf('The "%s::%s()" method must return a RouteCollection: "%s" returned.', \get_class($loaderObject), $method, $type));
  60. }
  61. // make the object file tracked so that if it changes, the cache rebuilds
  62. $this->addClassResource(new \ReflectionClass($loaderObject), $routeCollection);
  63. return $routeCollection;
  64. }
  65. private function addClassResource(\ReflectionClass $class, RouteCollection $collection)
  66. {
  67. do {
  68. if (is_file($class->getFileName())) {
  69. $collection->addResource(new FileResource($class->getFileName()));
  70. }
  71. } while ($class = $class->getParentClass());
  72. }
  73. }