You are here

class SessionCookieJar in Zircon Profile 8

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

Persists cookies in the client session

Hierarchy

Expanded class hierarchy of SessionCookieJar

File

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

Namespace

GuzzleHttp\Cookie
View source
class SessionCookieJar extends CookieJar {

  /** @var string session key */
  private $sessionKey;

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

  /**
   * Create a new SessionCookieJar object
   *
   * @param string $sessionKey        Session key name to store the cookie
   *                                  data in session
   * @param bool $storeSessionCookies Set to true to store session cookies
   *                                  in the cookie jar.
   */
  public function __construct($sessionKey, $storeSessionCookies = false) {
    $this->sessionKey = $sessionKey;
    $this->storeSessionCookies = $storeSessionCookies;
    $this
      ->load();
  }

  /**
   * Saves cookies to session when shutting down
   */
  public function __destruct() {
    $this
      ->save();
  }

  /**
   * Save cookies to the client session
   */
  public function save() {
    $json = [];
    foreach ($this as $cookie) {

      /** @var SetCookie $cookie */
      if (CookieJar::shouldPersist($cookie, $this->storeSessionCookies)) {
        $json[] = $cookie
          ->toArray();
      }
    }
    $_SESSION[$this->sessionKey] = json_encode($json);
  }

  /**
   * Load the contents of the client session into the data array
   */
  protected function load() {
    $cookieJar = isset($_SESSION[$this->sessionKey]) ? $_SESSION[$this->sessionKey] : null;
    $data = json_decode($cookieJar, true);
    if (is_array($data)) {
      foreach ($data as $cookie) {
        $this
          ->setCookie(new SetCookie($cookie));
      }
    }
    elseif (strlen($data)) {
      throw new \RuntimeException("Invalid cookie data");
    }
  }

}

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
SessionCookieJar::$sessionKey private property @var string session key
SessionCookieJar::$storeSessionCookies private property @var bool Control whether to presist session cookies or not.
SessionCookieJar::load protected function Load the contents of the client session into the data array
SessionCookieJar::save public function Save cookies to the client session
SessionCookieJar::__construct public function Create a new SessionCookieJar object Overrides CookieJar::__construct
SessionCookieJar::__destruct public function Saves cookies to session when shutting down