ContainerControllerResolver.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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\HttpKernel\Controller;
  11. use Psr\Container\ContainerInterface;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Component\DependencyInjection\Container;
  14. use Symfony\Component\HttpFoundation\Request;
  15. /**
  16. * A controller resolver searching for a controller in a psr-11 container when using the "service:method" notation.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
  20. */
  21. class ContainerControllerResolver extends ControllerResolver
  22. {
  23. protected $container;
  24. public function __construct(ContainerInterface $container, LoggerInterface $logger = null)
  25. {
  26. $this->container = $container;
  27. parent::__construct($logger);
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function getController(Request $request)
  33. {
  34. $controller = parent::getController($request);
  35. if (\is_array($controller) && isset($controller[0]) && \is_string($controller[0]) && $this->container->has($controller[0])) {
  36. $controller[0] = $this->instantiateController($controller[0]);
  37. }
  38. return $controller;
  39. }
  40. /**
  41. * Returns a callable for the given controller.
  42. *
  43. * @param string $controller A Controller string
  44. *
  45. * @return mixed A PHP callable
  46. *
  47. * @throws \LogicException When the name could not be parsed
  48. * @throws \InvalidArgumentException When the controller class does not exist
  49. */
  50. protected function createController($controller)
  51. {
  52. if (false !== strpos($controller, '::')) {
  53. return parent::createController($controller);
  54. }
  55. $method = null;
  56. if (1 == substr_count($controller, ':')) {
  57. // controller in the "service:method" notation
  58. list($controller, $method) = explode(':', $controller, 2);
  59. }
  60. if (!$this->container->has($controller)) {
  61. $this->throwExceptionIfControllerWasRemoved($controller);
  62. throw new \LogicException(sprintf('Controller not found: service "%s" does not exist.', $controller));
  63. }
  64. $service = $this->container->get($controller);
  65. if (null !== $method) {
  66. return [$service, $method];
  67. }
  68. if (!method_exists($service, '__invoke')) {
  69. throw new \LogicException(sprintf('Controller "%s" cannot be called without a method name. Did you forget an "__invoke" method?', $controller));
  70. }
  71. return $service;
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. protected function instantiateController($class)
  77. {
  78. if ($this->container->has($class)) {
  79. return $this->container->get($class);
  80. }
  81. try {
  82. return parent::instantiateController($class);
  83. } catch (\ArgumentCountError $e) {
  84. } catch (\ErrorException $e) {
  85. } catch (\TypeError $e) {
  86. }
  87. $this->throwExceptionIfControllerWasRemoved($class, $e);
  88. throw $e;
  89. }
  90. /**
  91. * @param string $controller
  92. * @param \Exception|\Throwable|null $previous
  93. */
  94. private function throwExceptionIfControllerWasRemoved($controller, $previous = null)
  95. {
  96. if ($this->container instanceof Container && isset($this->container->getRemovedIds()[$controller])) {
  97. throw new \LogicException(sprintf('Controller "%s" cannot be fetched from the container because it is private. Did you forget to tag the service with "controller.service_arguments"?', $controller), 0, $previous);
  98. }
  99. }
  100. }