TraceableValueResolver.php 2.0 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\Controller\ArgumentResolver;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
  13. use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
  14. use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
  15. use Symfony\Component\Stopwatch\Stopwatch;
  16. /**
  17. * Provides timing information via the stopwatch.
  18. *
  19. * @author Iltar van der Berg <kjarli@gmail.com>
  20. */
  21. final class TraceableValueResolver implements ArgumentValueResolverInterface, ValueResolverInterface
  22. {
  23. private ArgumentValueResolverInterface|ValueResolverInterface $inner;
  24. private Stopwatch $stopwatch;
  25. public function __construct(ArgumentValueResolverInterface|ValueResolverInterface $inner, Stopwatch $stopwatch)
  26. {
  27. $this->inner = $inner;
  28. $this->stopwatch = $stopwatch;
  29. }
  30. /**
  31. * @deprecated since Symfony 6.2, use resolve() instead
  32. */
  33. public function supports(Request $request, ArgumentMetadata $argument): bool
  34. {
  35. if ($this->inner instanceof ValueResolverInterface) {
  36. return true;
  37. }
  38. $method = $this->inner::class.'::'.__FUNCTION__;
  39. $this->stopwatch->start($method, 'controller.argument_value_resolver');
  40. $return = $this->inner->supports($request, $argument);
  41. $this->stopwatch->stop($method);
  42. return $return;
  43. }
  44. public function resolve(Request $request, ArgumentMetadata $argument): iterable
  45. {
  46. $method = $this->inner::class.'::'.__FUNCTION__;
  47. $this->stopwatch->start($method, 'controller.argument_value_resolver');
  48. yield from $this->inner->resolve($request, $argument);
  49. $this->stopwatch->stop($method);
  50. }
  51. }