1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jasny; |
4
|
|
|
|
5
|
|
|
use Jasny\Router\Runner; |
6
|
|
|
use Jasny\Router\Routes\Glob; |
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Route pretty URLs to correct controller |
12
|
|
|
*/ |
13
|
|
|
class Router |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Specific routes |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $routes = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Middlewares actions |
23
|
|
|
* @var array |
24
|
|
|
**/ |
25
|
|
|
protected $middlewares = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class constructor |
29
|
|
|
* |
30
|
|
|
* @param array $routes |
31
|
|
|
*/ |
32
|
|
|
public function __construct(array $routes) |
33
|
|
|
{ |
34
|
|
|
$this->routes = $routes; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get a list of all routes |
39
|
|
|
* |
40
|
|
|
* @return object |
41
|
|
|
*/ |
42
|
|
|
public function getRoutes() |
43
|
|
|
{ |
44
|
|
|
return $this->routes; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get middlewares |
49
|
|
|
* |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
public function getMiddlewares() |
53
|
|
|
{ |
54
|
|
|
return $this->middlewares; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Add middleware call to router |
59
|
|
|
* |
60
|
|
|
* @param callback $middleware |
61
|
|
|
* @return Router $this |
62
|
|
|
*/ |
63
|
|
|
public function add($middleware) |
64
|
|
|
{ |
65
|
|
|
if (!is_callable($middleware)) { |
66
|
|
|
throw new \InvalidArgumentException("Middleware should be a callable"); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->middlewares[] = $middleware; |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Run the action for the request |
76
|
|
|
* |
77
|
|
|
* @param ServerRequestInterface $request |
78
|
|
|
* @param ResponseInterface $response |
79
|
|
|
* @return ResponseInterface |
80
|
|
|
*/ |
81
|
|
|
final public function run(ServerRequestInterface $request, ResponseInterface $response) |
82
|
|
|
{ |
83
|
|
|
return $this->__invoke($request, $response); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Run the action for the request (optionally as middleware), previously running middlewares, if any |
88
|
|
|
* |
89
|
|
|
* @param ServerRequestInterface $request |
90
|
|
|
* @param ResponseInterface $response |
91
|
|
|
* @param callback $next |
92
|
|
|
* @return ResponseInterface |
93
|
|
|
*/ |
94
|
|
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next = null) |
95
|
|
|
{ |
96
|
|
|
$handle = [$this, 'handle']; |
97
|
|
|
|
98
|
|
|
#Call to $this->handle will be executed last in the chain of middlewares |
|
|
|
|
99
|
|
|
$next = function(ServerRequestInterface $request, ResponseInterface $response) use ($next, $handle) { |
|
|
|
|
100
|
|
|
return call_user_func($handle, $request, $response, $next); |
101
|
|
|
}; |
102
|
|
|
|
103
|
|
|
#Build middlewares call chain, so that the last added was executed in first place |
|
|
|
|
104
|
|
|
foreach ($this->middlewares as $middleware) { |
105
|
|
|
$next = function(ServerRequestInterface $request, ResponseInterface $response) use ($next, $middleware) { |
|
|
|
|
106
|
|
|
return $middleware($request, $response, $next); |
107
|
|
|
}; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $next($request, $response); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Run the action |
115
|
|
|
* |
116
|
|
|
* @param ServerRequestInterface $request |
117
|
|
|
* @param ResponseInterface $response |
118
|
|
|
* @param callback $next |
119
|
|
|
* @return ResponseInterface |
120
|
|
|
*/ |
121
|
|
|
protected function handle(ServerRequestInterface $request, ResponseInterface $response, $next = null) |
122
|
|
|
{ |
123
|
|
|
$glob = new Glob($this->routes); |
124
|
|
|
$route = $glob->getRoute($request); |
125
|
|
|
|
126
|
|
|
if (!$route) return $this->notFound($response); |
|
|
|
|
127
|
|
|
|
128
|
|
|
$runner = Runner::create($route); |
|
|
|
|
129
|
|
|
|
130
|
|
|
return $runner($request, $response, $next); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Return 'Not Found' response |
135
|
|
|
* |
136
|
|
|
* @param ResponseInterface $response |
137
|
|
|
* @return ResponseInterface |
138
|
|
|
*/ |
139
|
|
|
protected function notFound(ResponseInterface $response) |
140
|
|
|
{ |
141
|
|
|
$message = 'Not Found'; |
142
|
|
|
|
143
|
|
|
$body = $response->getBody(); |
144
|
|
|
$body->rewind(); |
145
|
|
|
$body->write($message); |
146
|
|
|
|
147
|
|
|
return $response->withStatus(404, $message)->withBody($body); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
|