class SessionCookieJar
Persists cookies in the client session
Hierarchy
- class \GuzzleHttp\Cookie\CookieJar implements \GuzzleHttp\Cookie\CookieJarInterface
- class \GuzzleHttp\Cookie\SessionCookieJar extends \GuzzleHttp\Cookie\CookieJar
Expanded class hierarchy of SessionCookieJar
File
-
vendor/
guzzlehttp/ guzzle/ src/ Cookie/ SessionCookieJar.php, line 8
Namespace
GuzzleHttp\CookieView source
class SessionCookieJar extends CookieJar {
/**
* @var string session key
*/
private $sessionKey;
/**
* @var bool Control whether to persist 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(string $sessionKey, bool $storeSessionCookies = false) {
parent::__construct();
$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() : void {
$json = [];
/** @var SetCookie $cookie */
foreach ($this as $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() : void {
if (!isset($_SESSION[$this->sessionKey])) {
return;
}
$data = \json_decode($_SESSION[$this->sessionKey], true);
if (\is_array($data)) {
foreach ($data as $cookie) {
$this->setCookie(new SetCookie($cookie));
}
}
elseif (\strlen($data)) {
throw new \RuntimeException('Invalid cookie data');
}
}
}
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 |
SessionCookieJar::$sessionKey | private | property | ||
SessionCookieJar::$storeSessionCookies | private | property | ||
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 |