ControllerArgumentValueResolverPass.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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\DependencyInjection;
  11. use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
  12. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  13. use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. use Symfony\Component\DependencyInjection\Reference;
  16. use Symfony\Component\HttpKernel\Controller\ArgumentResolver\TraceableValueResolver;
  17. use Symfony\Component\Stopwatch\Stopwatch;
  18. /**
  19. * Gathers and configures the argument value resolvers.
  20. *
  21. * @author Iltar van der Berg <kjarli@gmail.com>
  22. */
  23. class ControllerArgumentValueResolverPass implements CompilerPassInterface
  24. {
  25. use PriorityTaggedServiceTrait;
  26. private $argumentResolverService;
  27. private $argumentValueResolverTag;
  28. private $traceableResolverStopwatch;
  29. public function __construct(string $argumentResolverService = 'argument_resolver', string $argumentValueResolverTag = 'controller.argument_value_resolver', string $traceableResolverStopwatch = 'debug.stopwatch')
  30. {
  31. $this->argumentResolverService = $argumentResolverService;
  32. $this->argumentValueResolverTag = $argumentValueResolverTag;
  33. $this->traceableResolverStopwatch = $traceableResolverStopwatch;
  34. }
  35. public function process(ContainerBuilder $container)
  36. {
  37. if (!$container->hasDefinition($this->argumentResolverService)) {
  38. return;
  39. }
  40. $resolvers = $this->findAndSortTaggedServices($this->argumentValueResolverTag, $container);
  41. if ($container->getParameter('kernel.debug') && class_exists(Stopwatch::class) && $container->has($this->traceableResolverStopwatch)) {
  42. foreach ($resolvers as $resolverReference) {
  43. $id = (string) $resolverReference;
  44. $container->register("debug.$id", TraceableValueResolver::class)
  45. ->setDecoratedService($id)
  46. ->setArguments([new Reference("debug.$id.inner"), new Reference($this->traceableResolverStopwatch)]);
  47. }
  48. }
  49. $container
  50. ->getDefinition($this->argumentResolverService)
  51. ->replaceArgument(1, new IteratorArgument($resolvers))
  52. ;
  53. }
  54. }