GuzzleHttpCookieFileCookieJar

Persists non-session cookies using a JSON formatted file.

Defined (1)

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

/lib/Azure/GuzzleHttp/Cookie/FileCookieJar.php  
  1. class FileCookieJar extends CookieJar 
  2. /** @var string filename */ 
  3. private $filename; 
  4.  
  5. /** @var bool Control whether to persist session cookies or not. */ 
  6. private $storeSessionCookies; 
  7.  
  8. /** 
  9. * Create a new FileCookieJar object 
  10. * @param string $cookieFile File to store the cookie data 
  11. * @param bool $storeSessionCookies Set to true to store session cookies 
  12. * in the cookie jar. 
  13. * @throws \RuntimeException if the file cannot be found or created 
  14. */ 
  15. public function __construct($cookieFile, $storeSessionCookies = false) 
  16. $this->filename = $cookieFile; 
  17. $this->storeSessionCookies = $storeSessionCookies; 
  18.  
  19. if (file_exists($cookieFile)) { 
  20. $this->load($cookieFile); 
  21.  
  22. /** 
  23. * Saves the file when shutting down 
  24. */ 
  25. public function __destruct() 
  26. $this->save($this->filename); 
  27.  
  28. /** 
  29. * Saves the cookies to a file. 
  30. * @param string $filename File to save 
  31. * @throws \RuntimeException if the file cannot be found or created 
  32. */ 
  33. public function save($filename) 
  34. $json = []; 
  35. foreach ($this as $cookie) { 
  36. /** @var SetCookie $cookie */ 
  37. if (CookieJar::shouldPersist($cookie, $this->storeSessionCookies)) { 
  38. $json[] = $cookie->toArray(); 
  39.  
  40. $jsonStr = \GuzzleHttp\json_encode($json); 
  41. if (false === file_put_contents($filename, $jsonStr)) { 
  42. throw new \RuntimeException("Unable to save file {$filename}"); 
  43.  
  44. /** 
  45. * Load cookies from a JSON formatted file. 
  46. * Old cookies are kept unless overwritten by newly loaded ones. 
  47. * @param string $filename Cookie file to load. 
  48. * @throws \RuntimeException if the file cannot be loaded. 
  49. */ 
  50. public function load($filename) 
  51. $json = file_get_contents($filename); 
  52. if (false === $json) { 
  53. throw new \RuntimeException("Unable to load file {$filename}"); 
  54. } elseif ($json === '') { 
  55. return; 
  56.  
  57. $data = \GuzzleHttp\json_decode($json, true); 
  58. if (is_array($data)) { 
  59. foreach (json_decode($json, true) as $cookie) { 
  60. $this->setCookie(new SetCookie($cookie)); 
  61. } elseif (strlen($data)) { 
  62. throw new \RuntimeException("Invalid cookie file: {$filename}");