Skip to content

Commit 81fcb31

Browse files
authored
Merge pull request #2 from rakit/v0.4.0
v0.4.0 - Some changes in middleware
2 parents 0981d39 + 739c86d commit 81fcb31

File tree

2 files changed

+74
-14
lines changed

2 files changed

+74
-14
lines changed

src/App.php

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class App implements ArrayAccess {
3636

3737
protected $middlewares = array();
3838

39+
protected $global_middlewares = array();
40+
3941
protected $waiting_list_providers = array();
4042

4143
protected $providers = array();
@@ -113,17 +115,49 @@ public function once($event, Closure $callable)
113115
}
114116

115117
/**
116-
* Register a middleware
118+
* Set middleware
117119
*
118120
* @param string $name
119121
* @param mixed $callable
120122
* @return void
121123
*/
122-
public function middleware($name, $callable)
124+
public function setMiddleware($name, $callable)
123125
{
124126
$this->middlewares[$name] = $callable;
125127
}
126128

129+
/**
130+
* Check middleware is registered or not
131+
*
132+
* @param string $name
133+
* @return void
134+
*/
135+
public function hasMiddleware($name)
136+
{
137+
return isset($this->middlewares[$name]);
138+
}
139+
140+
/**
141+
* Add global middleware
142+
*
143+
* @param string|callable $name_or_callable
144+
* @return void
145+
*/
146+
public function useMiddleware($name_or_callable)
147+
{
148+
if (!is_callable($name_or_callable)) {
149+
if (is_string($name_or_callable) AND $this->hasMiddleware($name_or_callable)) {
150+
$callable = $this->middlewares[$name_or_callable];
151+
} else {
152+
throw new InvalidArgumentException("Cannot use global middleware. Middleware must be callable or registered middleware", 1);
153+
}
154+
} else {
155+
$callable = $name_or_callable;
156+
}
157+
158+
$this->global_middlewares[] = $callable;
159+
}
160+
127161
/**
128162
* Register GET route
129163
*
@@ -205,7 +239,11 @@ public function group($prefix, Closure $grouper)
205239
*/
206240
public function route($methods, $path, $action)
207241
{
208-
return $this->router->add($methods, $path, $action);
242+
$route = $this->router->add($methods, $path, $action);
243+
if (!empty($this->global_middlewares)) {
244+
$route->middleware($this->global_middlewares);
245+
}
246+
return $route;
209247
}
210248

211249
/**

tests/RunAppTests.php

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public function testRouteParamCondition()
179179
*/
180180
public function testMiddlewareBefore()
181181
{
182-
$this->app->middleware('foobar', function($req, $res, $next) {
182+
$this->app->setMiddleware('foobar', function($req, $res, $next) {
183183
$req->foobar = "foobar";
184184
return $next();
185185
});
@@ -197,7 +197,7 @@ public function testMiddlewareBefore()
197197
*/
198198
public function testMiddlewareAfter()
199199
{
200-
$this->app->middleware('uppercase', function($req, $res, $next) {
200+
$this->app->setMiddleware('uppercase', function($req, $res, $next) {
201201
$next();
202202
return strtoupper($res->body);
203203
});
@@ -215,7 +215,7 @@ public function testMiddlewareAfter()
215215
*/
216216
public function testMiddlewareBeforeAndAfter()
217217
{
218-
$this->app->middleware('uppercase', function($req, $res, $next) {
218+
$this->app->setMiddleware('uppercase', function($req, $res, $next) {
219219
$req->foobar = "foobar";
220220

221221
$next();
@@ -236,7 +236,7 @@ public function testMiddlewareBeforeAndAfter()
236236
*/
237237
public function testMiddlewareParam()
238238
{
239-
$this->app->middleware('setStr', function($req, $res, $next, $str) {
239+
$this->app->setMiddleware('setStr', function($req, $res, $next, $str) {
240240
$req->str = $str;
241241
return $next();
242242
});
@@ -254,17 +254,17 @@ public function testMiddlewareParam()
254254
*/
255255
public function testMultipleMiddleware()
256256
{
257-
$this->app->middleware('setStr', function($req, $res, $next, $str) {
257+
$this->app->setMiddleware('setStr', function($req, $res, $next, $str) {
258258
$req->str = $str;
259259
return $next();
260260
});
261261

262-
$this->app->middleware('uppercase', function($req, $res, $next) {
262+
$this->app->setMiddleware('uppercase', function($req, $res, $next) {
263263
$next();
264264
return strtoupper($res->body);
265265
});
266266

267-
$this->app->middleware('jsonify', function($req, $res, $next) {
267+
$this->app->setMiddleware('jsonify', function($req, $res, $next) {
268268
$next();
269269
return $res->json(['body' => $res->body]);
270270
});
@@ -282,7 +282,7 @@ public function testMultipleMiddleware()
282282
*/
283283
public function testIgnoringController()
284284
{
285-
$this->app->middleware('no-controller', function($req, $res, $next) {
285+
$this->app->setMiddleware('no-controller', function($req, $res, $next) {
286286
return "controller ignored";
287287
});
288288

@@ -331,7 +331,7 @@ public function testResponseJson()
331331
*/
332332
public function testMiddlewareKeepResponseToJson()
333333
{
334-
$this->app->middleware('uppercase', function($req, $res, $next) {
334+
$this->app->setMiddleware('uppercase', function($req, $res, $next) {
335335
$next();
336336
return strtoupper($res->body);
337337
});
@@ -345,6 +345,28 @@ public function testMiddlewareKeepResponseToJson()
345345
$this->assertResponse("GET", "/anything.json", '{"MESSAGE":"HELLO"}', 200, 'application/json');
346346
}
347347

348+
/**
349+
* @runInSeparateProcess
350+
* @preserveGlobalState enabled
351+
*/
352+
public function testGlobalMiddleware()
353+
{
354+
$this->app->setMiddleware('uppercase', function($req, $res, $next) {
355+
$next();
356+
return strtoupper($res->body);
357+
});
358+
359+
$this->app->useMiddleware('uppercase');
360+
361+
$this->app->get('/anything.json', function() {
362+
return [
363+
'message' => 'hello'
364+
];
365+
});
366+
367+
$this->assertResponse("GET", "/anything.json", '{"MESSAGE":"HELLO"}', 200, 'application/json');
368+
}
369+
348370
/**
349371
* @runInSeparateProcess
350372
* @preserveGlobalState enabled
@@ -369,7 +391,7 @@ public function testRouteGroupParamCondition()
369391
*/
370392
public function testRouteGroupMiddleware()
371393
{
372-
$this->app->middleware('setStr', function($req, $res, $next, $str) {
394+
$this->app->setMiddleware('setStr', function($req, $res, $next, $str) {
373395
$req->str = $str;
374396
return $next();
375397
});
@@ -425,7 +447,7 @@ public function testAppBindedToClosure()
425447
{
426448
$this->app->something = "foo";
427449
$this->app->foo = "bar";
428-
$this->app->middleware('test', function($req, $res, $next) {
450+
$this->app->setMiddleware('test', function($req, $res, $next) {
429451
$next();
430452
return strtoupper($this->something.$res->body);
431453
});

0 commit comments

Comments
 (0)