RequestContextProvider.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 $requestStack;
  22. private $cloner;
  23. public function __construct(RequestStack $requestStack)
  24. {
  25. $this->requestStack = $requestStack;
  26. $this->cloner = new VarCloner();
  27. $this->cloner->setMaxItems(0);
  28. $this->cloner->addCasters(ReflectionCaster::UNSET_CLOSURE_FILE_INFO);
  29. }
  30. public function getContext(): ?array
  31. {
  32. if (null === $request = $this->requestStack->getCurrentRequest()) {
  33. return null;
  34. }
  35. $controller = $request->attributes->get('_controller');
  36. return [
  37. 'uri' => $request->getUri(),
  38. 'method' => $request->getMethod(),
  39. 'controller' => $controller ? $this->cloner->cloneVar($controller) : $controller,
  40. 'identifier' => spl_object_hash($request),
  41. ];
  42. }
  43. }