Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. FileCookieJar.php

class FileCookieJar

Persists non-session cookies using a JSON formatted file

Hierarchy

  • class \GuzzleHttp\Cookie\CookieJar implements \GuzzleHttp\Cookie\CookieJarInterface
    • class \GuzzleHttp\Cookie\FileCookieJar extends \GuzzleHttp\Cookie\CookieJar

Expanded class hierarchy of FileCookieJar

File

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

Namespace

GuzzleHttp\Cookie
View source
class FileCookieJar extends CookieJar {
    
    /**
     * @var string filename
     */
    private $filename;
    
    /**
     * @var bool Control whether to persist 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(string $cookieFile, bool $storeSessionCookies = false) {
        parent::__construct();
        $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(string $filename) : void {
        $json = [];
        
        /** @var SetCookie $cookie */
        foreach ($this as $cookie) {
            if (CookieJar::shouldPersist($cookie, $this->storeSessionCookies)) {
                $json[] = $cookie->toArray();
            }
        }
        $jsonStr = Utils::jsonEncode($json);
        if (false === \file_put_contents($filename, $jsonStr, \LOCK_EX)) {
            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(string $filename) : void {
        $json = \file_get_contents($filename);
        if (false === $json) {
            throw new \RuntimeException("Unable to load file {$filename}");
        }
        if ($json === '') {
            return;
        }
        $data = Utils::jsonDecode($json, true);
        if (\is_array($data)) {
            foreach ($data as $cookie) {
                $this->setCookie(new SetCookie($cookie));
            }
        }
        elseif (\is_scalar($data) && !empty($data)) {
            throw new \RuntimeException("Invalid cookie file: {$filename}");
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
CookieJar::$cookies private property
CookieJar::$strictMode private property
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::getCookieByName public function Finds and returns the cookie based on the name
CookieJar::getCookiePathFromRequest private function Computes cookie path following RFC 6265 section 5.1.4
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
FileCookieJar::$storeSessionCookies private property
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

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal