Skip to content

Commit d472ec1

Browse files
committed
refactor: move cookie dispatching to Response class
1 parent d94cca9 commit d472ec1

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

system/Cookie/CookieStore.php

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

1414
use ArrayIterator;
1515
use CodeIgniter\Cookie\Exceptions\CookieException;
16-
use CodeIgniter\Security\Exceptions\SecurityException;
17-
use Config\Services;
1816
use Countable;
1917
use IteratorAggregate;
2018
use Traversable;
@@ -160,16 +158,12 @@ public function remove(string $name, string $prefix = '')
160158

161159
/**
162160
* Dispatches all cookies in store.
161+
*
162+
* @deprecated Response should dispatch cookies.
163163
*/
164164
public function dispatch(): void
165165
{
166-
$request = Services::request();
167-
168166
foreach ($this->cookies as $cookie) {
169-
if ($cookie->isSecure() && ! $request->isSecure()) {
170-
throw SecurityException::forDisallowedAction();
171-
}
172-
173167
$name = $cookie->getPrefixedName();
174168
$value = $cookie->getValue();
175169
$options = $cookie->getOptions();
@@ -240,6 +234,8 @@ protected function validateCookies(array $cookies): void
240234
* Extracted call to `setrawcookie()` in order to run unit tests on it.
241235
*
242236
* @codeCoverageIgnore
237+
*
238+
* @deprecated
243239
*/
244240
protected function setRawCookie(string $name, string $value, array $options): void
245241
{
@@ -250,6 +246,8 @@ protected function setRawCookie(string $name, string $value, array $options): vo
250246
* Extracted call to `setcookie()` in order to run unit tests on it.
251247
*
252248
* @codeCoverageIgnore
249+
*
250+
* @deprecated
253251
*/
254252
protected function setCookie(string $name, string $value, array $options): void
255253
{

system/HTTP/ResponseTrait.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use CodeIgniter\Cookie\Exceptions\CookieException;
1717
use CodeIgniter\HTTP\Exceptions\HTTPException;
1818
use CodeIgniter\Pager\PagerInterface;
19+
use CodeIgniter\Security\Exceptions\SecurityException;
1920
use Config\Services;
2021
use DateTime;
2122
use DateTimeZone;
@@ -697,7 +698,51 @@ protected function sendCookies()
697698
return;
698699
}
699700

700-
$this->cookieStore->dispatch();
701+
$this->dispatchCookies();
702+
}
703+
704+
private function dispatchCookies(): void
705+
{
706+
/** @var IncomingRequest $request */
707+
$request = Services::request();
708+
709+
foreach ($this->cookieStore->display() as $cookie) {
710+
if ($cookie->isSecure() && ! $request->isSecure()) {
711+
throw SecurityException::forDisallowedAction();
712+
}
713+
714+
$name = $cookie->getPrefixedName();
715+
$value = $cookie->getValue();
716+
$options = $cookie->getOptions();
717+
718+
if ($cookie->isRaw()) {
719+
$this->doSetRawCookie($name, $value, $options);
720+
} else {
721+
$this->doSetCookie($name, $value, $options);
722+
}
723+
}
724+
725+
$this->cookieStore->clear();
726+
}
727+
728+
/**
729+
* Extracted call to `setrawcookie()` in order to run unit tests on it.
730+
*
731+
* @codeCoverageIgnore
732+
*/
733+
private function doSetRawCookie(string $name, string $value, array $options): void
734+
{
735+
setrawcookie($name, $value, $options);
736+
}
737+
738+
/**
739+
* Extracted call to `setcookie()` in order to run unit tests on it.
740+
*
741+
* @codeCoverageIgnore
742+
*/
743+
private function doSetCookie(string $name, string $value, array $options): void
744+
{
745+
setcookie($name, $value, $options);
701746
}
702747

703748
/**

0 commit comments

Comments
 (0)