• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

codeigniter4 / CodeIgniter4 / 24612044552

18 Apr 2026 07:21PM UTC coverage: 88.237% (+0.007%) from 88.23%
24612044552

Pull #10121

github

web-flow
Merge c06b36dab into d0607f1e0
Pull Request #10121: refactor: lazily load CSP and Cookie only when needed by Response

21 of 21 new or added lines in 2 files covered. (100.0%)

1 existing line in 1 file now uncovered.

22707 of 25734 relevant lines covered (88.24%)

219.33 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.55
/system/HTTP/ResponseTrait.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\HTTP;
15

16
use CodeIgniter\Config\Services;
17
use CodeIgniter\Cookie\Cookie;
18
use CodeIgniter\Cookie\CookieStore;
19
use CodeIgniter\Cookie\Exceptions\CookieException;
20
use CodeIgniter\Exceptions\InvalidArgumentException;
21
use CodeIgniter\HTTP\Exceptions\HTTPException;
22
use CodeIgniter\I18n\Time;
23
use CodeIgniter\Pager\PagerInterface;
24
use CodeIgniter\Security\Exceptions\SecurityException;
25
use Config\App;
26
use Config\ContentSecurityPolicy as ContentSecurityPolicyConfig;
27
use Config\Cookie as CookieConfig;
28
use DateTime;
29
use DateTimeZone;
30

31
/**
32
 * Response Trait
33
 *
34
 * Additional methods to make a PSR-7 Response class
35
 * compliant with the framework's own ResponseInterface.
36
 *
37
 * @property array<int, string> $statusCodes
38
 * @property string|null        $body
39
 *
40
 * @see https://github.com/php-fig/http-message/blob/master/src/ResponseInterface.php
41
 */
42
trait ResponseTrait
43
{
44
    /**
45
     * Content security policy handler.
46
     *
47
     * Lazily instantiated on first use via `self::getCSP()` so that the
48
     * ContentSecurityPolicy class is not loaded on requests that do not use CSP.
49
     *
50
     * @var ContentSecurityPolicy|null
51
     */
52
    protected $CSP;
53

54
    /**
55
     * CookieStore instance.
56
     *
57
     * Lazily instantiated on first cookie-related call so that the Cookie and
58
     * CookieStore classes are not loaded on requests that do not use cookies.
59
     *
60
     * @var CookieStore|null
61
     */
62
    protected $cookieStore;
63

64
    /**
65
     * Type of format the body is in.
66
     * Valid: html, json, xml
67
     *
68
     * @var string
69
     */
70
    protected $bodyFormat = 'html';
71

72
    /**
73
     * Return an instance with the specified status code and, optionally, reason phrase.
74
     *
75
     * If no reason phrase is specified, will default recommended reason phrase for
76
     * the response's status code.
77
     *
78
     * @see http://tools.ietf.org/html/rfc7231#section-6
79
     * @see http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
80
     *
81
     * @param int    $code   The 3-digit integer result code to set.
82
     * @param string $reason The reason phrase to use with the
83
     *                       provided status code; if none is provided, will
84
     *                       default to the IANA name.
85
     *
86
     * @return $this
87
     *
88
     * @throws HTTPException For invalid status code arguments.
89
     */
90
    public function setStatusCode(int $code, string $reason = '')
91
    {
92
        if ($code < 100 || $code > 599) {
291✔
93
            throw HTTPException::forInvalidStatusCode($code);
3✔
94
        }
95

96
        if (! array_key_exists($code, static::$statusCodes) && $reason === '') {
288✔
97
            throw HTTPException::forUnkownStatusCode($code);
1✔
98
        }
99

100
        $this->statusCode = $code;
287✔
101

102
        $this->reason = $reason !== '' ? $reason : static::$statusCodes[$code];
287✔
103

104
        return $this;
287✔
105
    }
106

107
    // --------------------------------------------------------------------
108
    // Convenience Methods
109
    // --------------------------------------------------------------------
110

111
    /**
112
     * Sets the date header
113
     *
114
     * @return $this
115
     */
116
    public function setDate(DateTime $date)
117
    {
118
        $date->setTimezone(new DateTimeZone('UTC'));
69✔
119

120
        $this->setHeader('Date', $date->format('D, d M Y H:i:s') . ' GMT');
69✔
121

122
        return $this;
69✔
123
    }
124

125
    /**
126
     * Set the Link Header
127
     *
128
     * @see http://tools.ietf.org/html/rfc5988
129
     *
130
     * @return $this
131
     *
132
     * @todo Recommend moving to Pager
133
     */
134
    public function setLink(PagerInterface $pager)
135
    {
136
        $links    = '';
1✔
137
        $previous = $pager->getPreviousPageURI();
1✔
138

139
        if (is_string($previous) && $previous !== '') {
1✔
140
            $links .= '<' . $pager->getPageURI($pager->getFirstPage()) . '>; rel="first",';
1✔
141
            $links .= '<' . $previous . '>; rel="prev"';
1✔
142
        }
143

144
        $next = $pager->getNextPageURI();
1✔
145

146
        if (is_string($next) && $next !== '' && is_string($previous) && $previous !== '') {
1✔
147
            $links .= ',';
1✔
148
        }
149

150
        if (is_string($next) && $next !== '') {
1✔
151
            $links .= '<' . $next . '>; rel="next",';
1✔
152
            $links .= '<' . $pager->getPageURI($pager->getLastPage()) . '>; rel="last"';
1✔
153
        }
154

155
        $this->setHeader('Link', $links);
1✔
156

157
        return $this;
1✔
158
    }
159

160
    /**
161
     * Sets the Content Type header for this response with the mime type
162
     * and, optionally, the charset.
163
     *
164
     * @return $this
165
     */
166
    public function setContentType(string $mime, string $charset = 'UTF-8')
167
    {
168
        // add charset attribute if not already there and provided as parm
169
        if ((strpos($mime, 'charset=') < 1) && ($charset !== '')) {
891✔
170
            $mime .= '; charset=' . $charset;
891✔
171
        }
172

173
        $this->removeHeader('Content-Type'); // replace existing content type
891✔
174
        $this->setHeader('Content-Type', $mime);
891✔
175

176
        return $this;
891✔
177
    }
178

179
    /**
180
     * Converts the $body into JSON and sets the Content Type header.
181
     *
182
     * @param array|object|string $body
183
     *
184
     * @return $this
185
     */
186
    public function setJSON($body, bool $unencoded = false)
187
    {
188
        $this->body = $this->formatBody($body, 'json' . ($unencoded ? '-unencoded' : ''));
68✔
189

190
        return $this;
68✔
191
    }
192

193
    /**
194
     * Returns the current body, converted to JSON is it isn't already.
195
     *
196
     * @return string|null
197
     *
198
     * @throws InvalidArgumentException If the body property is not array.
199
     */
200
    public function getJSON()
201
    {
202
        $body = $this->body;
17✔
203

204
        if ($this->bodyFormat !== 'json') {
17✔
205
            $body = service('format')->getFormatter('application/json')->format($body);
3✔
206
        }
207

208
        return $body ?: null;
17✔
209
    }
210

211
    /**
212
     * Converts $body into XML, and sets the correct Content-Type.
213
     *
214
     * @param array|string $body
215
     *
216
     * @return $this
217
     */
218
    public function setXML($body)
219
    {
220
        $this->body = $this->formatBody($body, 'xml');
5✔
221

222
        return $this;
5✔
223
    }
224

225
    /**
226
     * Retrieves the current body into XML and returns it.
227
     *
228
     * @return bool|string|null
229
     *
230
     * @throws InvalidArgumentException If the body property is not array.
231
     */
232
    public function getXML()
233
    {
234
        $body = $this->body;
4✔
235

236
        if ($this->bodyFormat !== 'xml') {
4✔
237
            $body = service('format')->getFormatter('application/xml')->format($body);
1✔
238
        }
239

240
        return $body;
4✔
241
    }
242

243
    /**
244
     * Handles conversion of the data into the appropriate format,
245
     * and sets the correct Content-Type header for our response.
246
     *
247
     * @param array|object|string $body
248
     * @param string              $format Valid: json, xml
249
     *
250
     * @return false|string
251
     *
252
     * @throws InvalidArgumentException If the body property is not string or array.
253
     */
254
    protected function formatBody($body, string $format)
255
    {
256
        $this->bodyFormat = ($format === 'json-unencoded' ? 'json' : $format);
72✔
257
        $mime             = "application/{$this->bodyFormat}";
72✔
258
        $this->setContentType($mime);
72✔
259

260
        // Nothing much to do for a string...
261
        if (! is_string($body) || $format === 'json-unencoded') {
72✔
262
            $body = service('format')->getFormatter($mime)->format($body);
16✔
263
        }
264

265
        return $body;
72✔
266
    }
267

268
    // --------------------------------------------------------------------
269
    // Cache Control Methods
270
    //
271
    // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9
272
    // --------------------------------------------------------------------
273

274
    /**
275
     * Sets the appropriate headers to ensure this response
276
     * is not cached by the browsers.
277
     *
278
     * @return $this
279
     *
280
     * @todo Recommend researching these directives, might need: 'private', 'no-transform', 'no-store', 'must-revalidate'
281
     *
282
     * @see DownloadResponse::noCache()
283
     */
284
    public function noCache()
285
    {
286
        $this->removeHeader('Cache-Control');
861✔
287
        $this->setHeader('Cache-Control', ['no-store', 'max-age=0', 'no-cache']);
861✔
288

289
        return $this;
861✔
290
    }
291

292
    /**
293
     * A shortcut method that allows the developer to set all of the
294
     * cache-control headers in one method call.
295
     *
296
     * The options array is used to provide the cache-control directives
297
     * for the header. It might look something like:
298
     *
299
     *      $options = [
300
     *          'max-age'  => 300,
301
     *          's-maxage' => 900
302
     *          'etag'     => 'abcde',
303
     *      ];
304
     *
305
     * Typical options are:
306
     *  - etag
307
     *  - last-modified
308
     *  - max-age
309
     *  - s-maxage
310
     *  - private
311
     *  - public
312
     *  - must-revalidate
313
     *  - proxy-revalidate
314
     *  - no-transform
315
     *
316
     * @return $this
317
     */
318
    public function setCache(array $options = [])
319
    {
320
        if ($options === []) {
3✔
321
            return $this;
1✔
322
        }
323

324
        $this->removeHeader('Cache-Control');
2✔
325
        $this->removeHeader('ETag');
2✔
326

327
        // ETag
328
        if (isset($options['etag'])) {
2✔
329
            $this->setHeader('ETag', $options['etag']);
2✔
330
            unset($options['etag']);
2✔
331
        }
332

333
        // Last Modified
334
        if (isset($options['last-modified'])) {
2✔
335
            $this->setLastModified($options['last-modified']);
2✔
336

337
            unset($options['last-modified']);
2✔
338
        }
339

340
        $this->setHeader('Cache-Control', $options);
2✔
341

342
        return $this;
2✔
343
    }
344

345
    /**
346
     * Sets the Last-Modified date header.
347
     *
348
     * $date can be either a string representation of the date or,
349
     * preferably, an instance of DateTime.
350
     *
351
     * @param DateTime|string $date
352
     *
353
     * @return $this
354
     */
355
    public function setLastModified($date)
356
    {
357
        if ($date instanceof DateTime) {
5✔
358
            $date->setTimezone(new DateTimeZone('UTC'));
2✔
359
            $this->setHeader('Last-Modified', $date->format('D, d M Y H:i:s') . ' GMT');
2✔
360
        } elseif (is_string($date)) {
3✔
361
            $this->setHeader('Last-Modified', $date);
3✔
362
        }
363

364
        return $this;
5✔
365
    }
366

367
    // --------------------------------------------------------------------
368
    // Output Methods
369
    // --------------------------------------------------------------------
370

371
    /**
372
     * Sends the output to the browser.
373
     *
374
     * @return $this
375
     */
376
    public function send()
377
    {
378
        // If we're enforcing a Content Security Policy,
379
        // we need to give it a chance to build out its headers.
380
        if ($this->shouldFinalizeCsp()) {
115✔
381
            $this->getCSP()->finalize($this);
65✔
382
        }
383

384
        $this->sendHeaders();
115✔
385
        $this->sendCookies();
115✔
386
        $this->sendBody();
114✔
387

388
        return $this;
114✔
389
    }
390

391
    /**
392
     * Decides whether {@see ContentSecurityPolicy::finalize()} should run for
393
     * this response. Keeping the CSP class unloaded on requests that do not
394
     * need it avoids the cost of constructing a 1000+ line service on every
395
     * request.
396
     */
397
    private function shouldFinalizeCsp(): bool
398
    {
399
        // Developer already touched CSP through getCSP(); respect it.
400
        if ($this->CSP !== null) {
115✔
401
            return true;
44✔
402
        }
403

404
        // A CSP instance has been registered (e.g., via Services::injectMock()
405
        // or any earlier service('csp') call) — reuse it instead of skipping.
406
        if (Services::has('csp')) {
71✔
407
            return true;
8✔
408
        }
409

410
        if (config(App::class)->CSPEnabled) {
63✔
411
            return true;
1✔
412
        }
413

414
        // Placeholders in the body still need to be stripped even when CSP
415
        // is disabled, so the body is scanned for the configured nonce tags
416
        // before committing to loading the full CSP class.
417
        $body = (string) $this->body;
62✔
418

419
        if ($body === '') {
62✔
420
            return false;
9✔
421
        }
422

423
        $cspConfig = config(ContentSecurityPolicyConfig::class);
53✔
424

425
        return str_contains($body, $cspConfig->scriptNonceTag)
53✔
426
            || str_contains($body, $cspConfig->styleNonceTag);
53✔
427
    }
428

429
    /**
430
     * Sends the headers of this HTTP response to the browser.
431
     *
432
     * @return $this
433
     */
434
    public function sendHeaders()
435
    {
436
        // Have the headers already been sent?
437
        if ($this->pretend || headers_sent()) {
119✔
438
            return $this;
53✔
439
        }
440

441
        // Per spec, MUST be sent with each request, if possible.
442
        // http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html
443
        if (! isset($this->headers['Date']) && PHP_SAPI !== 'cli-server') {
67✔
444
            $this->setDate(DateTime::createFromFormat('U', (string) Time::now()->getTimestamp()));
67✔
445
        }
446

447
        // HTTP Status
448
        header(sprintf('HTTP/%s %s %s', $this->getProtocolVersion(), $this->getStatusCode(), $this->getReasonPhrase()), true, $this->getStatusCode());
67✔
449

450
        // Send all of our headers
451
        foreach ($this->headers() as $name => $value) {
67✔
452
            if ($value instanceof Header) {
67✔
453
                header(
67✔
454
                    $name . ': ' . $value->getValueLine(),
67✔
455
                    true,
67✔
456
                    $this->getStatusCode(),
67✔
457
                );
67✔
458
            } else {
459
                $replace = true;
1✔
460

461
                foreach ($value as $header) {
1✔
462
                    header(
1✔
463
                        $name . ': ' . $header->getValueLine(),
1✔
464
                        $replace,
1✔
465
                        $this->getStatusCode(),
1✔
466
                    );
1✔
467
                    $replace = false;
1✔
468
                }
469
            }
470
        }
471

472
        return $this;
67✔
473
    }
474

475
    /**
476
     * Sends the Body of the message to the browser.
477
     *
478
     * @return $this
479
     */
480
    public function sendBody()
481
    {
482
        echo $this->body;
114✔
483

484
        return $this;
114✔
485
    }
486

487
    /**
488
     * Perform a redirect to a new URL, in two flavors: header or location.
489
     *
490
     * @param string   $uri  The URI to redirect to
491
     * @param int|null $code The type of redirection, defaults to 302
492
     *
493
     * @return $this
494
     *
495
     * @throws HTTPException For invalid status code.
496
     */
497
    public function redirect(string $uri, string $method = 'auto', ?int $code = null)
498
    {
499
        // IIS environment likely? Use 'refresh' for better compatibility
500
        $superglobals   = service('superglobals');
61✔
501
        $serverSoftware = $superglobals->server('SERVER_SOFTWARE');
61✔
502
        if (
503
            $method === 'auto'
61✔
504
            && $serverSoftware !== null
61✔
505
            && str_contains($serverSoftware, 'Microsoft-IIS')
61✔
506
        ) {
507
            $method = 'refresh';
6✔
508
        } elseif ($method !== 'refresh' && $code === null) {
55✔
509
            // override status code for HTTP/1.1 & higher
510
            $serverProtocol = $superglobals->server('SERVER_PROTOCOL');
36✔
511
            $requestMethod  = $superglobals->server('REQUEST_METHOD');
36✔
512
            if (
513
                $serverProtocol !== null
36✔
514
                && $requestMethod !== null
36✔
515
                && $this->getProtocolVersion() >= 1.1
36✔
516
            ) {
517
                if ($requestMethod === Method::GET) {
8✔
518
                    $code = 302;
2✔
519
                } elseif (in_array($requestMethod, [Method::POST, Method::PUT, Method::DELETE], true)) {
6✔
520
                    // reference: https://en.wikipedia.org/wiki/Post/Redirect/Get
521
                    $code = 303;
4✔
522
                } else {
523
                    $code = 307;
2✔
524
                }
525
            }
526
        }
527

528
        if ($code === null) {
61✔
529
            $code = 302;
31✔
530
        }
531

532
        match ($method) {
533
            'refresh' => $this->setHeader('Refresh', '0;url=' . $uri),
61✔
534
            default   => $this->setHeader('Location', $uri),
54✔
535
        };
536

537
        $this->setStatusCode($code);
61✔
538

539
        return $this;
61✔
540
    }
541

542
    /**
543
     * Set a cookie
544
     *
545
     * Accepts an arbitrary number of binds (up to 7) or an associative
546
     * array in the first parameter containing all the values.
547
     *
548
     * @param array|Cookie|string $name     Cookie name / array containing binds / Cookie object
549
     * @param string              $value    Cookie value
550
     * @param int                 $expire   Cookie expiration time in seconds
551
     * @param string              $domain   Cookie domain (e.g.: '.yourdomain.com')
552
     * @param string              $path     Cookie path (default: '/')
553
     * @param string              $prefix   Cookie name prefix ('': the default prefix)
554
     * @param bool|null           $secure   Whether to only transfer cookies via SSL
555
     * @param bool|null           $httponly Whether only make the cookie accessible via HTTP (no javascript)
556
     * @param string|null         $samesite
557
     *
558
     * @return $this
559
     */
560
    public function setCookie(
561
        $name,
562
        $value = '',
563
        $expire = 0,
564
        $domain = '',
565
        $path = '/',
566
        $prefix = '',
567
        $secure = null,
568
        $httponly = null,
569
        $samesite = null,
570
    ) {
571
        $store = $this->initializeCookieStore();
172✔
572

573
        if ($name instanceof Cookie) {
172✔
574
            $this->cookieStore = $store->put($name);
80✔
575

576
            return $this;
80✔
577
        }
578

579
        $cookieConfig = config(CookieConfig::class);
92✔
580

581
        $secure ??= $cookieConfig->secure;
92✔
582
        $httponly ??= $cookieConfig->httponly;
92✔
583
        $samesite ??= $cookieConfig->samesite;
92✔
584

585
        if (is_array($name)) {
92✔
586
            // always leave 'name' in last place, as the loop will break otherwise, due to ${$item}
587
            foreach (['samesite', 'value', 'expire', 'domain', 'path', 'prefix', 'secure', 'httponly', 'name'] as $item) {
19✔
588
                if (isset($name[$item])) {
19✔
589
                    ${$item} = $name[$item];
19✔
590
                }
591
            }
592
        }
593

594
        if (is_numeric($expire)) {
92✔
595
            $expire = $expire > 0 ? Time::now()->getTimestamp() + $expire : 0;
89✔
596
        }
597

598
        $cookie = new Cookie($name, $value, [
92✔
599
            'expires'  => $expire ?: 0,
92✔
600
            'domain'   => $domain,
92✔
601
            'path'     => $path,
92✔
602
            'prefix'   => $prefix,
92✔
603
            'secure'   => $secure,
92✔
604
            'httponly' => $httponly,
92✔
605
            'samesite' => $samesite ?? '',
92✔
606
        ]);
92✔
607

608
        $this->cookieStore = $store->put($cookie);
90✔
609

610
        return $this;
90✔
611
    }
612

613
    /**
614
     * Returns the `CookieStore` instance.
615
     *
616
     * @return CookieStore
617
     */
618
    public function getCookieStore()
619
    {
620
        return $this->initializeCookieStore();
6✔
621
    }
622

623
    /**
624
     * Checks to see if the Response has a specified cookie or not.
625
     */
626
    public function hasCookie(string $name, ?string $value = null, string $prefix = ''): bool
627
    {
628
        $store  = $this->initializeCookieStore();
29✔
629
        $prefix = $prefix !== '' ? $prefix : Cookie::setDefaults()['prefix']; // to retain BC
29✔
630

631
        return $store->has($name, $prefix, $value);
29✔
632
    }
633

634
    /**
635
     * Returns the cookie
636
     *
637
     * @param string $prefix Cookie prefix.
638
     *                       '': the default prefix
639
     *
640
     * @return array<string, Cookie>|Cookie|null
641
     */
642
    public function getCookie(?string $name = null, string $prefix = '')
643
    {
644
        $store = $this->initializeCookieStore();
20✔
645

646
        if ((string) $name === '') {
20✔
647
            return $store->display();
1✔
648
        }
649

650
        try {
651
            $prefix = $prefix !== '' ? $prefix : Cookie::setDefaults()['prefix']; // to retain BC
20✔
652

653
            return $store->get($name, $prefix);
20✔
654
        } catch (CookieException $e) {
1✔
655
            log_message('error', (string) $e);
1✔
656

657
            return null;
1✔
658
        }
659
    }
660

661
    /**
662
     * Sets a cookie to be deleted when the response is sent.
663
     *
664
     * @return $this
665
     */
666
    public function deleteCookie(string $name = '', string $domain = '', string $path = '/', string $prefix = '')
667
    {
668
        if ($name === '') {
11✔
669
            return $this;
1✔
670
        }
671

672
        $store  = $this->initializeCookieStore();
11✔
673
        $prefix = $prefix !== '' ? $prefix : Cookie::setDefaults()['prefix']; // to retain BC
11✔
674

675
        $prefixed = $prefix . $name;
11✔
676
        $found    = false;
11✔
677

678
        /** @var Cookie $cookie */
679
        foreach ($store as $cookie) {
11✔
680
            if ($cookie->getPrefixedName() === $prefixed) {
10✔
681
                if ($domain !== $cookie->getDomain()) {
10✔
682
                    continue;
1✔
683
                }
684

685
                if ($path !== $cookie->getPath()) {
10✔
686
                    continue;
1✔
687
                }
688

689
                $cookie = $cookie->withValue('')->withExpired();
10✔
690
                $found  = true;
10✔
691

692
                $this->cookieStore = $store->put($cookie);
10✔
693
                break;
10✔
694
            }
695
        }
696

697
        if (! $found) {
11✔
698
            $this->setCookie($name, '', 0, $domain, $path, $prefix);
2✔
699
        }
700

701
        return $this;
11✔
702
    }
703

704
    /**
705
     * Returns all cookies currently set.
706
     *
707
     * @return array<string, Cookie>
708
     */
709
    public function getCookies()
710
    {
711
        if ($this->cookieStore === null) {
10✔
712
            return [];
2✔
713
        }
714

715
        return $this->cookieStore->display();
9✔
716
    }
717

718
    /**
719
     * Actually sets the cookies.
720
     *
721
     * @return void
722
     */
723
    protected function sendCookies()
724
    {
725
        if ($this->pretend || $this->cookieStore === null) {
116✔
726
            return;
64✔
727
        }
728

729
        $this->dispatchCookies();
52✔
730
    }
731

732
    private function dispatchCookies(): void
733
    {
734
        /** @var IncomingRequest $request */
735
        $request = service('request');
52✔
736

737
        foreach ($this->cookieStore->display() as $cookie) {
52✔
738
            if ($cookie->isSecure() && ! $request->isSecure()) {
52✔
739
                throw SecurityException::forInsecureCookie();
1✔
740
            }
741

742
            $name    = $cookie->getPrefixedName();
51✔
743
            $value   = $cookie->getValue();
51✔
744
            $options = $cookie->getOptions();
51✔
745

746
            if ($cookie->isRaw()) {
51✔
UNCOV
747
                $this->doSetRawCookie($name, $value, $options);
×
748
            } else {
749
                $this->doSetCookie($name, $value, $options);
51✔
750
            }
751
        }
752

753
        $this->cookieStore->clear();
51✔
754
    }
755

756
    /**
757
     * Extracted call to `setrawcookie()` in order to run unit tests on it.
758
     *
759
     * @codeCoverageIgnore
760
     */
761
    private function doSetRawCookie(string $name, string $value, array $options): void
762
    {
763
        setrawcookie($name, $value, $options);
764
    }
765

766
    /**
767
     * Extracted call to `setcookie()` in order to run unit tests on it.
768
     *
769
     * @codeCoverageIgnore
770
     */
771
    private function doSetCookie(string $name, string $value, array $options): void
772
    {
773
        setcookie($name, $value, $options);
774
    }
775

776
    /**
777
     * Force a download.
778
     *
779
     * Generates the headers that force a download to happen. And
780
     * sends the file to the browser.
781
     *
782
     * @param string      $filename The name you want the downloaded file to be named
783
     *                              or the path to the file to send
784
     * @param string|null $data     The data to be downloaded. Set null if the $filename is the file path
785
     * @param bool        $setMime  Whether to try and send the actual MIME type
786
     *
787
     * @return DownloadResponse|null
788
     */
789
    public function download(string $filename = '', $data = '', bool $setMime = false)
790
    {
791
        if ($filename === '' || $data === '') {
4✔
792
            return null;
1✔
793
        }
794

795
        $filepath = '';
3✔
796
        if ($data === null) {
3✔
797
            $filepath = $filename;
1✔
798
            $filename = explode('/', str_replace(DIRECTORY_SEPARATOR, '/', $filename));
1✔
799
            $filename = end($filename);
1✔
800
        }
801

802
        $response = new DownloadResponse($filename, $setMime);
3✔
803

804
        if ($filepath !== '') {
3✔
805
            $response->setFilePath($filepath);
1✔
806
        } elseif ($data !== null) {
2✔
807
            $response->setBinary($data);
2✔
808
        }
809

810
        return $response;
3✔
811
    }
812

813
    public function getCSP(): ContentSecurityPolicy
814
    {
815
        return $this->CSP ??= service('csp');
92✔
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
        $this->cookieStore ??= new CookieStore([]);
178✔
826

827
        return $this->cookieStore;
178✔
828
    }
829
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc