From a353f8e828520159363a46f84852c5a2ed26ffa7 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Wed, 3 Jan 2024 02:21:46 +0300 Subject: [PATCH 1/6] Refactoring: Added Route Options as Class --- .../framework/test/router/RouterTest.php | 106 +++++++++--------- webfiori/framework/router/RouteOption.php | 72 ++++++++++++ 2 files changed, 126 insertions(+), 52 deletions(-) create mode 100644 webfiori/framework/router/RouteOption.php diff --git a/tests/webfiori/framework/test/router/RouterTest.php b/tests/webfiori/framework/test/router/RouterTest.php index 6bb18a32..97d8b122 100644 --- a/tests/webfiori/framework/test/router/RouterTest.php +++ b/tests/webfiori/framework/test/router/RouterTest.php @@ -2,9 +2,11 @@ namespace webfiori\framework\test\router; use PHPUnit\Framework\TestCase; +use webfiori\framework\router\RouteOption; use webfiori\framework\router\Router; use webfiori\framework\router\RouterUri; use webfiori\framework\Util; +use webfiori\http\RequestMethod; /** * Description of RouterTest * @@ -23,14 +25,14 @@ public function test00() { */ public function testAddAPIRoute00() { $this->assertTrue(Router::api([ - 'path' => '/call-api-00', - 'route-to' => '/my-api.php'])); + RouteOption::PATH => '/call-api-00', + RouteOption::TO => '/my-api.php'])); $this->assertFalse(Router::page([ - 'path' => '/call-api-00', - 'route-to' => '/my-other-api.php'])); + RouteOption::PATH => '/call-api-00', + RouteOption::TO => '/my-other-api.php'])); $this->assertTrue(Router::page([ - 'path' => '/call-api-01', - 'route-to' => '/my-api.php'])); + RouteOption::PATH => '/call-api-01', + RouteOption::TO => '/my-api.php'])); } /** * @test @@ -43,20 +45,20 @@ public function testAddClosureRoute00() { { }; $this->assertTrue(Router::closure([ - 'path' => '/call', - 'route-to' => $c1 + RouteOption::PATH => '/call', + RouteOption::TO => $c1 ])); $this->assertFalse(Router::closure([ - 'path' => '/call', - 'route-to' => $c2 + RouteOption::PATH => '/call', + RouteOption::TO => $c2 ])); $this->assertTrue(Router::closure([ - 'path' => '/call-2', - 'route-to' => $c1 + RouteOption::PATH => '/call-2', + RouteOption::TO => $c1 ])); $this->assertFalse(Router::closure([ - 'path' => '/call', - 'route-to' => 'Not Func' + RouteOption::PATH => '/call', + RouteOption::TO => 'Not Func' ])); } /** @@ -64,14 +66,14 @@ public function testAddClosureRoute00() { */ public function testAddViewRoute00() { $this->assertTrue(Router::page([ - 'path' => '/view-something', - 'route-to' => 'my-view.php'])); + RouteOption::PATH => '/view-something', + RouteOption::TO => 'my-view.php'])); $this->assertFalse(Router::page([ - 'path' => '/view-something', - 'route-to' => '/my-other-view.php'])); + RouteOption::PATH => '/view-something', + RouteOption::TO => '/my-other-view.php'])); $this->assertTrue(Router::page([ - 'path' => '/view-something-2', - 'route-to' => '/my-view.php'])); + RouteOption::PATH => '/view-something-2', + RouteOption::TO => '/my-view.php'])); } /** * @test @@ -82,11 +84,11 @@ public function testOptionalParam00() { { }); Router::closure([ - 'path' => '{var-1}/{var-2?}', - 'route-to' => function() + RouteOption::PATH => '{var-1}/{var-2?}', + RouteOption::TO => function() { }, - 'vars-values' => [ + RouteOption::VALUES => [ 'var-1' => [ 'hello' ] @@ -108,8 +110,8 @@ public function testOptionalParam01() { Router::removeAll(); Router::closure([ - 'path' => '{var-1}/{var-2?}', - 'route-to' => function() + RouteOption::PATH => '{var-1}/{var-2?}', + RouteOption::TO => function() { } ]); @@ -130,8 +132,8 @@ public function testRoute00() { { }); Router::closure([ - 'path' => '{var-1}/{var-2}', - 'route-to' => function() + RouteOption::PATH => '{var-1}/{var-2}', + RouteOption::TO => function() { } ]); @@ -153,8 +155,8 @@ public function testRoute01() { { }); Router::closure([ - 'path' => '{var-1}/{var-2}/{var-1}', - 'route-to' => function() + RouteOption::PATH => '{var-1}/{var-2}/{var-1}', + RouteOption::TO => function() { } ]); @@ -170,39 +172,39 @@ public function testRoute01() { public function testRoutesGroup00() { Router::removeAll(); Router::page([ - 'path' => 'users', - 'case-sensitive' => false, - 'middleware' => 'M1', - 'languages' => ['EN'], - 'methods' => 'post', - 'routes' => [ + RouteOption::PATH => 'users', + RouteOption::CASE_SENSITIVE => false, + RouteOption::MIDDLEWARE => 'M1', + RouteOption::LANGS => ['EN'], + RouteOption::REQUEST_METHODS => RequestMethod::POST, + RouteOption::SUB_ROUTES => [ [ - 'path' => 'view-user/{user-id}', - 'route-to' => 'ViewUserPage.php', - 'languages' => ['AR'] + RouteOption::PATH => 'view-user/{user-id}', + RouteOption::TO => 'ViewUserPage.php', + RouteOption::LANGS => ['AR'] ], [ - 'path' => 'get-users', - 'languages' => ['AR'], - 'case-sensitive' => true, - 'routes' => [ + RouteOption::PATH => 'get-users', + RouteOption::LANGS => ['AR'], + RouteOption::CASE_SENSITIVE => true, + RouteOption::SUB_ROUTES => [ [ - 'path' => 'by-name', - 'route-to' => 'GetUserByName.php', - 'languages' => ['FR'], - 'case-sensitive' => false, + RouteOption::PATH => 'by-name', + RouteOption::TO => 'GetUserByName.php', + RouteOption::LANGS => ['FR'], + RouteOption::CASE_SENSITIVE => false, ], [ - 'path' => 'by-email', - 'route-to' => 'GetUserByEmail.php' + RouteOption::PATH => 'by-email', + RouteOption::TO => 'GetUserByEmail.php' ] ], ], [ - 'path' => '/', - 'route-to' => 'ListUsers.php', - 'case-sensitive' => true, - 'methods' => ['options', 'get'] + RouteOption::PATH => '/', + RouteOption::TO => 'ListUsers.php', + RouteOption::CASE_SENSITIVE => true, + RouteOption::REQUEST_METHODS => [RequestMethod::OPTIONS, RequestMethod::GET] ] ] ]); diff --git a/webfiori/framework/router/RouteOption.php b/webfiori/framework/router/RouteOption.php new file mode 100644 index 00000000..c97bc121 --- /dev/null +++ b/webfiori/framework/router/RouteOption.php @@ -0,0 +1,72 @@ + Date: Wed, 3 Jan 2024 02:26:15 +0300 Subject: [PATCH 2/6] Update Router.php --- webfiori/framework/router/Router.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webfiori/framework/router/Router.php b/webfiori/framework/router/Router.php index 80e3ef0e..795eed6c 100644 --- a/webfiori/framework/router/Router.php +++ b/webfiori/framework/router/Router.php @@ -228,7 +228,7 @@ private function __construct() { public static function addRoute(array $options) : bool { $options['type'] = Router::CUSTOMIZED; - return Router::get()->addRouteHelper1($options); + return Router::getInstance()->addRouteHelper1($options); } /** * Adds new route to a web services set. From d6d9ee5c092a392578fba03dd9cfd7ea7136ee35 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Wed, 3 Jan 2024 02:33:20 +0300 Subject: [PATCH 3/6] Update CreateWebService.php --- webfiori/framework/cli/helpers/CreateWebService.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/webfiori/framework/cli/helpers/CreateWebService.php b/webfiori/framework/cli/helpers/CreateWebService.php index c37d729e..86b185be 100644 --- a/webfiori/framework/cli/helpers/CreateWebService.php +++ b/webfiori/framework/cli/helpers/CreateWebService.php @@ -13,8 +13,8 @@ use webfiori\framework\cli\commands\CreateCommand; use webfiori\framework\writers\ServiceHolder; use webfiori\framework\writers\WebServiceWriter; -use webfiori\http\AbstractWebService; use webfiori\http\ParamTypes; +use webfiori\http\RequestMethod; use webfiori\http\RequestParameter; /** @@ -37,7 +37,7 @@ public function readClassInfo() { $this->setClassInfo(APP_DIR.'\\apis', 'Service'); $this->setServiceName(); - $this->serviceObj->addRequestMethod($this->select('Request method:', AbstractWebService::METHODS, 0)); + $this->serviceObj->addRequestMethod($this->select('Request method:', RequestMethod::getAll(), 0)); if ($this->confirm('Would you like to add request parameters to the service?', false)) { $this->addParamsToService(); From e62455a80452305cebdac3f0605a81dceaaf3fef Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Wed, 3 Jan 2024 02:48:48 +0300 Subject: [PATCH 4/6] Update CreateWebService.php --- webfiori/framework/cli/helpers/CreateWebService.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/webfiori/framework/cli/helpers/CreateWebService.php b/webfiori/framework/cli/helpers/CreateWebService.php index 86b185be..8a50fa38 100644 --- a/webfiori/framework/cli/helpers/CreateWebService.php +++ b/webfiori/framework/cli/helpers/CreateWebService.php @@ -37,7 +37,9 @@ public function readClassInfo() { $this->setClassInfo(APP_DIR.'\\apis', 'Service'); $this->setServiceName(); - $this->serviceObj->addRequestMethod($this->select('Request method:', RequestMethod::getAll(), 0)); + $methods = RequestMethod::getAll(); + array_multisort($methods); + $this->serviceObj->addRequestMethod($this->select('Request method:', $methods, 2)); if ($this->confirm('Would you like to add request parameters to the service?', false)) { $this->addParamsToService(); From 9fec9e84a4c746dc3dac6446d7906008afe3fc6b Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Wed, 3 Jan 2024 02:48:54 +0300 Subject: [PATCH 5/6] Update CreateCommandTest.php --- .../framework/test/cli/CreateCommandTest.php | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/tests/webfiori/framework/test/cli/CreateCommandTest.php b/tests/webfiori/framework/test/cli/CreateCommandTest.php index ae2dd4ee..b00b6181 100644 --- a/tests/webfiori/framework/test/cli/CreateCommandTest.php +++ b/tests/webfiori/framework/test/cli/CreateCommandTest.php @@ -88,7 +88,8 @@ public function testCreateWebService00() { 'webfiori', 'create' ]); - $this->assertEquals(0, $runner->start()); + $result = $runner->start(); + //$this->assertEquals(0, $result); $this->assertEquals([ "What would you like to create?\n", "0: Database table class.\n", @@ -105,15 +106,15 @@ public function testCreateWebService00() { "Enter an optional namespace for the class: Enter = 'app\apis'\n", "Enter a name for the new web service:\n", "Request method:\n", - "0: GET <--\n", - "1: HEAD\n", - "2: POST\n", - "3: PUT\n", - "4: DELETE\n", - "5: TRACE\n", - "6: OPTIONS\n", - "7: PATCH\n", - "8: CONNECT\n", + "0: CONNECT\n", + "1: DELETE\n", + "2: GET <--\n", + "3: HEAD\n", + "4: OPTIONS\n", + "5: POST\n", + "6: PUT\n", + "7: TRACE\n", + "Would you like to add request parameters to the service?(y/N)\n", "Choose parameter type:\n", "0: array <--\n", From 62204beef59c408c6f61a1d3c3b5923522e6a315 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Wed, 3 Jan 2024 02:49:11 +0300 Subject: [PATCH 6/6] Update CreateCommandTest.php --- tests/webfiori/framework/test/cli/CreateCommandTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/webfiori/framework/test/cli/CreateCommandTest.php b/tests/webfiori/framework/test/cli/CreateCommandTest.php index b00b6181..556d1232 100644 --- a/tests/webfiori/framework/test/cli/CreateCommandTest.php +++ b/tests/webfiori/framework/test/cli/CreateCommandTest.php @@ -89,7 +89,7 @@ public function testCreateWebService00() { 'create' ]); $result = $runner->start(); - //$this->assertEquals(0, $result); + $this->assertEquals(0, $result); $this->assertEquals([ "What would you like to create?\n", "0: Database table class.\n",