ServiceValueResolver.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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\HttpFoundation\Request;
  13. use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
  14. use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
  15. /**
  16. * Yields a service keyed by _controller and argument name.
  17. *
  18. * @author Nicolas Grekas <p@tchwork.com>
  19. */
  20. final class ServiceValueResolver implements ArgumentValueResolverInterface
  21. {
  22. private $container;
  23. public function __construct(ContainerInterface $container)
  24. {
  25. $this->container = $container;
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function supports(Request $request, ArgumentMetadata $argument)
  31. {
  32. return is_string($controller = $request->attributes->get('_controller')) && $this->container->has($controller) && $this->container->get($controller)->has($argument->getName());
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function resolve(Request $request, ArgumentMetadata $argument)
  38. {
  39. yield $this->container->get($request->attributes->get('_controller'))->get($argument->getName());
  40. }
  41. }