|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Barryvdh\Debugbar; |
| 4 | + |
| 5 | +use DebugBar\HttpDriverInterface; |
| 6 | +use Illuminate\Session\Store; |
| 7 | +use Symfony\Component\HttpFoundation\Response; |
| 8 | +use Symfony\Component\HttpFoundation\Session\Session; |
| 9 | + |
| 10 | +/** |
| 11 | + * HTTP driver for Larave Request/Session using Cookies |
| 12 | + */ |
| 13 | +class SessionHttpDriver implements HttpDriverInterface |
| 14 | +{ |
| 15 | + /** @var \SessionHandlerInterface */ |
| 16 | + protected $session; |
| 17 | + |
| 18 | + /** @var \Symfony\Component\HttpFoundation\Response */ |
| 19 | + protected $response; |
| 20 | + |
| 21 | + protected $data = null; |
| 22 | + |
| 23 | + protected $id = '_debugbar'; |
| 24 | + |
| 25 | + public function __construct($session, $response = null) |
| 26 | + { |
| 27 | + $this->session = $session; |
| 28 | + $this->response = $response; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @param \Symfony\Component\HttpFoundation\Response $response |
| 33 | + * @return void |
| 34 | + */ |
| 35 | + public function setResponse($response) |
| 36 | + { |
| 37 | + $this->response = $response; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * {@inheritDoc} |
| 42 | + */ |
| 43 | + public function setHeaders(array $headers) |
| 44 | + { |
| 45 | + if (!is_null($this->response)) { |
| 46 | + $this->response->headers->add($headers); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + protected function ensureStarted() |
| 51 | + { |
| 52 | + if ($this->data === null) { |
| 53 | + $this->data = json_decode($this->session->read($this->id), true) ?: []; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * {@inheritDoc} |
| 59 | + */ |
| 60 | + public function isSessionStarted() |
| 61 | + { |
| 62 | + $this->ensureStarted(); |
| 63 | + return true; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * {@inheritDoc} |
| 68 | + */ |
| 69 | + public function setSessionValue($name, $value) |
| 70 | + { |
| 71 | + $this->isSessionStarted(); |
| 72 | + |
| 73 | + $this->data[$name] = $value; |
| 74 | + $this->session->write($this->id, json_encode($this->data)); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * {@inheritDoc} |
| 79 | + */ |
| 80 | + public function hasSessionValue($name) |
| 81 | + { |
| 82 | + $this->ensureStarted(); |
| 83 | + |
| 84 | + return array_key_exists($name, $this->data); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * {@inheritDoc} |
| 89 | + */ |
| 90 | + public function getSessionValue($name) |
| 91 | + { |
| 92 | + $this->ensureStarted(); |
| 93 | + |
| 94 | + return $this->data[$name] ?? null; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * {@inheritDoc} |
| 99 | + */ |
| 100 | + public function deleteSessionValue($name) |
| 101 | + {$this->isSessionStarted(); |
| 102 | + |
| 103 | + unset($this->data[$name]); |
| 104 | + |
| 105 | + $this->session->write($this->id, json_encode($this->data)); |
| 106 | + } |
| 107 | +} |
0 commit comments