ControllerArgumentValueResolverPass.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. public function process(ContainerBuilder $container)
  27. {
  28. if (!$container->hasDefinition('argument_resolver')) {
  29. return;
  30. }
  31. $resolvers = $this->findAndSortTaggedServices('controller.argument_value_resolver', $container);
  32. if ($container->getParameter('kernel.debug') && class_exists(Stopwatch::class) && $container->has('debug.stopwatch')) {
  33. foreach ($resolvers as $resolverReference) {
  34. $id = (string) $resolverReference;
  35. $container->register("debug.$id", TraceableValueResolver::class)
  36. ->setDecoratedService($id)
  37. ->setArguments([new Reference("debug.$id.inner"), new Reference('debug.stopwatch')]);
  38. }
  39. }
  40. $container
  41. ->getDefinition('argument_resolver')
  42. ->replaceArgument(1, new IteratorArgument($resolvers))
  43. ;
  44. }
  45. }