You are here

class FileCookieJar in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/guzzlehttp/guzzle/src/Cookie/FileCookieJar.php \GuzzleHttp\Cookie\FileCookieJar

Persists non-session cookies using a JSON formatted file

Hierarchy

Expanded class hierarchy of FileCookieJar

File

vendor/guzzlehttp/guzzle/src/Cookie/FileCookieJar.php, line 7

Namespace

GuzzleHttp\Cookie
View source
class FileCookieJar extends CookieJar {

  /** @var string filename */
  private $filename;

  /** @var bool Control whether to presist session cookies or not. */
  private $storeSessionCookies;

  /**
   * Create a new FileCookieJar object
   *
   * @param string $cookieFile        File to store the cookie data
   * @param bool $storeSessionCookies Set to true to store session cookies
   *                                  in the cookie jar.
   *
   * @throws \RuntimeException if the file cannot be found or created
   */
  public function __construct($cookieFile, $storeSessionCookies = false) {
    $this->filename = $cookieFile;
    $this->storeSessionCookies = $storeSessionCookies;
    if (file_exists($cookieFile)) {
      $this
        ->load($cookieFile);
    }
  }

  /**
   * Saves the file when shutting down
   */
  public function __destruct() {
    $this
      ->save($this->filename);
  }

  /**
   * Saves the cookies to a file.
   *
   * @param string $filename File to save
   * @throws \RuntimeException if the file cannot be found or created
   */
  public function save($filename) {
    $json = [];
    foreach ($this as $cookie) {

      /** @var SetCookie $cookie */
      if (CookieJar::shouldPersist($cookie, $this->storeSessionCookies)) {
        $json[] = $cookie
          ->toArray();
      }
    }
    if (false === file_put_contents($filename, json_encode($json))) {
      throw new \RuntimeException("Unable to save file {$filename}");
    }
  }

  /**
   * Load cookies from a JSON formatted file.
   *
   * Old cookies are kept unless overwritten by newly loaded ones.
   *
   * @param string $filename Cookie file to load.
   * @throws \RuntimeException if the file cannot be loaded.
   */
  public function load($filename) {
    $json = file_get_contents($filename);
    if (false === $json) {
      throw new \RuntimeException("Unable to load file {$filename}");
    }
    $data = json_decode($json, true);
    if (is_array($data)) {
      foreach (json_decode($json, true) as $cookie) {
        $this
          ->setCookie(new SetCookie($cookie));
      }
    }
    elseif (strlen($data)) {
      throw new \RuntimeException("Invalid cookie file: {$filename}");
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CookieJar::$cookies private property @var SetCookie[] Loaded cookie data
CookieJar::$strictMode private property @var bool
CookieJar::clear public function Remove cookies currently held in the cookie jar. Overrides CookieJarInterface::clear
CookieJar::clearSessionCookies public function Discard all sessions cookies. Overrides CookieJarInterface::clearSessionCookies
CookieJar::count public function
CookieJar::extractCookies public function Extract cookies from an HTTP response and store them in the CookieJar. Overrides CookieJarInterface::extractCookies
CookieJar::fromArray public static function Create a new Cookie jar from an associative array and domain.
CookieJar::getCookieValue public static function Quote the cookie value if it is not already quoted and it contains problematic characters.
CookieJar::getIterator public function
CookieJar::removeCookieIfEmpty private function If a cookie already exists and the server asks to set it again with a null value, the cookie must be deleted.
CookieJar::setCookie public function Sets a cookie in the cookie jar. Overrides CookieJarInterface::setCookie
CookieJar::shouldPersist public static function Evaluate if this cookie should be persisted to storage that survives between requests.
CookieJar::toArray public function Converts the cookie jar to an array. Overrides CookieJarInterface::toArray
CookieJar::withCookieHeader public function Create a request with added cookie headers. Overrides CookieJarInterface::withCookieHeader
FileCookieJar::$filename private property @var string filename
FileCookieJar::$storeSessionCookies private property @var bool Control whether to presist session cookies or not.
FileCookieJar::load public function Load cookies from a JSON formatted file.
FileCookieJar::save public function Saves the cookies to a file.
FileCookieJar::__construct public function Create a new FileCookieJar object Overrides CookieJar::__construct
FileCookieJar::__destruct public function Saves the file when shutting down