GuzzleHttpCookieSessionCookieJar

Persists cookies in the client session.

Defined (1)

The class is defined in the following location(s).

/lib/Azure/GuzzleHttp/Cookie/SessionCookieJar.php  
  1. class SessionCookieJar extends CookieJar 
  2. /** @var string session key */ 
  3. private $sessionKey; 
  4.  
  5. /** @var bool Control whether to persist session cookies or not. */ 
  6. private $storeSessionCookies; 
  7.  
  8. /** 
  9. * Create a new SessionCookieJar object 
  10. * @param string $sessionKey Session key name to store the cookie  
  11. * data in session 
  12. * @param bool $storeSessionCookies Set to true to store session cookies 
  13. * in the cookie jar. 
  14. */ 
  15. public function __construct($sessionKey, $storeSessionCookies = false) 
  16. $this->sessionKey = $sessionKey; 
  17. $this->storeSessionCookies = $storeSessionCookies; 
  18. $this->load(); 
  19.  
  20. /** 
  21. * Saves cookies to session when shutting down 
  22. */ 
  23. public function __destruct() 
  24. $this->save(); 
  25.  
  26. /** 
  27. * Save cookies to the client session 
  28. */ 
  29. public function save() 
  30. $json = []; 
  31. foreach ($this as $cookie) { 
  32. /** @var SetCookie $cookie */ 
  33. if (CookieJar::shouldPersist($cookie, $this->storeSessionCookies)) { 
  34. $json[] = $cookie->toArray(); 
  35.  
  36. $_SESSION[$this->sessionKey] = json_encode($json); 
  37.  
  38. /** 
  39. * Load the contents of the client session into the data array 
  40. */ 
  41. protected function load() 
  42. if (!isset($_SESSION[$this->sessionKey])) { 
  43. return; 
  44. $data = json_decode($_SESSION[$this->sessionKey], true); 
  45. if (is_array($data)) { 
  46. foreach ($data as $cookie) { 
  47. $this->setCookie(new SetCookie($cookie)); 
  48. } elseif (strlen($data)) { 
  49. throw new \RuntimeException("Invalid cookie data");