Skip to content

Commit db6ea4e

Browse files
committed
fix: remove cookie restriction
1 parent 593dc1b commit db6ea4e

File tree

1 file changed

+32
-34
lines changed

1 file changed

+32
-34
lines changed

src/Response.php

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function httpVersion(?string $version = null)
5656

5757
/**
5858
* Output plain text
59-
*
59+
*
6060
* @param mixed $data The data to output
6161
* @param int $code The response status code
6262
*/
@@ -75,7 +75,7 @@ public function plain($data, int $code = 200)
7575
* @param string $data The data to output
7676
* @param int $code The response status code
7777
*/
78-
public function xml($data, int $code = 200)
78+
public function xml(string $data, int $code = 200)
7979
{
8080
$this->status = $code;
8181
$this->headers['Content-Type'] = 'application/xml';
@@ -86,7 +86,7 @@ public function xml($data, int $code = 200)
8686

8787
/**
8888
* Output json encoded data with an HTTP code/message
89-
*
89+
*
9090
* @param mixed $data The data to output
9191
* @param int $code The response status code
9292
* @param bool $showCode Show response code in body?
@@ -115,7 +115,7 @@ public function json($data, int $code = 200, bool $showCode = false)
115115

116116
/**
117117
* Output data from an HTML or PHP file
118-
*
118+
*
119119
* @param string $file The file to output
120120
* @param int $code The http status code
121121
*/
@@ -134,7 +134,7 @@ public function page(string $file, int $code = 200)
134134

135135
/**
136136
* Output some html/PHP
137-
*
137+
*
138138
* @param string $markup The data to output
139139
* @param int $code The http status code
140140
*/
@@ -151,7 +151,7 @@ public function markup(string $markup, int $code = 200)
151151

152152
/**
153153
* Output plain text
154-
*
154+
*
155155
* @param string $file Path to the file to download
156156
* @param string|null $name The of the file as shown to user
157157
* @param int $code The response status code
@@ -190,7 +190,7 @@ public function noContent()
190190

191191
/**
192192
* Output some data and break the application
193-
*
193+
*
194194
* @param mixed $data The data to output
195195
* @param int $code The Http status code
196196
*/
@@ -232,25 +232,26 @@ public function redirect(string $url, int $status = 302)
232232

233233
/**
234234
* Force set HTTP status code
235-
*
236-
* @param int $httpCode The response code to set
235+
*
236+
* @param int|null code The response code to set
237237
*/
238-
public function status($code = null)
238+
public function status(?int $code = null): Response
239239
{
240240
$this->status = $code;
241241
Headers::status($code);
242+
242243
return $this;
243244
}
244245

245246
/**
246247
* set header
247-
*
248+
*
248249
* @param string|array $name Header name
249250
* @param string|null $value Header value
250251
* @param boolean $replace Replace existing header
251252
* @param int $httpCode The HTTP status code
252253
*/
253-
public function withHeader($name, ?string $value = '', $replace = true, int $httpCode = 200)
254+
public function withHeader($name, ?string $value = '', bool $replace = true, int $httpCode = 200): Response
254255
{
255256
if (class_exists('Leaf\Eien\Server') && PHP_SAPI === 'cli') {
256257
$this->headers = array_merge(
@@ -285,9 +286,11 @@ public function withHeader($name, ?string $value = '', $replace = true, int $htt
285286
*
286287
* @param string $name The name of the cookie
287288
* @param string $value The value of cookie
288-
* @param string $expire When the cookie expires. Default: 7 days
289+
* @param int|null $expire When the cookie expires. Default: 7 days
290+
*
291+
* @return Response
289292
*/
290-
public function withCookie(string $name, string $value, int $expire = null)
293+
public function withCookie(string $name, string $value, int $expire = null): Response
291294
{
292295
$this->cookies[$name] = [$value, $expire ?? (time() + 604800)];
293296

@@ -303,7 +306,7 @@ public function withCookie(string $name, string $value, int $expire = null)
303306
*
304307
* @param mixed $name The name of the cookie
305308
*/
306-
public function withoutCookie($name)
309+
public function withoutCookie($name): Response
307310
{
308311
$this->cookies[$name] = ['', -1];
309312

@@ -317,10 +320,10 @@ public function withoutCookie($name)
317320
/**
318321
* Flash a piece of data to the session.
319322
*
320-
* @param string|array $name The key of the item to set
323+
* @param string|array key The key of the item to set
321324
* @param string $value The value of flash item
322325
*/
323-
public function withFlash($key, string $value)
326+
public function withFlash($key, string $value): Response
324327
{
325328
if (!class_exists('Leaf\Http\Session')) {
326329
Headers::contentHtml();
@@ -340,7 +343,7 @@ public function withFlash($key, string $value)
340343

341344
/**
342345
* Get message for HTTP status code
343-
*
346+
*
344347
* @param int $status
345348
* @return string|null
346349
*/
@@ -351,12 +354,10 @@ public static function getMessageForCode(int $status): ?string
351354

352355
/**
353356
* Sends HTTP headers.
354-
*
355-
* @param int $code The http status code to attach
356357
*
357358
* @return $this
358359
*/
359-
public function sendHeaders()
360+
public function sendHeaders(): Response
360361
{
361362
if (class_exists('Leaf\Eien\Server') && PHP_SAPI === 'cli') {
362363
\Leaf\Config::set('response.headers', $this->headers);
@@ -379,33 +380,28 @@ public function sendHeaders()
379380
/**
380381
* Send cookies
381382
*/
382-
public function sendCookies()
383+
public function sendCookies(): Response
383384
{
384385
if (class_exists('Leaf\Eien\Server') && PHP_SAPI === 'cli') {
385386
\Leaf\Config::set('response.cookies', $this->cookies);
386387
return $this;
387388
}
388389

389-
if (!class_exists('Leaf\Http\Cookie')) {
390-
Headers::contentHtml();
391-
trigger_error('Leaf cookie not found. Run `leaf install cookie` or `composer require leafs/cookie`');
392-
}
393-
394-
foreach ($this->cookies as $key => $value) {
395-
Cookie::set($key, $value[0], ['expire' => $value[1]]);
390+
if (class_exists('Leaf\Http\Cookie')) {
391+
foreach ($this->cookies as $key => $value) {
392+
Cookie::set($key, $value[0], ['expire' => $value[1]]);
393+
}
396394
}
397395

398396
return $this;
399397
}
400398

401399
/**
402400
* Sends content for the current web response.
403-
*
404-
* @param string $content The content to output
405401
*
406402
* @return $this
407403
*/
408-
public function sendContent()
404+
public function sendContent(): Response
409405
{
410406
if (strpos($this->headers['Content-Disposition'] ?? '', 'attachment') !== false) {
411407
readfile($this->content);
@@ -418,10 +414,10 @@ public function sendContent()
418414

419415
/**
420416
* Send the Http headers and content
421-
*
417+
*
422418
* @return $this
423419
*/
424-
public function send()
420+
public function send(): Response
425421
{
426422
$this->sendHeaders()->sendCookies()->sendContent();
427423

@@ -432,6 +428,8 @@ public function send()
432428
} elseif (!\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true)) {
433429
$this->closeOutputBuffers(0, true);
434430
}
431+
432+
return $this;
435433
}
436434

437435
/**

0 commit comments

Comments
 (0)