ServiceValueResolver.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\ArgumentResolver;
  11. use Psr\Container\ContainerInterface;
  12. use Symfony\Component\DependencyInjection\Exception\RuntimeException;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
  15. use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
  16. use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
  17. /**
  18. * Yields a service keyed by _controller and argument name.
  19. *
  20. * @author Nicolas Grekas <p@tchwork.com>
  21. */
  22. final class ServiceValueResolver implements ArgumentValueResolverInterface, ValueResolverInterface
  23. {
  24. private ContainerInterface $container;
  25. public function __construct(ContainerInterface $container)
  26. {
  27. $this->container = $container;
  28. }
  29. /**
  30. * @deprecated since Symfony 6.2, use resolve() instead
  31. */
  32. public function supports(Request $request, ArgumentMetadata $argument): bool
  33. {
  34. @trigger_deprecation('symfony/http-kernel', '6.2', 'The "%s()" method is deprecated, use "resolve()" instead.', __METHOD__);
  35. $controller = $request->attributes->get('_controller');
  36. if (\is_array($controller) && \is_callable($controller, true) && \is_string($controller[0])) {
  37. $controller = $controller[0].'::'.$controller[1];
  38. } elseif (!\is_string($controller) || '' === $controller) {
  39. return false;
  40. }
  41. if ('\\' === $controller[0]) {
  42. $controller = ltrim($controller, '\\');
  43. }
  44. if (!$this->container->has($controller) && false !== $i = strrpos($controller, ':')) {
  45. $controller = substr($controller, 0, $i).strtolower(substr($controller, $i));
  46. }
  47. return $this->container->has($controller) && $this->container->get($controller)->has($argument->getName());
  48. }
  49. public function resolve(Request $request, ArgumentMetadata $argument): array
  50. {
  51. $controller = $request->attributes->get('_controller');
  52. if (\is_array($controller) && \is_callable($controller, true) && \is_string($controller[0])) {
  53. $controller = $controller[0].'::'.$controller[1];
  54. } elseif (!\is_string($controller) || '' === $controller) {
  55. return [];
  56. }
  57. if ('\\' === $controller[0]) {
  58. $controller = ltrim($controller, '\\');
  59. }
  60. if (!$this->container->has($controller) && false !== $i = strrpos($controller, ':')) {
  61. $controller = substr($controller, 0, $i).strtolower(substr($controller, $i));
  62. }
  63. if (!$this->container->has($controller) || !$this->container->get($controller)->has($argument->getName())) {
  64. return [];
  65. }
  66. try {
  67. return [$this->container->get($controller)->get($argument->getName())];
  68. } catch (RuntimeException $e) {
  69. $what = \sprintf('argument $%s of "%s()"', $argument->getName(), $controller);
  70. $message = preg_replace('/service "\.service_locator\.[^"]++"/', $what, $e->getMessage());
  71. if ($e->getMessage() === $message) {
  72. $message = \sprintf('Cannot resolve %s: %s', $what, $message);
  73. }
  74. $r = new \ReflectionProperty($e, 'message');
  75. $r->setValue($e, $message);
  76. throw $e;
  77. }
  78. }
  79. }