Skip to content

Commit 61b0f29

Browse files
committed
README.md has been updated, Dependency injection in init instance has been removed, Middleware function has been modified for handling indexes by selection.
1 parent 368ca22 commit 61b0f29

File tree

2 files changed

+77
-63
lines changed

2 files changed

+77
-63
lines changed

README.md

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# This library has a quick use of the router with regular expressions based on [mrjgreen's phroute](https://github.com/mrjgreen/phroute).
2+
[![Latest Stable Version](http://poser.pugx.org/lion-framework/lion-route/v)](https://packagist.org/packages/lion-framework/lion-route) [![Total Downloads](http://poser.pugx.org/lion-framework/lion-route/downloads)](https://packagist.org/packages/lion-framework/lion-route) [![Latest Unstable Version](http://poser.pugx.org/lion-framework/lion-route/v/unstable)](https://packagist.org/packages/lion-framework/lion-route) [![License](http://poser.pugx.org/lion-framework/lion-route/license)](https://packagist.org/packages/lion-framework/lion-route) [![PHP Version Require](http://poser.pugx.org/lion-framework/lion-route/require/php)](https://packagist.org/packages/lion-framework/lion-route)
23

34
## Install
45
```
@@ -11,12 +12,7 @@ require_once("vendor/autoload.php");
1112

1213
use LionRoute\Route;
1314

14-
Route::init([
15-
'class' => [
16-
'RouteCollector' => Phroute\Phroute\RouteCollector::class,
17-
'Dispatcher' => Phroute\Phroute\Dispatcher::class
18-
]
19-
]);
15+
Route::init();
2016

2117
Route::any('/', function() {
2218
return [
@@ -33,12 +29,7 @@ Route::processOutput(Route::dispatch(2));
3329
```php
3430
use LionRoute\Route;
3531

36-
Route::init([
37-
'class' => [
38-
'RouteCollector' => Phroute\Phroute\RouteCollector::class,
39-
'Dispatcher' => Phroute\Phroute\Dispatcher::class
40-
]
41-
]);
32+
Route::init();
4233

4334
Route::get($route, $handler);
4435
Route::post($route, $handler);
@@ -63,24 +54,35 @@ use in routes:
6354
```
6455

6556
### ~~Filters~~ Middleware:
66-
is identical to filters, we change the name of `filter` to `middleware`.
67-
`Route::newMiddleware('auth', Auth::class, 'auth')` is the basic syntax for adding a middleware to our RouteCollector object, The first parameter is the name of the middleware, The second parameter is the class to which that is referenced and the third parameter the name of the function to which it belongs.
57+
Is identical to filters, we change the name of `filter` to `middleware`.
58+
`Route::newMiddleware('auth', Auth::class, 'auth')` is the basic syntax for adding a middleware to our RouteCollector object. The first parameter is the name of the middleware. The second parameter is the class referenced and the third parameter the name of the function it belongs to. <br>
59+
60+
```php
61+
'middleware' => [
62+
Route::newMiddleware('auth', Auth::class, 'auth'),
63+
Route::newMiddleware('no-auth', Auth::class, 'auth')
64+
]
65+
```
66+
67+
When calling `Route::middleware()` keep in mind that the first parameter is an array loaded with data. <br>
68+
69+
The first index is the middleware at position `before`. <br>
70+
The second index is optional and points to `after`. <br>
71+
The third index is optional and indicates a `prefix` to work the middleware in a more dynamic way. <br>
72+
73+
Take into account that if more than 3 parameters are added, these are left over and do not generate internal errors in their operation.
6874
```php
6975
use LionRoute\Route;
7076
use Example\Auth;
7177

7278
Route::init([
73-
'class' => [
74-
'RouteCollector' => Phroute\Phroute\RouteCollector::class,
75-
'Dispatcher' => Phroute\Phroute\Dispatcher::class
76-
],
7779
'middleware' => [
7880
Route::newMiddleware('auth', Auth::class, 'auth'),
7981
Route::newMiddleware('no-auth', Auth::class, 'auth')
8082
]
8183
]);
8284

83-
Route::middleware(['before' => 'auth'], function() {
85+
Route::middleware(['no-auth'], function() {
8486
Route::post('login', function() {
8587
return [
8688
'status' => "success",
@@ -102,31 +104,19 @@ Route::prefix('authenticate', function() {
102104
});
103105
```
104106

107+
### Example methods:
108+
#### GET
105109
```php
106-
Route::middleware(['before' => 'no-auth'], function() {
107-
Route::prefix('authenticate', function() {
108-
Route::post('login', function() {
109-
return [
110-
'status' => "success",
111-
'message' => "Hello world."
112-
];
113-
});
114-
});
110+
Route::get('/example-url', function() {
111+
$get = new Example();
112+
$get->getMethod();
115113
});
116114

117115
// or
118116

119-
Route::middleware(['before' => 'no-auth', 'prefix' => 'authenticate'], function() {
120-
Route::post('login', function() {
121-
return [
122-
'status' => "success",
123-
'message' => "Hello world."
124-
];
125-
});
126-
});
117+
Route::get('/example-url', [Example::class, 'getMethod']);
127118
```
128119

129-
### Example methods:
130120
#### POST
131121
```php
132122
Route::post('/example-url', function() {
@@ -163,6 +153,18 @@ Route::delete('/example-url/{id}', function($id) {
163153
Route::delete('/example-url/{id}', [Example::class, 'deleteMethod']);
164154
```
165155

156+
#### ANY
157+
```php
158+
Route::any('/example-url', function($id) {
159+
$any = new Example();
160+
$any->anyMethod();
161+
});
162+
163+
// or
164+
165+
Route::any('/example-url', [Example::class, 'anyMethod']);
166+
```
167+
166168
## Credits
167169
[PHRoute](https://github.com/mrjgreen/phroute)
168170

src/LionRoute/Route.php

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,28 @@
22

33
namespace LionRoute;
44

5+
use Phroute\Phroute\{ RouteCollector, RouteParser, Dispatcher };
6+
use Phroute\Phroute\Exception\{ HttpRouteNotFoundException, HttpMethodNotAllowedException };
57
use LionRoute\Middleware;
68

79
class Route {
810

911
private static $router;
10-
private static array $class_map;
1112

1213
public function __construct() {
1314

1415
}
1516

16-
public static function init(array $filters): void {
17-
self::$class_map = $filters['class'];
18-
$parser = isset(self::$class_map['RouteParser']) ? new self::$class_map['RouteParser']() : null;
19-
self::$router = new self::$class_map['RouteCollector']($parser);
17+
public static function init(array $filters = []): void {
18+
self::$router = new RouteCollector(new RouteParser());
2019
self::createMiddleware(isset($filters['middleware']) ? $filters['middleware'] : []);
2120
}
2221

23-
public static function createMiddleware(array $filters): void {
22+
public static function newMiddleware(string $middlewareName, string $objectClass, string $methodClass): Middleware {
23+
return new Middleware($middlewareName, $objectClass, $methodClass);
24+
}
25+
26+
private static function createMiddleware(array $filters): void {
2427
if (count($filters) > 0) {
2528
foreach ($filters as $key => $obj) {
2629
self::$router->filter($obj->getMiddlewareName(), function() use ($obj) {
@@ -40,24 +43,27 @@ public static function prefix(string $prefix_name, \Closure $closure): void {
4043
}
4144

4245
public static function middleware(array $middleware, \Closure $closure): void {
43-
self::$router->group($middleware, function($router) use ($closure) {
46+
$count = count($middleware);
47+
$list_middleware = [];
48+
49+
if ($count === 1) {
50+
$list_middleware = ['before' => $middleware[0]];
51+
} elseif ($count === 2) {
52+
$list_middleware = ['before' => $middleware[0], 'after' => $middleware[1]];
53+
} elseif ($count >= 3) {
54+
$list_middleware = ['before' => $middleware[0], 'after' => $middleware[1], 'prefix' => $middleware[2]];
55+
}
56+
57+
self::$router->group($list_middleware, function($router) use ($closure) {
4458
$closure();
4559
});
4660
}
4761

48-
public static function any(string $url, \Closure|array $controller_function, array $filters = []): void {
49-
if (count($filters) > 0) {
50-
self::$router->any($url, $controller_function, $filters);
51-
} else {
52-
self::$router->any($url, $controller_function);
53-
}
54-
}
55-
56-
public static function delete(string $url, \Closure|array $controller_function, array $filters = []): void {
62+
public static function get(string $url, \Closure|array $controller_function, array $filters = []): void {
5763
if (count($filters) > 0) {
58-
self::$router->delete($url, $controller_function, $filters);
64+
self::$router->get($url, $controller_function, $filters);
5965
} else {
60-
self::$router->delete($url, $controller_function);
66+
self::$router->get($url, $controller_function);
6167
}
6268
}
6369

@@ -77,16 +83,20 @@ public static function put(string $url, \Closure|array $controller_function, arr
7783
}
7884
}
7985

80-
public static function get(string $url, \Closure|array $controller_function, array $filters = []): void {
86+
public static function delete(string $url, \Closure|array $controller_function, array $filters = []): void {
8187
if (count($filters) > 0) {
82-
self::$router->get($url, $controller_function, $filters);
88+
self::$router->delete($url, $controller_function, $filters);
8389
} else {
84-
self::$router->get($url, $controller_function);
90+
self::$router->delete($url, $controller_function);
8591
}
8692
}
8793

88-
public static function newMiddleware(string $middlewareName, string $objectClass, string $methodClass): Middleware {
89-
return new Middleware($middlewareName, $objectClass, $methodClass);
94+
public static function any(string $url, \Closure|array $controller_function, array $filters = []): void {
95+
if (count($filters) > 0) {
96+
self::$router->any($url, $controller_function, $filters);
97+
} else {
98+
self::$router->any($url, $controller_function);
99+
}
90100
}
91101

92102
private static function processInput($index): string {
@@ -99,12 +109,14 @@ public static function processOutput($response): void {
99109

100110
public static function dispatch($index) {
101111
try {
102-
return (new self::$class_map['Dispatcher'](self::$router->getData()))->dispatch(
112+
return (new Dispatcher(self::$router->getData()))->dispatch(
103113
$_SERVER['REQUEST_METHOD'],
104114
self::processInput($index)
105115
);
106-
} catch (\Exception $e) {
107-
return ['status' => "error", 'message' => $e->getMessage()];
116+
} catch (HttpRouteNotFoundException $e) {
117+
return ['status' => "error", 'message' => "Path not found: {$e->getMessage()}"];
118+
} catch (HttpMethodNotAllowedException $e) {
119+
return ['status' => "error", 'message' => "Method not allowed: {$e->getMessage()}"];
108120
}
109121
}
110122

0 commit comments

Comments
 (0)