Skip to content

Commit

Permalink
Route helper & docs improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
nicollassilva committed Jul 13, 2021
1 parent 6cad0a7 commit 13c68b9
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 1 deletion.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions src/Helpers/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 14 additions & 0 deletions src/Helpers/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
10 changes: 10 additions & 0 deletions src/Router/RouteManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
99 changes: 99 additions & 0 deletions tests/RouteHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

use MinasRouter\Router\Route;
use PHPUnit\Framework\TestCase;
use MinasRouter\Helpers\Functions;
use MinasRouter\Router\RouteManager;

final class RouteHelperTest extends TestCase
{
protected function setUp(): void
{
Route::start('http://localhost/');

Route::get('/topic/{id}/comments', 'TopicController@comments')->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);
}
}
}
2 changes: 1 addition & 1 deletion tests/RouteMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 13c68b9

Please sign in to comment.