From 9e81d1d870a51c295285aabeead8fb12bc231ae1 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Sat, 20 Apr 2024 23:52:26 +0900 Subject: [PATCH] Add Halo handling to HaloRenderer and tests This commit includes a modification in HaloRenderer class where a new method 'setHaloCookie' checks if PHP_SAPI is 'cli' before setting the cookie. The 'render' method has also been modified to --- src/Halo/HaloRenderer.php | 16 ++++++++++++++-- tests/Fake/app/public/index.php | 2 +- tests/Halo/HaloModuleTest.php | 1 + 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Halo/HaloRenderer.php b/src/Halo/HaloRenderer.php index 1dc7bdd..b580981 100644 --- a/src/Halo/HaloRenderer.php +++ b/src/Halo/HaloRenderer.php @@ -39,6 +39,7 @@ use const JSON_UNESCAPED_SLASHES; use const JSON_UNESCAPED_UNICODE; use const PHP_EOL; +use const PHP_SAPI; final class HaloRenderer implements RenderInterface { @@ -63,7 +64,9 @@ public function __construct( public function render(ResourceObject $ro) { if (! $this->isEableHalo()) { - return $this->renderer->render($ro); + $ro->view = $this->renderer->render($ro); + + return $ro->view; } $originalView = $this->renderer->render($ro); @@ -82,7 +85,7 @@ private function isEableHalo(): bool } if ($_GET[self::HALO_KEY] === '1') { - setcookie(self::HALO_COOKIE_KEY, '1'); + $this->setHaloCookie(); return true; } @@ -290,4 +293,13 @@ private function getProfileInfo(ResourceObject $ro): string return $html . ''; } + + public function setHaloCookie(): void + { + if (PHP_SAPI === 'cli') { + return; + } + + setcookie(self::HALO_COOKIE_KEY, '1'); + } } diff --git a/tests/Fake/app/public/index.php b/tests/Fake/app/public/index.php index 9e74476..a516f86 100644 --- a/tests/Fake/app/public/index.php +++ b/tests/Fake/app/public/index.php @@ -7,4 +7,4 @@ require dirname(__DIR__) . '/vendor/autoload.php'; require dirname(__DIR__, 4) . '/vendor/autoload.php'; -exit((new Bootstrap())(PHP_SAPI === 'cli-server' ? 'dev-html-app' : 'dev-prod-html-app', $GLOBALS, $_SERVER)); +exit((new Bootstrap())(PHP_SAPI === 'cli-server' ? 'dev-app' : 'dev-prod-app', $GLOBALS, $_SERVER)); diff --git a/tests/Halo/HaloModuleTest.php b/tests/Halo/HaloModuleTest.php index efb13b5..18086b5 100644 --- a/tests/Halo/HaloModuleTest.php +++ b/tests/Halo/HaloModuleTest.php @@ -14,6 +14,7 @@ class HaloModuleTest extends TestCase protected function setUp(): void { + $_GET['halo'] = '1'; $injector = Injector::getInstance('dev-app'); $this->resource = $injector->getInstance(ResourceInterface::class); }