RequestContextProvider.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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\VarDumper\Dumper\ContextProvider;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use Symfony\Component\VarDumper\Caster\ReflectionCaster;
  13. use Symfony\Component\VarDumper\Cloner\VarCloner;
  14. /**
  15. * Tries to provide context from a request.
  16. *
  17. * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
  18. */
  19. final class RequestContextProvider implements ContextProviderInterface
  20. {
  21. private VarCloner $cloner;
  22. public function __construct(
  23. private RequestStack $requestStack,
  24. ) {
  25. $this->cloner = new VarCloner();
  26. $this->cloner->setMaxItems(0);
  27. $this->cloner->addCasters(ReflectionCaster::UNSET_CLOSURE_FILE_INFO);
  28. }
  29. public function getContext(): ?array
  30. {
  31. if (null === $request = $this->requestStack->getCurrentRequest()) {
  32. return null;
  33. }
  34. $controller = $request->attributes->get('_controller');
  35. return [
  36. 'uri' => $request->getUri(),
  37. 'method' => $request->getMethod(),
  38. 'controller' => $controller ? $this->cloner->cloneVar($controller) : $controller,
  39. 'identifier' => spl_object_hash($request),
  40. ];
  41. }
  42. }