@@ -19,7 +19,7 @@ class Response
1919 * @var array
2020 */
2121 public $ headers = [];
22-
22+
2323 /**
2424 * @var array
2525 */
@@ -222,7 +222,7 @@ public function exit($data, int $code = 500)
222222 public function redirect (string $ url , int $ status = 302 )
223223 {
224224 if (class_exists ('Leaf\Eien\Server ' ) && PHP_SAPI === 'cli ' ) {
225- \Leaf \Config::set ('response.headers ' , array_merge ( $ this -> headers , [ ' Location ' => $ url ]) );
225+ \Leaf \Config::set ('response.redirect ' , [ $ url , $ status ] );
226226 return ;
227227 }
228228
@@ -252,23 +252,25 @@ public function status($code = null)
252252 */
253253 public function withHeader ($ name , ?string $ value = '' , $ replace = true , int $ httpCode = 200 )
254254 {
255+ if (class_exists ('Leaf\Eien\Server ' ) && PHP_SAPI === 'cli ' ) {
256+ $ this ->headers = array_merge (
257+ $ this ->headers ,
258+ is_array ($ name ) ? $ name : [$ name => $ value ]
259+ );
260+
261+ \Leaf \Config::set ('response.headers ' , $ this ->headers );
262+
263+ return $ this ;
264+ }
265+
255266 $ this ->status = $ httpCode ;
256267 Headers::status ($ httpCode );
257268
258269 if (is_array ($ name )) {
259270 $ this ->headers = array_merge ($ this ->headers , $ name );
260-
261- if (class_exists ('Leaf\Eien\Server ' ) && PHP_SAPI === 'cli ' ) {
262- \Leaf \Config::set ('response.headers ' , $ this ->headers );
263- }
264-
265271 return $ this ;
266272 }
267273
268- if (class_exists ('Leaf\Eien\Server ' ) && PHP_SAPI === 'cli ' ) {
269- \Leaf \Config::set ('response.headers ' , array_merge ($ this ->headers , [$ name => $ value ]));
270- }
271-
272274 if ($ replace === false || $ httpCode !== 200 ) {
273275 Headers::set ($ name , $ value , $ replace , $ httpCode );
274276 } else {
@@ -285,9 +287,9 @@ public function withHeader($name, ?string $value = '', $replace = true, int $htt
285287 * @param string $value The value of cookie
286288 * @param string $expire When the cookie expires. Default: 7 days
287289 */
288- public function withCookie (string $ name , string $ value , string $ expire = " 7 days " )
290+ public function withCookie (string $ name , string $ value , int $ expire = null )
289291 {
290- $ this ->cookies [] = [$ name , $ value , $ expire ];
292+ $ this ->cookies [$ name ] = [$ value , $ expire ?? ( time () + 604800 ) ];
291293
292294 if (class_exists ('Leaf\Eien\Server ' ) && PHP_SAPI === 'cli ' ) {
293295 \Leaf \Config::set ('response.cookies ' , $ this ->cookies );
@@ -303,7 +305,7 @@ public function withCookie(string $name, string $value, string $expire = "7 days
303305 */
304306 public function withoutCookie ($ name )
305307 {
306- $ this ->cookies [] = [$ name , '' , -1 ];
308+ $ this ->cookies [$ name ] = ['' , -1 ];
307309
308310 if (class_exists ('Leaf\Eien\Server ' ) && PHP_SAPI === 'cli ' ) {
309311 \Leaf \Config::set ('response.cookies ' , $ this ->cookies );
@@ -389,8 +391,8 @@ public function sendCookies()
389391 trigger_error ('Leaf cookie not found. Run `leaf install cookie` or `composer require leafs/cookie` ' );
390392 }
391393
392- foreach ($ this ->cookies as $ cookie ) {
393- Cookie::set (... $ cookie );
394+ foreach ($ this ->cookies as $ key => $ value ) {
395+ Cookie::set ($ key , $ value [ 0 ], [ ' expire ' => $ value [ 1 ]] );
394396 }
395397
396398 return $ this ;
@@ -423,10 +425,6 @@ public function send()
423425 {
424426 $ this ->sendHeaders ()->sendCookies ()->sendContent ();
425427
426- if (class_exists ('Leaf\Eien\Server ' ) && PHP_SAPI === 'cli ' ) {
427- \Leaf \Config::set ('response.headers ' , $ this ->headers );
428- }
429-
430428 if (\function_exists ('fastcgi_finish_request ' )) {
431429 fastcgi_finish_request ();
432430 } elseif (\function_exists ('litespeed_finish_request ' )) {
@@ -457,24 +455,4 @@ public static function closeOutputBuffers(int $targetLevel, bool $flush): void
457455 }
458456 }
459457 }
460-
461- /**
462- * Returns the Response as an HTTP string.
463- *
464- * The string representation of the Response is the same as the
465- * one that will be sent to the client only if the prepare() method
466- * has been called before.
467- *
468- * @return string
469- */
470- public function __toString ()
471- {
472- Headers::contentPlain ();
473- return
474- sprintf ('%s %s %s ' , $ this ->httpVersion (), $ this ->status , Status::$ statusTexts [$ this ->status ]) . "\r\n" .
475- implode ("" , array_map (function ($ key , $ value ) {
476- return sprintf ("%s: %s \r\n" , $ key , $ value );
477- }, array_keys ($ this ->headers ), array_values ($ this ->headers ))) .
478- $ this ->content ;
479- }
480458}
0 commit comments