Skip to content

Commit 4841326

Browse files
Create Config\Cookie for Cookie class (#4508)
1 parent df8730a commit 4841326

14 files changed

Lines changed: 413 additions & 291 deletions

File tree

app/Config/App.php

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Config;
44

55
use CodeIgniter\Config\BaseConfig;
6-
use DateTimeInterface;
76

87
class App extends BaseConfig
98
{
@@ -242,6 +241,8 @@ class App extends BaseConfig
242241
* Set a cookie name prefix if you need to avoid collisions.
243242
*
244243
* @var string
244+
*
245+
* @deprecated use Config\Cookie::$prefix property instead.
245246
*/
246247
public $cookiePrefix = '';
247248

@@ -253,6 +254,8 @@ class App extends BaseConfig
253254
* Set to `.your-domain.com` for site-wide cookies.
254255
*
255256
* @var string
257+
*
258+
* @deprecated use Config\Cookie::$domain property instead.
256259
*/
257260
public $cookieDomain = '';
258261

@@ -264,6 +267,8 @@ class App extends BaseConfig
264267
* Typically will be a forward slash.
265268
*
266269
* @var string
270+
*
271+
* @deprecated use Config\Cookie::$path property instead.
267272
*/
268273
public $cookiePath = '/';
269274

@@ -275,6 +280,8 @@ class App extends BaseConfig
275280
* Cookie will only be set if a secure HTTPS connection exists.
276281
*
277282
* @var boolean
283+
*
284+
* @deprecated use Config\Cookie::$secure property instead.
278285
*/
279286
public $cookieSecure = false;
280287

@@ -286,6 +293,8 @@ class App extends BaseConfig
286293
* Cookie will only be accessible via HTTP(S) (no JavaScript).
287294
*
288295
* @var boolean
296+
*
297+
* @deprecated use Config\Cookie::$httponly property instead.
289298
*/
290299
public $cookieHTTPOnly = true;
291300

@@ -310,40 +319,11 @@ class App extends BaseConfig
310319
* will be set on cookies. If set to `None`, `$cookieSecure` must also be set.
311320
*
312321
* @var string
322+
*
323+
* @deprecated use Config\Cookie::$samesite property instead.
313324
*/
314325
public $cookieSameSite = 'Lax';
315326

316-
/**
317-
* --------------------------------------------------------------------------
318-
* Cookie Raw
319-
* --------------------------------------------------------------------------
320-
*
321-
* This flag allows setting a "raw" cookie, i.e., its name and value are
322-
* not URL encoded using `rawurlencode()`.
323-
*
324-
* If this is set to `true`, cookie names should be compliant of RFC 2616's
325-
* list of allowed characters.
326-
*
327-
* @var boolean
328-
*
329-
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#attributes
330-
* @see https://tools.ietf.org/html/rfc2616#section-2.2
331-
*/
332-
public $cookieRaw = false;
333-
334-
/**
335-
* --------------------------------------------------------------------------
336-
* Cookie Expires Timestamp
337-
* --------------------------------------------------------------------------
338-
*
339-
* Default expires timestamp for cookies. Setting this to `0` will mean the
340-
* cookie will not have the `Expires` attribute and will behave as a session
341-
* cookie.
342-
*
343-
* @var DateTimeInterface|integer|string
344-
*/
345-
public $cookieExpires = 0;
346-
347327
/**
348328
* --------------------------------------------------------------------------
349329
* Reverse Proxy IPs

app/Config/Cookie.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
namespace Config;
4+
5+
use CodeIgniter\Config\BaseConfig;
6+
use DateTimeInterface;
7+
8+
class Cookie extends BaseConfig
9+
{
10+
/**
11+
* --------------------------------------------------------------------------
12+
* Cookie Prefix
13+
* --------------------------------------------------------------------------
14+
*
15+
* Set a cookie name prefix if you need to avoid collisions.
16+
*
17+
* @var string
18+
*/
19+
public $prefix = '';
20+
21+
/**
22+
* --------------------------------------------------------------------------
23+
* Cookie Expires Timestamp
24+
* --------------------------------------------------------------------------
25+
*
26+
* Default expires timestamp for cookies. Setting this to `0` will mean the
27+
* cookie will not have the `Expires` attribute and will behave as a session
28+
* cookie.
29+
*
30+
* @var DateTimeInterface|integer|string
31+
*/
32+
public $expires = 0;
33+
34+
/**
35+
* --------------------------------------------------------------------------
36+
* Cookie Path
37+
* --------------------------------------------------------------------------
38+
*
39+
* Typically will be a forward slash.
40+
*
41+
* @var string
42+
*/
43+
public $path = '/';
44+
45+
/**
46+
* --------------------------------------------------------------------------
47+
* Cookie Domain
48+
* --------------------------------------------------------------------------
49+
*
50+
* Set to `.your-domain.com` for site-wide cookies.
51+
*
52+
* @var string
53+
*/
54+
public $domain = '';
55+
56+
/**
57+
* --------------------------------------------------------------------------
58+
* Cookie Secure
59+
* --------------------------------------------------------------------------
60+
*
61+
* Cookie will only be set if a secure HTTPS connection exists.
62+
*
63+
* @var boolean
64+
*/
65+
public $secure = false;
66+
67+
/**
68+
* --------------------------------------------------------------------------
69+
* Cookie HTTPOnly
70+
* --------------------------------------------------------------------------
71+
*
72+
* Cookie will only be accessible via HTTP(S) (no JavaScript).
73+
*
74+
* @var boolean
75+
*/
76+
public $httponly = true;
77+
78+
/**
79+
* --------------------------------------------------------------------------
80+
* Cookie SameSite
81+
* --------------------------------------------------------------------------
82+
*
83+
* Configure cookie SameSite setting. Allowed values are:
84+
* - None
85+
* - Lax
86+
* - Strict
87+
* - ''
88+
*
89+
* Alternatively, you can use the constant names:
90+
* - `Cookie::SAMESITE_NONE`
91+
* - `Cookie::SAMESITE_LAX`
92+
* - `Cookie::SAMESITE_STRICT`
93+
*
94+
* Defaults to `Lax` for compatibility with modern browsers. Setting `''`
95+
* (empty string) means default SameSite attribute set by browsers (`Lax`)
96+
* will be set on cookies. If set to `None`, `$secure` must also be set.
97+
*
98+
* @var string
99+
*/
100+
public $samesite = 'Lax';
101+
102+
/**
103+
* --------------------------------------------------------------------------
104+
* Cookie Raw
105+
* --------------------------------------------------------------------------
106+
*
107+
* This flag allows setting a "raw" cookie, i.e., its name and value are
108+
* not URL encoded using `rawurlencode()`.
109+
*
110+
* If this is set to `true`, cookie names should be compliant of RFC 2616's
111+
* list of allowed characters.
112+
*
113+
* @var boolean
114+
*
115+
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#attributes
116+
* @see https://tools.ietf.org/html/rfc2616#section-2.2
117+
*/
118+
public $raw = false;
119+
}

env

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,19 @@
9191
# contentsecuritypolicy.sandbox = false
9292
# contentsecuritypolicy.upgradeInsecureRequests = false
9393

94+
#--------------------------------------------------------------------
95+
# COOKIE
96+
#--------------------------------------------------------------------
97+
98+
# cookie.prefix = ''
99+
# cookie.expires = 0
100+
# cookie.path = '/'
101+
# cookie.domain = ''
102+
# cookie.secure = false
103+
# cookie.httponly = false
104+
# cookie.samesite = 'Lax'
105+
# cookie.raw = false
106+
94107
#--------------------------------------------------------------------
95108
# ENCRYPTION
96109
#--------------------------------------------------------------------

system/Cookie/CloneableCookieInterface.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,6 @@
1919
*/
2020
interface CloneableCookieInterface extends CookieInterface
2121
{
22-
/**
23-
* Creates a new Cookie with URL encoding option updated.
24-
*
25-
* @param boolean $raw
26-
*
27-
* @return static
28-
*/
29-
public function withRaw(bool $raw = true);
30-
3122
/**
3223
* Creates a new Cookie with a new cookie prefix.
3324
*
@@ -79,22 +70,22 @@ public function withExpired();
7970
public function withNeverExpiring();
8071

8172
/**
82-
* Creates a new Cookie with a new domain the cookie is available.
73+
* Creates a new Cookie with a new path on the server the cookie is available.
8374
*
84-
* @param string|null $domain
75+
* @param string|null $path
8576
*
8677
* @return static
8778
*/
88-
public function withDomain(?string $domain);
79+
public function withPath(?string $path);
8980

9081
/**
91-
* Creates a new Cookie with a new path on the server the cookie is available.
82+
* Creates a new Cookie with a new domain the cookie is available.
9283
*
93-
* @param string|null $path
84+
* @param string|null $domain
9485
*
9586
* @return static
9687
*/
97-
public function withPath(?string $path);
88+
public function withDomain(?string $domain);
9889

9990
/**
10091
* Creates a new Cookie with a new "Secure" attribute.
@@ -108,18 +99,27 @@ public function withSecure(bool $secure = true);
10899
/**
109100
* Creates a new Cookie with a new "HttpOnly" attribute
110101
*
111-
* @param boolean $httpOnly
102+
* @param boolean $httponly
112103
*
113104
* @return static
114105
*/
115-
public function withHttpOnly(bool $httpOnly = true);
106+
public function withHTTPOnly(bool $httponly = true);
116107

117108
/**
118109
* Creates a new Cookie with a new "SameSite" attribute.
119110
*
120-
* @param string $sameSite
111+
* @param string $samesite
121112
*
122113
* @return static
123114
*/
124-
public function withSameSite(string $sameSite);
115+
public function withSameSite(string $samesite);
116+
117+
/**
118+
* Creates a new Cookie with URL encoding option updated.
119+
*
120+
* @param boolean $raw
121+
*
122+
* @return static
123+
*/
124+
public function withRaw(bool $raw = true);
125125
}

0 commit comments

Comments
 (0)