annotate vendor/guzzlehttp/guzzle/src/Cookie/FileCookieJar.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2 namespace GuzzleHttp\Cookie;
Chris@0 3
Chris@0 4 /**
Chris@0 5 * Persists non-session cookies using a JSON formatted file
Chris@0 6 */
Chris@0 7 class FileCookieJar extends CookieJar
Chris@0 8 {
Chris@0 9 /** @var string filename */
Chris@0 10 private $filename;
Chris@0 11
Chris@0 12 /** @var bool Control whether to persist session cookies or not. */
Chris@0 13 private $storeSessionCookies;
Chris@0 14
Chris@0 15 /**
Chris@0 16 * Create a new FileCookieJar object
Chris@0 17 *
Chris@0 18 * @param string $cookieFile File to store the cookie data
Chris@0 19 * @param bool $storeSessionCookies Set to true to store session cookies
Chris@0 20 * in the cookie jar.
Chris@0 21 *
Chris@0 22 * @throws \RuntimeException if the file cannot be found or created
Chris@0 23 */
Chris@0 24 public function __construct($cookieFile, $storeSessionCookies = false)
Chris@0 25 {
Chris@0 26 $this->filename = $cookieFile;
Chris@0 27 $this->storeSessionCookies = $storeSessionCookies;
Chris@0 28
Chris@0 29 if (file_exists($cookieFile)) {
Chris@0 30 $this->load($cookieFile);
Chris@0 31 }
Chris@0 32 }
Chris@0 33
Chris@0 34 /**
Chris@0 35 * Saves the file when shutting down
Chris@0 36 */
Chris@0 37 public function __destruct()
Chris@0 38 {
Chris@0 39 $this->save($this->filename);
Chris@0 40 }
Chris@0 41
Chris@0 42 /**
Chris@0 43 * Saves the cookies to a file.
Chris@0 44 *
Chris@0 45 * @param string $filename File to save
Chris@0 46 * @throws \RuntimeException if the file cannot be found or created
Chris@0 47 */
Chris@0 48 public function save($filename)
Chris@0 49 {
Chris@0 50 $json = [];
Chris@0 51 foreach ($this as $cookie) {
Chris@0 52 /** @var SetCookie $cookie */
Chris@0 53 if (CookieJar::shouldPersist($cookie, $this->storeSessionCookies)) {
Chris@0 54 $json[] = $cookie->toArray();
Chris@0 55 }
Chris@0 56 }
Chris@0 57
Chris@0 58 $jsonStr = \GuzzleHttp\json_encode($json);
Chris@0 59 if (false === file_put_contents($filename, $jsonStr)) {
Chris@0 60 throw new \RuntimeException("Unable to save file {$filename}");
Chris@0 61 }
Chris@0 62 }
Chris@0 63
Chris@0 64 /**
Chris@0 65 * Load cookies from a JSON formatted file.
Chris@0 66 *
Chris@0 67 * Old cookies are kept unless overwritten by newly loaded ones.
Chris@0 68 *
Chris@0 69 * @param string $filename Cookie file to load.
Chris@0 70 * @throws \RuntimeException if the file cannot be loaded.
Chris@0 71 */
Chris@0 72 public function load($filename)
Chris@0 73 {
Chris@0 74 $json = file_get_contents($filename);
Chris@0 75 if (false === $json) {
Chris@0 76 throw new \RuntimeException("Unable to load file {$filename}");
Chris@0 77 } elseif ($json === '') {
Chris@0 78 return;
Chris@0 79 }
Chris@0 80
Chris@0 81 $data = \GuzzleHttp\json_decode($json, true);
Chris@0 82 if (is_array($data)) {
Chris@0 83 foreach (json_decode($json, true) as $cookie) {
Chris@0 84 $this->setCookie(new SetCookie($cookie));
Chris@0 85 }
Chris@0 86 } elseif (strlen($data)) {
Chris@0 87 throw new \RuntimeException("Invalid cookie file: {$filename}");
Chris@0 88 }
Chris@0 89 }
Chris@0 90 }