sessionKey = $sessionKey; $this->storeSessionCookies = $storeSessionCookies; $this->load(); } /** * Saves cookies to session when shutting down */ public function __destruct() { $this->save(); } /** * Save cookies to the client session */ public function save(): void { $json = []; /** @var SetCookie $cookie */ foreach ($this as $cookie) { if (CookieJar::shouldPersist($cookie, $this->storeSessionCookies)) { $json[] = $cookie->toArray(); } } $json = \json_encode($json); if (false === $json) { throw new \RuntimeException('Unable to encode cookie data'); } $_SESSION[$this->sessionKey] = $json; } /** * Load the contents of the client session into the data array */ protected function load(): void { if (!isset($_SESSION[$this->sessionKey])) { return; } $json = $_SESSION[$this->sessionKey]; if (!\is_string($json)) { throw new \RuntimeException('Invalid cookie data'); } $data = \json_decode($json, true); if (\is_array($data)) { foreach ($data as $cookie) { if (!\is_array($cookie)) { throw new \RuntimeException('Invalid cookie data'); } $this->setCookie(new SetCookie($cookie)); } } elseif (\is_scalar($data) && \strlen((string) $data)) { throw new \RuntimeException('Invalid cookie data'); } } }