Skip to content

Commit

Permalink
cookie facade, helper fucntion, Cookie gets/sets
Browse files Browse the repository at this point in the history
  • Loading branch information
donwilson committed Nov 15, 2023
1 parent 791b441 commit 588c3d8
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 35 deletions.
3 changes: 3 additions & 0 deletions src/Magnetar/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,9 @@ public function registerCoreContainerAliases(): void {
'config' => [
\Magnetar\Config\Config::class,
],
'cookie' => [
\Magnetar\Http\CookieJar\CookieJar::class,
],
'database' => [
\Magnetar\Database\ConnectionManager::class,
],
Expand Down
34 changes: 34 additions & 0 deletions src/Magnetar/Helpers/Facades/Cookie.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);

namespace Magnetar\Helpers\Facades;

use Magnetar\Helpers\Facades\Facade;

/**
* @method importCookiesFromRequest(Magnetar\Http\Request $request): void
* @method getCookies(): array
* @method getQueuedCookies(): array
* @method get(string $name): ?Magnetar\Http\CookieJar\Cookie
* @method getValue(string $name): ?string
* @method set(string $name, string $value, ?int $expires_seconds=null, ?string $path=null, ?string $domain=null, ?bool $secure=null, ?bool $httponly=null): self
* @method setCookie(Magnetar\Http\CookieJar\Cookie $cookie): self
* @method remove(string $name): self
* @method setDefaults(?int $expires_seconds=null, ?string $path=null, ?string $domain=null, ?bool $secure=null, ?bool $httponly=null): self
* @method getDefaultExpiresSeconds(): int
* @method getDefaultPath(): string
* @method getDefaultDomain(): string
* @method getDefaultSecure(): bool
* @method getDefaultHttpOnly(): bool
*
* @see \Magnetar\Http\CookieJar\CookieJar
*/
class Cookie extends Facade {
/**
* Get the named key that this facade represents
* @return string
*/
protected static function getFacadeKey(): string {
return 'cookie';
}
}
2 changes: 2 additions & 0 deletions src/Magnetar/Helpers/Facades/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Magnetar\Helpers\Facades\Facade;

/**
* @method processRequestCookies(): void
* @method create(): Magnetar\Http\Request
* @method headers(): array
* @method header(string $name): ?string
Expand All @@ -17,6 +18,7 @@
* @method get(string $name, mixed $default=null): mixed
* @method isset(string $name): bool
* @method parameters(): array
* @method cookies(): array
* @method body(): string
* @method file(string $input_name): Magnetar\Http\UploadedFile|array|null
* @method files(): array
Expand Down
2 changes: 1 addition & 1 deletion src/Magnetar/Helpers/Facades/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @method responseCode(int $response_code=200): self
* @method body(string $body=''): self
* @method sendBody(): self
* @method setCookie(string $name, ?string $value=null, int $expires=0, string $path='', string $domain='', bool $secure=false, bool $httponly=false): self
* @method setCookie(string $name, ?string $value=null, ?int $expires=null): self
* @method send(): self
* @method sendHeaders(): self
* @method header(string $header, ?string $value=null, int|bool $replace=true, ?int $response_code=0): self|string
Expand Down
131 changes: 108 additions & 23 deletions src/Magnetar/Http/CookieJar/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,83 +21,168 @@ public function __construct(
protected string $value='',

/**
* Expiration time in seconds. Used for newly created cookies
* Expiration in seconds
* @var int|null
*/
protected int|null $expires_seconds=null
protected int|null $expires_seconds=null,

/**
* Path
* @var string|null
*/
protected string|null $path=null,

/**
* Domain
* @var string|null
*/
protected string|null $domain=null,

/**
* Secure flag
* @var bool|null
*/
protected bool|null $secure=null,

/**
* Httponly flag
* @var bool|null
*/
protected bool|null $httponly=null
) {

}

/**
* Get the name of the cookie
* @return string
*/
public function getName(): string {
return $this->name;
}

/**
* Set the name of the cookie
* @param string $name The cookie name
* @return self
*/
public function setName(string $name): self {
$this->name = $name;

return $this;
}

/**
* Get the value of the cookie
* @return string
*/
public function getValue(): string {
return $this->value;
}

/**
* Set the value of the cookie
* @param string|bool $value The cookie value. If false, the cookie will be deleted
* @return self
*/
public function setValue(string|bool $value): self {
$this->value = $value;

return $this;
}

/**
* Get the expiration time of the cookie
* @return int|null
*/
public function getExpires(): int|null {
return $this->expires_seconds;
}

/**
* Set the value of the cookie
* Set the expiration time of the cookie
* @param int $expires_seconds The expiration time in seconds. If null, the default will be used
* @return self
*/
public function setValue(string $value): void {
$this->value = $value;
public function setExpires(int|null $expires_seconds=null): self {
$this->expires_seconds = $expires_seconds;

return $this;
}

/**
* Get the expiration time of the cookie
* @return string|null
*/
public function getPath(): string|null {
return $this->path;
}

/**
* Set the expiration time of the cookie
* @param string|null $path The path. If null, the default will be used
* @return self
*/
public function setExpires(int $expires_seconds): void {
$this->expires_seconds = $expires_seconds;
public function setPath(string|null $path): self {
$this->path = $path;

return $this;
}

/**
* Get the Domain setting
* @return string|null
*/
public function getDomain(): string|null {
return $this->domain;
}

/**
* Get the cookie as a string
* Set the Domain setting
* @param string|null $path The path. If null, the default will be used
* @return self
*/
public function __toString(): string {
return $this->name .'='. $this->value;
public function setDomain(string|null $domain): self {
$this->domain = $domain;

return $this;
}

/**
* Create a new cookie
* Get the Secure setting
* @return bool|null
*/
public static function create(string $name, string $value='', int|null $expires=null): Cookie {
return new Cookie($name, $value, $expires);
public function getSecure(): bool|null {
return $this->secure;
}

/**
* Create a new cookie from a string
* Set the Secure setting
* @param bool|null $secure The Secure setting. If null, the default will be used
* @return self
*/
public static function fromString(string $cookie): Cookie {
$parts = explode('=', $cookie, 2);
public function setSecure(bool|null $secure): self {
$this->secure = $secure;

return new Cookie($parts[0], $parts[1]);
return $this;
}

/**
* Create a new cookie from an array
* Get the Http Only setting
* @return bool|null
*/
public static function fromArray(array $cookie): Cookie {
return new Cookie($cookie['name'], $cookie['value'], $cookie['expires']);
public function getHttpOnly(): bool|null {
return $this->httponly;
}

/**
* Create a new cookie from a cookie object
* Set the Http Only setting
* @param bool|null $httponly The Http Only setting. If null, the default will be used
* @return self
*/
public static function fromCookie(Cookie $cookie): Cookie {
return new Cookie($cookie->getName(), $cookie->getValue(), $cookie->getExpires());
public function setHttpOnly(bool|null $httponly): self {
$this->httponly = $httponly;

return $this;
}
}
36 changes: 25 additions & 11 deletions src/Magnetar/Http/CookieJar/CookieJar.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Magnetar\Http\CookieJar;

use Magnetar\Http\Request;
use Magnetar\Http\CookieJar\Cookie;

class CookieJar {
/**
Expand Down Expand Up @@ -134,12 +135,20 @@ public function getValue(string $name): string|null {
public function set(
string $name,
string $value,
int|null $expires_seconds=null
int|null $expires_seconds=null,
string|null $path=null,
string|null $domain=null,
bool|null $secure=null,
bool|null $httponly=null
): self {
$this->cookie_queue[ $name ] = new Cookie(
$name,
$value,
$expires_seconds ?? $this->default_expires_seconds
$expires_seconds,
$path,
$domain,
$secure,
$httponly
);

return $this;
Expand Down Expand Up @@ -167,16 +176,13 @@ public function remove(string $name): self {
return $this;
}


public function getDefaultExpiresSeconds(): int {
return $this->default_expires_seconds;
}

/**
* Set the default cookie settings
* @param int $expires_seconds The default expiration time in seconds
* @param string $path The default path
* @param string $domain The default domain
* Set the default cookie settings. If a param is left null, no change is made
* @param int|null $expires_seconds The default expiration time in seconds
* @param string|null $path The default path
* @param string|null $domain The default domain
* @param bool|null $secure The default secure flag
* @param bool|null $httponly The default httponly flag
* @return self
*/
public function setDefaults(
Expand Down Expand Up @@ -216,6 +222,14 @@ public function setDefaults(
return $this;
}

/**
* Get the default expiration time in seconds
* @return int
*/
public function getDefaultExpiresSeconds(): int {
return $this->default_expires_seconds;
}

/**
* Get the default cookie path
* @return string The default cookie path
Expand Down
43 changes: 43 additions & 0 deletions src/Magnetar/_autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,49 @@ 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|bool|null $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|null $path Optional. Cookie path. If null, the default path from config will be used. Defaults to null
* @param string|null $domain Optional. Cookie domain. If null, the default domain from config will be used. Defaults to null
* @param bool|null $secure Optional. Whether the cookie should only be sent over HTTPS. If null, the default secure setting from config will be used. Defaults to null
* @param bool|null $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::get
*/
function cookie(
string $name,
string|bool|null $value=null,
int|null $expires_seconds=null,
string|null $path=null,
string|null $domain=null,
bool|null $secure=null,
bool|null $httponly=null
): mixed {
if(null === $value) {
// get the cookie value
return app('cookie')->get($name);
} elseif(false === $value) {
// delete the cookie
return app('cookie')->remove($name);
}

return app('cookie')->get(
$name,
$value,
$expires_seconds,
$path,
$domain,
$secure,
$httponly
);
}
}

if(!function_exists('data_path')) {
/**
* Get the path to a file in the data directory
Expand Down

0 comments on commit 588c3d8

Please sign in to comment.