Skip to content

Commit e1f0810

Browse files
authored
Merge pull request #4596 from mostafakhudair/cookie
Simplify Cookie Class
2 parents b20fa0d + 096f972 commit e1f0810

File tree

10 files changed

+135
-195
lines changed

10 files changed

+135
-195
lines changed

system/Common.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ function config(string $name, bool $getShared = true)
244244
*/
245245
function cookie(string $name, string $value = '', array $options = []): Cookie
246246
{
247-
return Cookie::create($name, $value, $options);
247+
return new Cookie($name, $value, $options);
248248
}
249249
}
250250

system/Cookie/CloneableCookieInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function withValue(string $value);
5353
*
5454
* @return static
5555
*/
56-
public function withExpiresAt($expires = 0);
56+
public function withExpires($expires);
5757

5858
/**
5959
* Creates a new Cookie that will expire the cookie from the browser.

system/Cookie/Cookie.php

Lines changed: 19 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* reassign this new instance to a new variable to capture it.
2828
*
2929
* ```php
30-
* $cookie = Cookie::create('test_cookie', 'test_value');
30+
* $cookie = new Cookie('test_cookie', 'test_value');
3131
* $cookie->getName(); // test_cookie
3232
*
3333
* $cookie->withName('prod_cookie');
@@ -149,8 +149,8 @@ public static function setDefaults($config = [])
149149
$newDefaults = $config;
150150
}
151151

152-
// This array union ensures that even if passed `$config`
153-
// is not `CookieConfig` or `array`, no empty defaults will occur.
152+
// This array union ensures that even if passed `$config` is not
153+
// `CookieConfig` or `array`, no empty defaults will occur.
154154
self::$defaults = $newDefaults + $oldDefaults;
155155

156156
return $oldDefaults;
@@ -197,21 +197,19 @@ public static function fromHeaderString(string $cookie, bool $raw = false)
197197
$data[strtolower($attr)] = $val;
198198
}
199199

200-
return static::create($name, $value, $data);
200+
return new static($name, $value, $data);
201201
}
202202

203203
/**
204-
* Create Cookie objects on the fly.
204+
* Construct a new Cookie instance.
205205
*
206-
* @param string $name
207-
* @param string $value
208-
* @param array<string, mixed> $options
206+
* @param string $name The cookie's name
207+
* @param string $value The cookie's value
208+
* @param array<string, mixed> $options The cookie's options
209209
*
210210
* @throws CookieException
211-
*
212-
* @return static
213211
*/
214-
public static function create(string $name, string $value = '', array $options = [])
212+
final public function __construct(string $name, string $value = '', array $options = [])
215213
{
216214
$options += self::$defaults;
217215

@@ -224,56 +222,14 @@ public static function create(string $name, string $value = '', array $options =
224222
unset($options['max-age']);
225223
}
226224

227-
return new static(
228-
$name,
229-
$value,
230-
$options['expires'],
231-
$options['prefix'],
232-
$options['path'],
233-
$options['domain'],
234-
$options['secure'],
235-
$options['httponly'],
236-
$options['raw'],
237-
$options['samesite']
238-
);
239-
}
240-
241-
/**
242-
* Construct a new Cookie instance.
243-
*
244-
* @param string $name The name of the cookie
245-
* @param string $value The value of the cookie
246-
* @param DateTimeInterface|integer|string $expires The time the cookie expires
247-
* @param string|null $prefix The prefix of the cookie
248-
* @param string|null $path The path on the server in which the cookie is available
249-
* @param string|null $domain The domain in which the cookie is available
250-
* @param boolean $secure Whether to send back the cookie over HTTPS
251-
* @param boolean $httponly Whether to forbid JavaScript access to cookies
252-
* @param boolean $raw Whether the cookie should be sent with no URL encoding
253-
* @param string $samesite Whether the cookie will be available for cross-site requests
254-
*
255-
* @throws CookieException
256-
*/
257-
final public function __construct(
258-
string $name,
259-
string $value = '',
260-
$expires = 0,
261-
string $prefix = null,
262-
string $path = null,
263-
string $domain = null,
264-
bool $secure = false,
265-
bool $httponly = true,
266-
bool $raw = false,
267-
string $samesite = self::SAMESITE_LAX
268-
)
269-
{
270225
// to retain BC
271-
$prefix = $prefix ?: self::$defaults['prefix'];
272-
$path = $path ?: self::$defaults['path'];
273-
$domain = $domain ?: self::$defaults['domain'];
274-
$secure = $secure ?: self::$defaults['secure'];
275-
$httponly = $httponly ?: self::$defaults['httponly'];
276-
$samesite = $samesite ?: self::$defaults['samesite'];
226+
$prefix = $options['prefix'] ?: self::$defaults['prefix'];
227+
$path = $options['path'] ?: self::$defaults['path'];
228+
$domain = $options['domain'] ?: self::$defaults['domain'];
229+
$secure = $options['secure'] ?: self::$defaults['secure'];
230+
$httponly = $options['httponly'] ?: self::$defaults['httponly'];
231+
$samesite = $options['samesite'] ?: self::$defaults['samesite'];
232+
$raw = $options['raw'] ?: self::$defaults['raw'];
277233

278234
$this->validateName($name, $raw);
279235
$this->validatePrefix($prefix, $secure, $path, $domain);
@@ -282,7 +238,7 @@ final public function __construct(
282238
$this->prefix = $prefix;
283239
$this->name = $name;
284240
$this->value = $value;
285-
$this->expires = static::convertExpiresTimestamp($expires);
241+
$this->expires = static::convertExpiresTimestamp($options['expires']);
286242
$this->path = $path;
287243
$this->domain = $domain;
288244
$this->secure = $secure;
@@ -438,7 +394,7 @@ public function isRaw(): bool
438394
*/
439395
public function getOptions(): array
440396
{
441-
// This is the order of options in `setcookie`. DO NOT change.
397+
// This is the order of options in `setcookie`. DO NOT CHANGE.
442398
return [
443399
'expires' => $this->expires,
444400
'path' => $this->path,
@@ -496,7 +452,7 @@ public function withValue(string $value)
496452
/**
497453
* {@inheritDoc}
498454
*/
499-
public function withExpiresAt($expires = 0)
455+
public function withExpires($expires)
500456
{
501457
$cookie = clone $this;
502458

system/HTTP/ResponseTrait.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -607,17 +607,15 @@ public function setCookie(
607607
$expire = $expire > 0 ? time() + $expire : 0;
608608
}
609609

610-
$options = [
610+
$cookie = new Cookie($name, $value, [
611611
'expires' => $expire ?: 0,
612612
'domain' => $domain,
613613
'path' => $path,
614614
'prefix' => $prefix,
615615
'secure' => $secure,
616616
'httponly' => $httponly,
617617
'samesite' => $samesite ?? '',
618-
];
619-
620-
$cookie = Cookie::create($name, $value, $options);
618+
]);
621619

622620
$this->cookieStore = $this->cookieStore->put($cookie);
623621

system/Security/Security.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public function __construct(App $config)
149149
$expires = $security->expires ?? $config->CSRFExpire ?? 7200;
150150

151151
Cookie::setDefaults($cookie);
152-
$this->cookie = Cookie::create($rawCookieName, $this->generateHash(), [
152+
$this->cookie = new Cookie($rawCookieName, $this->generateHash(), [
153153
'expires' => $expires === 0 ? 0 : time() + $expires,
154154
]);
155155
}

system/Session/Session.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public function __construct(SessionHandlerInterface $driver, App $config)
192192
*/
193193
$config = config('Cookie');
194194

195-
$this->cookie = Cookie::create($this->sessionCookieName, '', [
195+
$this->cookie = new Cookie($this->sessionCookieName, '', [
196196
'expires' => $this->sessionExpiration === 0 ? 0 : time() + $this->sessionExpiration,
197197
'path' => $config->path,
198198
'domain' => $config->domain,
@@ -1009,7 +1009,7 @@ protected function startSession()
10091009
protected function setCookie()
10101010
{
10111011
$expiration = $this->sessionExpiration === 0 ? 0 : time() + $this->sessionExpiration;
1012-
$this->cookie = $this->cookie->withValue(session_id())->withExpiresAt($expiration);
1012+
$this->cookie = $this->cookie->withValue(session_id())->withExpires($expiration);
10131013

10141014
cookies([$this->cookie], false)->dispatch();
10151015
}

system/Test/Mock/MockSession.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ protected function startSession()
5757
protected function setCookie()
5858
{
5959
$expiration = $this->sessionExpiration === 0 ? 0 : time() + $this->sessionExpiration;
60-
$this->cookie = $this->cookie->withValue(session_id())->withExpiresAt($expiration);
60+
$this->cookie = $this->cookie->withValue(session_id())->withExpires($expiration);
6161

6262
$this->cookies[] = $this->cookie;
6363
}

tests/system/Cookie/CookieStoreTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ protected function tearDown(): void
3232
public function testCookieStoreInitialization(): void
3333
{
3434
$cookies = [
35-
Cookie::create('dev', 'cookie'),
36-
Cookie::create('prod', 'cookie', ['raw' => true]),
35+
new Cookie('dev', 'cookie'),
36+
new Cookie('prod', 'cookie', ['raw' => true]),
3737
];
3838

3939
$store = new CookieStore($cookies);
@@ -76,12 +76,12 @@ public function testInvalidCookieStored(): void
7676
public function testPutRemoveCookiesInStore(): void
7777
{
7878
$cookies = [
79-
Cookie::create('dev', 'cookie'),
80-
Cookie::create('prod', 'cookie', ['raw' => true]),
79+
new Cookie('dev', 'cookie'),
80+
new Cookie('prod', 'cookie', ['raw' => true]),
8181
];
8282

8383
$store = new CookieStore($cookies);
84-
$bottle = $store->put(Cookie::create('test', 'cookie'));
84+
$bottle = $store->put(new Cookie('test', 'cookie'));
8585
$jar = $store->remove('dev');
8686

8787
$this->assertNotSame($store->display(), $bottle->display());
@@ -94,8 +94,8 @@ public function testPutRemoveCookiesInStore(): void
9494
public function testCookieDispatching(): void
9595
{
9696
$cookies = [
97-
'dev' => Cookie::create('dev', 'cookie'),
98-
'prod' => Cookie::create('prod', 'cookie', ['raw' => true]),
97+
'dev' => new Cookie('dev', 'cookie'),
98+
'prod' => new Cookie('prod', 'cookie', ['raw' => true]),
9999
];
100100

101101
$dev = $cookies['dev']->getOptions();

0 commit comments

Comments
 (0)