From 3068acc87e1407e6e909dcd2c6e3ae78aa9beece Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Thu, 21 Mar 2024 17:59:59 +0000 Subject: [PATCH 1/3] chore: fix default param --- src/App.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/App.php b/src/App.php index b8ce4184..dd9cb926 100755 --- a/src/App.php +++ b/src/App.php @@ -598,10 +598,16 @@ protected function getArguments(Hook $hook, array $values, array $requestParams) $existsInValues = \array_key_exists($key, $values); $paramExists = $existsInRequest || $existsInValues; - $arg = $existsInRequest ? $requestParams[$key] : $param['default']; - if (\is_callable($arg)) { - $arg = \call_user_func_array($arg, $this->getResources($param['injections'])); + if ($existsInRequest) { + $arg = $requestParams[$key]; + } else { + if (!is_string($param['default']) && \is_callable($param['default'])) { + $arg = \call_user_func_array($param['default'], $this->getResources($param['injections'])); + } else { + $arg = $param['default']; + } } + $value = $existsInValues ? $values[$key] : $arg; if (!$param['skipValidation']) { From f11f6d56d8a5560a4f2e64198651df909f845b0d Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Thu, 21 Mar 2024 18:06:19 +0000 Subject: [PATCH 2/3] chore: add test for callable params --- composer.lock | 49 +++++++++++++++++++++++------------------------ tests/AppTest.php | 16 ++++++++++++++++ 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/composer.lock b/composer.lock index 6ecc6744..bbff81f8 100644 --- a/composer.lock +++ b/composer.lock @@ -733,16 +733,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.10.60", + "version": "1.10.64", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "95dcea7d6c628a3f2f56d091d8a0219485a86bbe" + "reference": "fb9f270daffedcb5ff46275dcafe92538b1bc4bb" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/95dcea7d6c628a3f2f56d091d8a0219485a86bbe", - "reference": "95dcea7d6c628a3f2f56d091d8a0219485a86bbe", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/fb9f270daffedcb5ff46275dcafe92538b1bc4bb", + "reference": "fb9f270daffedcb5ff46275dcafe92538b1bc4bb", "shasum": "" }, "require": { @@ -791,7 +791,7 @@ "type": "tidelift" } ], - "time": "2024-03-07T13:30:19+00:00" + "time": "2024-03-21T09:57:47+00:00" }, { "name": "phpunit/php-code-coverage", @@ -1114,16 +1114,16 @@ }, { "name": "phpunit/phpunit", - "version": "9.6.17", + "version": "9.6.18", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "1a156980d78a6666721b7e8e8502fe210b587fcd" + "reference": "32c2c2d6580b1d8ab3c10b1e9e4dc263cc69bb04" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/1a156980d78a6666721b7e8e8502fe210b587fcd", - "reference": "1a156980d78a6666721b7e8e8502fe210b587fcd", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/32c2c2d6580b1d8ab3c10b1e9e4dc263cc69bb04", + "reference": "32c2c2d6580b1d8ab3c10b1e9e4dc263cc69bb04", "shasum": "" }, "require": { @@ -1197,7 +1197,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", "security": "https://github.com/sebastianbergmann/phpunit/security/policy", - "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.17" + "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.18" }, "funding": [ { @@ -1213,7 +1213,7 @@ "type": "tidelift" } ], - "time": "2024-02-23T13:14:51+00:00" + "time": "2024-03-21T12:07:32+00:00" }, { "name": "psr/cache", @@ -2169,16 +2169,16 @@ }, { "name": "sebastian/resource-operations", - "version": "3.0.3", + "version": "3.0.4", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/resource-operations.git", - "reference": "0f4443cb3a1d92ce809899753bc0d5d5a8dd19a8" + "reference": "05d5692a7993ecccd56a03e40cd7e5b09b1d404e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/resource-operations/zipball/0f4443cb3a1d92ce809899753bc0d5d5a8dd19a8", - "reference": "0f4443cb3a1d92ce809899753bc0d5d5a8dd19a8", + "url": "https://api.github.com/repos/sebastianbergmann/resource-operations/zipball/05d5692a7993ecccd56a03e40cd7e5b09b1d404e", + "reference": "05d5692a7993ecccd56a03e40cd7e5b09b1d404e", "shasum": "" }, "require": { @@ -2190,7 +2190,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "3.0-dev" + "dev-main": "3.0-dev" } }, "autoload": { @@ -2211,8 +2211,7 @@ "description": "Provides a list of PHP built-in functions that operate on resources", "homepage": "https://www.github.com/sebastianbergmann/resource-operations", "support": { - "issues": "https://github.com/sebastianbergmann/resource-operations/issues", - "source": "https://github.com/sebastianbergmann/resource-operations/tree/3.0.3" + "source": "https://github.com/sebastianbergmann/resource-operations/tree/3.0.4" }, "funding": [ { @@ -2220,7 +2219,7 @@ "type": "github" } ], - "time": "2020-09-28T06:45:17+00:00" + "time": "2024-03-14T16:00:52+00:00" }, { "name": "sebastian/type", @@ -3348,16 +3347,16 @@ }, { "name": "webmozart/glob", - "version": "4.6.0", + "version": "4.7.0", "source": { "type": "git", "url": "https://github.com/webmozarts/glob.git", - "reference": "3c17f7dec3d9d0e87b575026011f2e75a56ed655" + "reference": "8a2842112d6916e61e0e15e316465b611f3abc17" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/webmozarts/glob/zipball/3c17f7dec3d9d0e87b575026011f2e75a56ed655", - "reference": "3c17f7dec3d9d0e87b575026011f2e75a56ed655", + "url": "https://api.github.com/repos/webmozarts/glob/zipball/8a2842112d6916e61e0e15e316465b611f3abc17", + "reference": "8a2842112d6916e61e0e15e316465b611f3abc17", "shasum": "" }, "require": { @@ -3391,9 +3390,9 @@ "description": "A PHP implementation of Ant's glob.", "support": { "issues": "https://github.com/webmozarts/glob/issues", - "source": "https://github.com/webmozarts/glob/tree/4.6.0" + "source": "https://github.com/webmozarts/glob/tree/4.7.0" }, - "time": "2022-05-24T19:45:58+00:00" + "time": "2024-03-07T20:33:40+00:00" } ], "aliases": [], diff --git a/tests/AppTest.php b/tests/AppTest.php index c3c2580c..38f720dd 100755 --- a/tests/AppTest.php +++ b/tests/AppTest.php @@ -142,6 +142,22 @@ public function testCanGetDefaultValueWithFunction(): void \ob_end_clean(); $this->assertEquals('first-second-second', $result); + + /** Try to pass a param that is a PHP callable */ + $route = new Route('GET', '/path'); + $route + ->param('x', '', new Text(200), 'x param', true, ['first', 'second']) + ->action(function ($x) { + echo $x; + }); + + $request = new UtopiaRequestTest(); + $request::_setParams(['x' => 'count']); + $this->app->execute($route, $request, new Response()); + $result = \ob_get_contents(); + \ob_end_clean(); + + $this->assertEquals('count', $result); } public function testCanExecuteRoute(): void From d333880cf4903a121dee316cb0e122b7cd4cfebb Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Thu, 21 Mar 2024 18:07:23 +0000 Subject: [PATCH 3/3] chore: linter --- src/App.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/App.php b/src/App.php index dd9cb926..1458838c 100755 --- a/src/App.php +++ b/src/App.php @@ -607,7 +607,7 @@ protected function getArguments(Hook $hook, array $values, array $requestParams) $arg = $param['default']; } } - + $value = $existsInValues ? $values[$key] : $arg; if (!$param['skipValidation']) {