Skip to content

Commit d0e8a07

Browse files
committed
address review
1 parent faa2c5b commit d0e8a07

3 files changed

Lines changed: 41 additions & 20 deletions

File tree

system/HTTP/Response.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace CodeIgniter\HTTP;
1515

16+
use CodeIgniter\Cookie\Cookie;
1617
use CodeIgniter\HTTP\Exceptions\HTTPException;
1718

1819
/**
@@ -142,6 +143,8 @@ class Response extends Message implements ResponseInterface
142143
*/
143144
public function __construct()
144145
{
146+
Cookie::setDefaults(config('Cookie'));
147+
145148
$this->noCache()->setContentType('text/html');
146149
}
147150

system/HTTP/ResponseTrait.php

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ public function setCookie(
568568
$httponly = null,
569569
$samesite = null,
570570
) {
571-
$store = $this->initializeCookieStore();
571+
$store = $this->getCookieStore();
572572

573573
if ($name instanceof Cookie) {
574574
$this->cookieStore = $store->put($name);
@@ -613,19 +613,24 @@ public function setCookie(
613613
/**
614614
* Returns the `CookieStore` instance.
615615
*
616+
* Lazily instantiates the `CookieStore` on first call, so that the Cookie and
617+
* CookieStore classes are not loaded on requests that do not use cookies.
618+
*
616619
* @return CookieStore
617620
*/
618621
public function getCookieStore()
619622
{
620-
return $this->initializeCookieStore();
623+
$this->cookieStore ??= new CookieStore([]);
624+
625+
return $this->cookieStore;
621626
}
622627

623628
/**
624629
* Checks to see if the Response has a specified cookie or not.
625630
*/
626631
public function hasCookie(string $name, ?string $value = null, string $prefix = ''): bool
627632
{
628-
$store = $this->initializeCookieStore();
633+
$store = $this->getCookieStore();
629634
$prefix = $prefix !== '' ? $prefix : Cookie::setDefaults()['prefix']; // to retain BC
630635

631636
return $store->has($name, $prefix, $value);
@@ -641,7 +646,7 @@ public function hasCookie(string $name, ?string $value = null, string $prefix =
641646
*/
642647
public function getCookie(?string $name = null, string $prefix = '')
643648
{
644-
$store = $this->initializeCookieStore();
649+
$store = $this->getCookieStore();
645650

646651
if ((string) $name === '') {
647652
return $store->display();
@@ -669,7 +674,7 @@ public function deleteCookie(string $name = '', string $domain = '', string $pat
669674
return $this;
670675
}
671676

672-
$store = $this->initializeCookieStore();
677+
$store = $this->getCookieStore();
673678
$prefix = $prefix !== '' ? $prefix : Cookie::setDefaults()['prefix']; // to retain BC
674679

675680
$prefixed = $prefix . $name;
@@ -812,21 +817,8 @@ public function download(string $filename = '', $data = '', bool $setMime = fals
812817

813818
public function getCSP(): ContentSecurityPolicy
814819
{
815-
return $this->CSP ??= service('csp');
816-
}
817-
818-
/**
819-
* Lazily initializes the cookie store and the Cookie class defaults.
820-
* Called by every cookie-related method so cookie machinery is only
821-
* loaded when the developer actually interacts with cookies.
822-
*/
823-
private function initializeCookieStore(): CookieStore
824-
{
825-
if ($this->cookieStore === null) {
826-
Cookie::setDefaults(config(CookieConfig::class));
827-
$this->cookieStore = new CookieStore([]);
828-
}
820+
$this->CSP ??= service('csp');
829821

830-
return $this->cookieStore;
822+
return $this->CSP;
831823
}
832824
}

tests/system/HTTP/ResponseTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use CodeIgniter\Config\Factories;
1717
use CodeIgniter\Config\Services;
18+
use CodeIgniter\Cookie\Cookie;
1819
use CodeIgniter\Cookie\CookieStore;
1920
use CodeIgniter\HTTP\Exceptions\HTTPException;
2021
use CodeIgniter\Superglobals;
@@ -753,4 +754,29 @@ public function testGetCookiesReturnsEmptyArrayWhenCookieStoreNotInitialized():
753754
'getCookies() must not instantiate the store when no cookies have been set.',
754755
);
755756
}
757+
758+
public function testGetCookieStillUsesSetCookieDefaultsWhenStoreNotInitialized(): void
759+
{
760+
$oldDefaults = Cookie::setDefaults();
761+
762+
config('Cookie')->prefix = 'test_';
763+
764+
try {
765+
$response = new Response();
766+
767+
$this->assertNull($this->getPrivateProperty($response, 'cookieStore'));
768+
769+
$response->setCookie('foo', 'bar');
770+
771+
$this->assertInstanceOf(CookieStore::class, $this->getPrivateProperty($response, 'cookieStore'));
772+
$this->assertTrue($response->hasCookie('foo'));
773+
774+
$cookie = $response->getCookie('foo');
775+
$this->assertSame('test_', $cookie->getPrefix());
776+
$this->assertSame('test_foo', $cookie->getPrefixedName());
777+
} finally {
778+
Cookie::setDefaults($oldDefaults);
779+
Factories::reset('config');
780+
}
781+
}
756782
}

0 commit comments

Comments
 (0)