ObjectLoader.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. abstract protected function getObject(string $id): object;
  28. /**
  29. * Calls the object method that will load the routes.
  30. */
  31. public function load(mixed $resource, ?string $type = null): RouteCollection
  32. {
  33. if (!preg_match('/^[^\:]+(?:::(?:[^\:]+))?$/', $resource)) {
  34. 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'));
  35. }
  36. $parts = explode('::', $resource);
  37. $method = $parts[1] ?? '__invoke';
  38. $loaderObject = $this->getObject($parts[0]);
  39. if (!\is_object($loaderObject)) {
  40. throw new \TypeError(\sprintf('"%s:getObject()" must return an object: "%s" returned.', static::class, get_debug_type($loaderObject)));
  41. }
  42. if (!\is_callable([$loaderObject, $method])) {
  43. throw new \BadMethodCallException(\sprintf('Method "%s" not found on "%s" when importing routing resource "%s".', $method, get_debug_type($loaderObject), $resource));
  44. }
  45. $routeCollection = $loaderObject->$method($this, $this->env);
  46. if (!$routeCollection instanceof RouteCollection) {
  47. $type = get_debug_type($routeCollection);
  48. throw new \LogicException(\sprintf('The "%s::%s()" method must return a RouteCollection: "%s" returned.', get_debug_type($loaderObject), $method, $type));
  49. }
  50. // make the object file tracked so that if it changes, the cache rebuilds
  51. $this->addClassResource(new \ReflectionClass($loaderObject), $routeCollection);
  52. return $routeCollection;
  53. }
  54. private function addClassResource(\ReflectionClass $class, RouteCollection $collection): void
  55. {
  56. do {
  57. if (is_file($class->getFileName())) {
  58. $collection->addResource(new FileResource($class->getFileName()));
  59. }
  60. } while ($class = $class->getParentClass());
  61. }
  62. }