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