source: externals/php/vendor/guzzlehttp/guzzle/src/Cookie/SessionCookieJar.php@ f8c242e

Last change on this file since f8c242e was 01a620f, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago

Removed submodules for php externals and use composer to provide them and versioned them afterwards

  • Property mode set to 100644
File size: 1.5 KB
Line 
1<?php
2namespace GuzzleHttp\Cookie;
3
4/**
5 * Persists cookies in the client session
6 */
7class SessionCookieJar extends CookieJar
8{
9 /** @var string session key */
10 private $sessionKey;
11
12 /**
13 * Create a new SessionCookieJar object
14 *
15 * @param string $sessionKey Session key name to store the cookie data in session
16 */
17 public function __construct($sessionKey)
18 {
19 $this->sessionKey = $sessionKey;
20 $this->load();
21 }
22
23 /**
24 * Saves cookies to session when shutting down
25 */
26 public function __destruct()
27 {
28 $this->save();
29 }
30
31 /**
32 * Save cookies to the client session
33 */
34 public function save()
35 {
36 $json = [];
37 foreach ($this as $cookie) {
38 /** @var SetCookie $cookie */
39 if ($cookie->getExpires() && !$cookie->getDiscard()) {
40 $json[] = $cookie->toArray();
41 }
42 }
43
44 $_SESSION[$this->sessionKey] = json_encode($json);
45 }
46
47 /**
48 * Load the contents of the client session into the data array
49 */
50 protected function load()
51 {
52 $cookieJar = isset($_SESSION[$this->sessionKey])
53 ? $_SESSION[$this->sessionKey]
54 : null;
55
56 $data = json_decode($cookieJar, true);
57 if (is_array($data)) {
58 foreach ($data as $cookie) {
59 $this->setCookie(new SetCookie($cookie));
60 }
61 } elseif (strlen($data)) {
62 throw new \RuntimeException("Invalid cookie data");
63 }
64 }
65}
Note: See TracBrowser for help on using the repository browser.