From 13c68b991f9c422aed7a09720ca72efb95fb6d44 Mon Sep 17 00:00:00 2001 From: Nicollas Date: Mon, 12 Jul 2021 22:31:25 -0300 Subject: [PATCH] Route helper & docs improvements --- README.md | 16 ++++++ src/Helpers/Functions.php | 29 ++++++++++ src/Helpers/helpers.php | 14 +++++ src/Router/RouteManager.php | 10 ++++ tests/RouteHelperTest.php | 99 +++++++++++++++++++++++++++++++++++ tests/RouteMiddlewareTest.php | 2 +- 6 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 tests/RouteHelperTest.php diff --git a/README.md b/README.md index 8b05e7e..b78221e 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,8 @@ composer require nicollassilva/minasrouter # Documentation ### 1. Configuration +- [Redirect to Public Folder](https://github.com/nicollassilva/minasrouter#redirect-to-public-folder) +* **Public Folder Configuration** - [Apache](https://github.com/nicollassilva/minasrouter#apache) - [Nginx](https://github.com/nicollassilva/minasrouter#apache) ### 2. Routes @@ -96,6 +98,20 @@ composer require nicollassilva/minasrouter To start using MinasRouter, all navigation management must be redirected to your system's default route file, which will do the entire route handling process and return what was configured by default. Configure according to the examples below and according to your server. +## Redirect to Public Folder + +```apacheconf +RewriteEngine on +Options All -Indexes + +RewriteCond %{HTTPS} on +RewriteCond %{HTTP_HOST} (www\.)?localhost +RewriteRule (.*) https://%{HTTP_HOST} [L,R=301] + +RewriteCond %{REQUEST_URI} !public/ +RewriteRule (.*) /public/$1 [L] +``` + ### apache ```apacheconf diff --git a/src/Helpers/Functions.php b/src/Helpers/Functions.php index 33bd7c1..e41effa 100644 --- a/src/Helpers/Functions.php +++ b/src/Helpers/Functions.php @@ -29,6 +29,35 @@ public function get(String $routerName) return $this->collection()->getByName($routerName); } + public function getStructuredRoute(String $routerName, $params) + { + $params = (array) func_get_arg(1); + + if(!$router = $this->get($routerName)) { + return null; + } + + $originalRoute = $router->getOriginalRoute(); + + preg_match_all("/{\w+\??}/", $originalRoute, $matches); + + if(empty($matches[0])) { + return $originalRoute; + } + + if(count($matches[0]) != count($params)) { + return null; + } + + foreach($matches[0] as $index => $match) { + $paramForReplace = isset($params[$index]) ? $params[$index] : 'undefined'; + + $originalRoute = str_replace($match, $paramForReplace, $originalRoute); + } + + return $originalRoute; + } + /** * Method responsible for returning the * current route. diff --git a/src/Helpers/helpers.php b/src/Helpers/helpers.php index 6d8dc60..86bcae3 100644 --- a/src/Helpers/helpers.php +++ b/src/Helpers/helpers.php @@ -12,4 +12,18 @@ function router(): Functions { return new Functions; } + + /** + * Method responsible for returning a + * string route with replaced parameters. + * + * @param string $routeName + * @param string|array|null $params + * + * @return null|string + */ + function route(String $routeName, $params = null) + { + return router()->getStructuredRoute($routeName, $params); + } } \ No newline at end of file diff --git a/src/Router/RouteManager.php b/src/Router/RouteManager.php index f60eaa6..556e29f 100644 --- a/src/Router/RouteManager.php +++ b/src/Router/RouteManager.php @@ -357,6 +357,16 @@ public function getMiddleware() return $this->middleware; } + /** + * Get the original uri route. + * + * @return string + */ + public function getOriginalRoute(): String + { + return $this->originalRoute; + } + /** * Method responsible for defining the middlewares of the route. * diff --git a/tests/RouteHelperTest.php b/tests/RouteHelperTest.php new file mode 100644 index 0000000..6e6b4cd --- /dev/null +++ b/tests/RouteHelperTest.php @@ -0,0 +1,99 @@ +name('topic.comments'); + Route::get('/topic/{id}/comment/{idComment}/show', 'TopicCommentController@show')->name('topic.comment.show'); + Route::get('/topic/{id}/comment/{idComment}/edit', 'TopicCommentController@edit')->name('topic.comment.edit'); + Route::put('/topic/comment/update', 'TopicCommentUpdate@index')->name('topic.comment.update'); + } + /** + * @test + */ + public function check_if_router_helper_function_returns_a_collection() + { + $helperRouter = router(); + + $this->assertInstanceOf(Functions::class, $helperRouter); + } + + /** + * @test + */ + public function check_if_get_function_helper_returns_a_determinate_route() + { + $topicCommentsRoute = router()->get('topic.comments'); + + $this->assertInstanceOf(RouteManager::class, $topicCommentsRoute); + + $this->assertSame('topic.comments', $topicCommentsRoute->getName()); + + $this->assertSame('/topic/{id}/comments', $topicCommentsRoute->getOriginalRoute()); + + $topicCommentShowRoute = router()->get('topic.comment.show'); + + $this->assertInstanceOf(RouteManager::class, $topicCommentShowRoute); + + $this->assertSame('topic.comment.show', $topicCommentShowRoute->getName()); + + $this->assertSame('/topic/{id}/comment/{idComment}/show', $topicCommentShowRoute->getOriginalRoute()); + } + + /** + * @test + */ + public function check_if_route_helper_function_replace_the_regex_with_parameters() + { + $scenaryOne = [ + route('topic.comment.edit'), + route('topic.comment.edit', [12]), + route('topic.comment.edit', [12, 534]), + route('topic.comment.edit', ['topic', 534]), + route('topic.comment.edit', [12, 'topic']), + ]; + + $scenaryOneExpected = [ + null, null, '/topic/12/comment/534/edit', '/topic/topic/comment/534/edit', '/topic/12/comment/topic/edit' + ]; + + foreach($scenaryOne as $key => $result) { + $this->assertSame($scenaryOneExpected[$key], $result); + } + + $scenaryTwo = [ + route('topic.comments'), + route('topic.comments', 12), + route('topic.comments', 'neymar') + ]; + + $scenaryTwoExpected = [ + null, '/topic/12/comments', '/topic/neymar/comments' + ]; + + foreach($scenaryTwo as $key => $result) { + $this->assertSame($scenaryTwoExpected[$key], $result); + } + + $scenaryThree = [ + route('topic.comment.update'), + route('topic.comment.update', 12), + route('topic.comment.update', [12]), + route('topic.comment.update', ['oi', 12]) + ]; + + $scenaryThreeExpected = '/topic/comment/update'; + + foreach($scenaryThree as $result) { + $this->assertSame($scenaryThreeExpected, $result); + } + } +} diff --git a/tests/RouteMiddlewareTest.php b/tests/RouteMiddlewareTest.php index 6865506..ebb51bf 100644 --- a/tests/RouteMiddlewareTest.php +++ b/tests/RouteMiddlewareTest.php @@ -98,7 +98,7 @@ public function check_how_middlewares_behave_in_a_real_scenario() Route::get("/forgout-password", "DashboardController@forgoutPassword")->name("forgout.password"), Route::get("/app", "AppController@index")->name("app.index")->middleware("isLogged"), Route::get("/admins", "AppController@admins")->name("app.admins")->middleware("isAdmin, isLogged"), - Route::get("/forgout-email", "DashboardController@forgoutEmail")->name("forgout.email"), + Route::get("/forgot-email", "DashboardController@forgoutEmail")->name("forgot.email"), Route::get("/server-status", "DashboardController@serverStatus")->name("server.status"), Route::get("/posts", "AppController@posts")->name("app.posts")->middleware(["isLogged", "isModerator"]), Route::get("/admin-ranking", "DashboardController@adminRanking")->name("admin.ranking")