From 861d7692c77e92b99489ff801f57435483ab473b Mon Sep 17 00:00:00 2001 From: mychidarko Date: Thu, 11 Aug 2022 00:58:55 +0000 Subject: [PATCH 1/3] feat: add functions to swap out http objects --- src/App.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/App.php b/src/App.php index fe43189..0ec194f 100755 --- a/src/App.php +++ b/src/App.php @@ -254,6 +254,30 @@ public function config($name, $value = null) $this->setupErrorHandler(); } + /** + * Swap out Leaf request instance + * + * @param mixed $class The new request class to attach + */ + public function setRequestClass($class) + { + $this->container->singleton('request', function () { + return new \Leaf\Http\Request(); + }); + } + + /** + * Swap out Leaf response instance + * + * @param mixed $class The new response class to attach + */ + public function setResponseClass($class) + { + $this->container->singleton('response', function () use($class) { + return new $class; + }); + } + /******************************************************************************** * Logging *******************************************************************************/ From f9fb9156fd22414b0f24d7e301257f8ba445ce18 Mon Sep 17 00:00:00 2001 From: mychidarko Date: Thu, 11 Aug 2022 01:08:01 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=E2=9C=85=20add=20test=20for=20swapping=20r?= =?UTF-8?q?esponse=20class?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/app.test.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/app.test.php b/tests/app.test.php index e88d157..e68ccf8 100644 --- a/tests/app.test.php +++ b/tests/app.test.php @@ -119,3 +119,17 @@ public function call() expect($app->config('inTest2'))->toBe('false'); }); + +test('swap out leaf response', function () { + class TestResponse extends \Leaf\Http\Response { + public function customMethod() + { + return 'This is some test response'; + } + } + + $leafInstance1 = new \Leaf\App(); + $leafInstance1->setResponseClass(TestResponse::class); + + expect($leafInstance1->response->customMethod())->toBe('This is some test response'); +}); From 1a9ee7fe1c09e18267b4dee5b17e82bbaf1f16d2 Mon Sep 17 00:00:00 2001 From: mychidarko Date: Thu, 11 Aug 2022 01:08:26 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=91=B7=20Fix=20styling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.php | 46 +++++++++++++++++++++++----------------------- tests/app.test.php | 19 ++++++++++--------- 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/src/App.php b/src/App.php index 0ec194f..73c85c6 100755 --- a/src/App.php +++ b/src/App.php @@ -254,29 +254,29 @@ public function config($name, $value = null) $this->setupErrorHandler(); } - /** - * Swap out Leaf request instance - * - * @param mixed $class The new request class to attach - */ - public function setRequestClass($class) - { - $this->container->singleton('request', function () { - return new \Leaf\Http\Request(); - }); - } - - /** - * Swap out Leaf response instance - * - * @param mixed $class The new response class to attach - */ - public function setResponseClass($class) - { - $this->container->singleton('response', function () use($class) { - return new $class; - }); - } + /** + * Swap out Leaf request instance + * + * @param mixed $class The new request class to attach + */ + public function setRequestClass($class) + { + $this->container->singleton('request', function () { + return new \Leaf\Http\Request(); + }); + } + + /** + * Swap out Leaf response instance + * + * @param mixed $class The new response class to attach + */ + public function setResponseClass($class) + { + $this->container->singleton('response', function () use ($class) { + return new $class(); + }); + } /******************************************************************************** * Logging diff --git a/tests/app.test.php b/tests/app.test.php index e68ccf8..1512099 100644 --- a/tests/app.test.php +++ b/tests/app.test.php @@ -121,15 +121,16 @@ public function call() }); test('swap out leaf response', function () { - class TestResponse extends \Leaf\Http\Response { - public function customMethod() - { - return 'This is some test response'; - } - } + class TestResponse extends \Leaf\Http\Response + { + public function customMethod() + { + return 'This is some test response'; + } + } - $leafInstance1 = new \Leaf\App(); - $leafInstance1->setResponseClass(TestResponse::class); + $leafInstance1 = new \Leaf\App(); + $leafInstance1->setResponseClass(TestResponse::class); - expect($leafInstance1->response->customMethod())->toBe('This is some test response'); + expect($leafInstance1->response->customMethod())->toBe('This is some test response'); });