RequestContextProvider.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace BeyondCode\DumpServer;
  3. use Illuminate\Http\Request;
  4. use Symfony\Component\VarDumper\Cloner\VarCloner;
  5. use Symfony\Component\VarDumper\Dumper\ContextProvider\ContextProviderInterface;
  6. class RequestContextProvider implements ContextProviderInterface
  7. {
  8. /**
  9. * The current request.
  10. *
  11. * @var \Illuminate\Http\Request|null
  12. */
  13. private $currentRequest;
  14. /**
  15. * The variable cloner.
  16. *
  17. * @var \Symfony\Component\VarDumper\Cloner\VarCloner
  18. */
  19. private $cloner;
  20. /**
  21. * RequestContextProvider constructor.
  22. *
  23. * @param \Illuminate\Http\Request|null $currentRequest
  24. * @return void
  25. */
  26. public function __construct(Request $currentRequest = null)
  27. {
  28. $this->currentRequest = $currentRequest;
  29. $this->cloner = new VarCloner;
  30. $this->cloner->setMaxItems(0);
  31. }
  32. /**
  33. * Get the context.
  34. *
  35. * @return array|null
  36. */
  37. public function getContext(): ?array
  38. {
  39. if ($this->currentRequest === null) {
  40. return null;
  41. }
  42. $controller = null;
  43. if ($route = $this->currentRequest->route()) {
  44. $controller = $route->controller;
  45. if (! $controller && ! is_string($route->action['uses'])) {
  46. $controller = $route->action['uses'];
  47. }
  48. }
  49. return [
  50. 'uri' => $this->currentRequest->getUri(),
  51. 'method' => $this->currentRequest->getMethod(),
  52. 'controller' => $controller ? $this->cloner->cloneVar(class_basename($controller)) : $controller,
  53. 'identifier' => spl_object_hash($this->currentRequest),
  54. ];
  55. }
  56. }