Skip to content

Commit 739c86d

Browse files
committed
Add useMiddleware and hasMiddleware
1 parent 0890c2f commit 739c86d

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

src/App.php

Lines changed: 39 additions & 1 deletion
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();
@@ -124,6 +126,38 @@ public function setMiddleware($name, $callable)
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)