Skip to content

Commit

Permalink
Merge pull request #201 from WebFiori/dev
Browse files Browse the repository at this point in the history
Refactoring: Added Route Options as Class
  • Loading branch information
usernane authored Jan 2, 2024
2 parents b418cc9 + 62204be commit 93adfb2
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 65 deletions.
21 changes: 11 additions & 10 deletions tests/webfiori/framework/test/cli/CreateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
106 changes: 54 additions & 52 deletions tests/webfiori/framework/test/router/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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
Expand All @@ -43,35 +45,35 @@ 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'
]));
}
/**
* @test
*/
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
Expand All @@ -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'
]
Expand All @@ -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()
{
}
]);
Expand All @@ -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()
{
}
]);
Expand All @@ -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()
{
}
]);
Expand All @@ -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]
]
]
]);
Expand Down
6 changes: 4 additions & 2 deletions webfiori/framework/cli/helpers/CreateWebService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -37,7 +37,9 @@ public function readClassInfo() {
$this->setClassInfo(APP_DIR.'\\apis', 'Service');

$this->setServiceName();
$this->serviceObj->addRequestMethod($this->select('Request method:', AbstractWebService::METHODS, 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();
Expand Down
72 changes: 72 additions & 0 deletions webfiori/framework/router/RouteOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* This file is licensed under MIT License.
*
* Copyright (c) 2019 Ibrahim BinAlshikh
*
* For more information on the license, please visit:
* https://github.com/WebFiori/.github/blob/main/LICENSE
*
*/

namespace webfiori\framework\router;

/**
* A class which is used to hold route options as constants.
*
* @author Ibrahim
*/
class RouteOption {
/**
* An option that represents the path part of the URI.
*/
const PATH = 'path';
/**
* An option which is used to set the resource at which the route will point to.
*/
const TO = 'route-to';
/**
* An option which is used to set route type.
*/
const TYPE = 'type';
/**
* An option which is used to indicate if path is case sensitive or not.
*/
const CASE_SENSITIVE = 'case-sensitive';
/**
* An option which is used to tell if the route should be part of auto-generated sitemap or not.
*/
const SITEMAP = 'in-sitemap';
/**
* An option which is used to treat the route as an API call.
*/
const API = 'as-api';
/**
* An option which is used to set an array as closure parameters (applies to routes of type closure only)
*/
const CLOSURE_PARAMS = 'closure-params';
/**
* An option which is used to set the name of controller action that will be invoked (MVC).
*/
const ACTION = 'action';
/**
* An option which is used to set an array of allowed request methods.
*/
const REQUEST_METHODS = 'methods';
/**
* An option which is used to set the languages at which the route will be available at (used in building sitemap).
*/
const LANGS = 'languages';
/**
* An option which is used to set an array of allowed values to route parameters.
*/
const VALUES = 'vars-values';
/**
* An option which is used to set the middleware that will be applied to the route.
*/
const MIDDLEWARE = 'middleware';
/**
* An option which is used to set sub-routes.
*/
const SUB_ROUTES = 'routes';
}
2 changes: 1 addition & 1 deletion webfiori/framework/router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 93adfb2

Please sign in to comment.