@@ -221,6 +221,11 @@ public function exit($data, int $code = 500)
221221 */
222222 public function redirect (string $ url , int $ status = 302 )
223223 {
224+ if (class_exists ('Leaf\Eien\Server ' ) && PHP_SAPI === 'cli ' ) {
225+ \Leaf \Config::set ('response.headers ' , array_merge ($ this ->headers , ['Location ' => $ url ]));
226+ return ;
227+ }
228+
224229 Headers::status ($ status );
225230 Headers::set ('Location ' , $ url , true , $ status );
226231 }
@@ -252,9 +257,18 @@ public function withHeader($name, ?string $value = '', $replace = true, int $htt
252257
253258 if (is_array ($ name )) {
254259 $ 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+
255265 return $ this ;
256266 }
257267
268+ if (class_exists ('Leaf\Eien\Server ' ) && PHP_SAPI === 'cli ' ) {
269+ \Leaf \Config::set ('response.headers ' , array_merge ($ this ->headers , [$ name => $ value ]));
270+ }
271+
258272 if ($ replace === false || $ httpCode !== 200 ) {
259273 Headers::set ($ name , $ value , $ replace , $ httpCode );
260274 } else {
@@ -273,12 +287,11 @@ public function withHeader($name, ?string $value = '', $replace = true, int $htt
273287 */
274288 public function withCookie (string $ name , string $ value , string $ expire = "7 days " )
275289 {
276- if (!class_exists ('Leaf\Http\Cookie ' )) {
277- Headers::contentHtml ();
278- trigger_error ('Leaf cookie not found. Run `leaf install cookie` or `composer require leafs/cookie` ' );
279- }
290+ $ this ->cookies [] = [$ name , $ value , $ expire ];
280291
281- Cookie::simpleCookie ($ name , $ value , $ expire );
292+ if (class_exists ('Leaf\Eien\Server ' ) && PHP_SAPI === 'cli ' ) {
293+ \Leaf \Config::set ('response.cookies ' , $ this ->cookies );
294+ }
282295
283296 return $ this ;
284297 }
@@ -290,12 +303,11 @@ public function withCookie(string $name, string $value, string $expire = "7 days
290303 */
291304 public function withoutCookie ($ name )
292305 {
293- if (!class_exists ('Leaf\Http\Cookie ' )) {
294- Headers::contentHtml ();
295- trigger_error ('Leaf cookie not found. Run `leaf install cookie` or `composer require leafs/cookie` ' );
296- }
306+ $ this ->cookies [] = [$ name , '' , -1 ];
297307
298- Cookie::unset ($ name );
308+ if (class_exists ('Leaf\Eien\Server ' ) && PHP_SAPI === 'cli ' ) {
309+ \Leaf \Config::set ('response.cookies ' , $ this ->cookies );
310+ }
299311
300312 return $ this ;
301313 }
@@ -344,6 +356,11 @@ public static function getMessageForCode(int $status): ?string
344356 */
345357 public function sendHeaders ()
346358 {
359+ if (class_exists ('Leaf\Eien\Server ' ) && PHP_SAPI === 'cli ' ) {
360+ \Leaf \Config::set ('response.headers ' , $ this ->headers );
361+ return $ this ;
362+ }
363+
347364 // headers have already been sent by the developer
348365 if (headers_sent ()) {
349366 return $ this ;
@@ -357,6 +374,28 @@ public function sendHeaders()
357374 return $ this ;
358375 }
359376
377+ /**
378+ * Send cookies
379+ */
380+ public function sendCookies ()
381+ {
382+ if (class_exists ('Leaf\Eien\Server ' ) && PHP_SAPI === 'cli ' ) {
383+ \Leaf \Config::set ('response.cookies ' , $ this ->cookies );
384+ return $ this ;
385+ }
386+
387+ if (!class_exists ('Leaf\Http\Cookie ' )) {
388+ Headers::contentHtml ();
389+ trigger_error ('Leaf cookie not found. Run `leaf install cookie` or `composer require leafs/cookie` ' );
390+ }
391+
392+ foreach ($ this ->cookies as $ cookie ) {
393+ Cookie::set (...$ cookie );
394+ }
395+
396+ return $ this ;
397+ }
398+
360399 /**
361400 * Sends content for the current web response.
362401 *
@@ -382,15 +421,10 @@ public function sendContent()
382421 */
383422 public function send ()
384423 {
385- $ this ->sendHeaders ()->sendContent ();
386-
387- if (class_exists ('Leaf\Eien\Server ' )) {
388- \Leaf \Config::set ('response.data ' , [
389- 'headers ' => $ this ->headers ,
390- 'body ' => strpos ($ this ->headers ['Content-Disposition ' ] ?? '' , 'attachment ' ) !== false
391- ? readfile ($ this ->content )
392- : $ this ->content ,
393- ]);
424+ $ this ->sendHeaders ()->sendCookies ()->sendContent ();
425+
426+ if (class_exists ('Leaf\Eien\Server ' ) && PHP_SAPI === 'cli ' ) {
427+ \Leaf \Config::set ('response.headers ' , $ this ->headers );
394428 }
395429
396430 if (\function_exists ('fastcgi_finish_request ' )) {
0 commit comments