Skip to content

Commit

Permalink
unqueueing cookies; access to cookie jar through cookie()
Browse files Browse the repository at this point in the history
  • Loading branch information
donwilson committed Nov 27, 2023
1 parent ea92f74 commit 0b1d0dd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
23 changes: 23 additions & 0 deletions src/Magnetar/Http/CookieJar/CookieJar.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,29 @@ public function setCookie(Cookie $cookie): self {
* @return self
*/
public function remove(string $name): self {
if(isset($this->cookie_queue[ $name ])) {
return $this->unequeue($name);
}

$this->cookie_queue[ $name ] = new Cookie(
$name,
'',
-604800,
$this->default_path,
$this->default_domain,
$this->default_secure,
$this->default_httponly
);

return $this;
}

/**
* Unqueue a cookie
* @param string $name The cookie name
* @return self
*/
public function unequeue(string $name): self {
unset($this->cookie_queue[ $name ]);

return $this;
Expand Down
11 changes: 8 additions & 3 deletions src/Magnetar/_autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function config_path(string $rel_path=''): string {
if(!function_exists('cookie')) {
/**
* Get or set a cookie. If $value is null, the cookie value from the request is returned. If $value is false, the cookie will be deleted
* @param string $name Cookie name
* @param ?string $name Cookie name. If null, the cookie jar will be returned. Defaults to null
* @param mixed $value Optional. Cookie value. If null, the cookie value will be returned. If false, the cookie will be deleted. Defaults to null
* @param int|null $expires_seconds Optional. Number of seconds until the cookie expires. If null, the default expiration time from config will be used. Defaults to null
* @param ?string $path Optional. Cookie path. If null, the default path from config will be used. Defaults to null
Expand All @@ -135,18 +135,23 @@ function config_path(string $rel_path=''): string {
* @param ?bool $httponly Optional. Whether the cookie should only be accessible over HTTP. If null, the default httponly setting from config will be used. Defaults to null
* @return mixed
*
* @see \Magnetar\Http\CookieJar\CookieJar
* @see \Magnetar\Http\CookieJar\CookieJar::get
* @see \Magnetar\Http\CookieJar\CookieJar::set
*/
function cookie(
string $name,
?string $name=null,
mixed $value=null,
?int $expires_seconds=null,
?string $path=null,
?string $domain=null,
?bool $secure=null,
?bool $httponly=null
): mixed {
if(null === $value) {
if(null === $name) {
// get the cookie jar
return app('cookie');
} elseif(null === $value) {
// get the cookie value
return app('cookie')->get($name);
} elseif(false === $value) {
Expand Down

0 comments on commit 0b1d0dd

Please sign in to comment.