1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Obsidian\Middleware; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Exception; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use Obsidian\Helpers\Mixed; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Allow objects to have middleware |
12
|
|
|
*/ |
13
|
|
|
trait HasMiddlewareTrait { |
14
|
|
|
/** |
15
|
|
|
* Array of all registered middleware |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $middleware = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Check if the passed entity is a valid middleware |
23
|
|
|
* |
24
|
|
|
* @param mixed $middleware |
25
|
|
|
* @return boolean |
26
|
|
|
*/ |
27
|
6 |
|
protected function isMiddleware( $middleware ) { |
28
|
6 |
|
if ( is_callable( $middleware ) ) { |
29
|
1 |
|
return true; |
30
|
|
|
} |
31
|
|
|
|
32
|
5 |
|
return is_a( $middleware, MiddlewareInterface::class, true ); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get registered middleware |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
5 |
|
public function getMiddleware() { |
41
|
5 |
|
return $this->middleware; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Add middleware |
46
|
|
|
* Accepts: a class name, an instance of a class, a callable, an array of any of the previous |
47
|
|
|
* |
48
|
|
|
* @param string|callable|\Obsidian\Middleware\MiddlewareInterface|array $middleware |
49
|
|
|
* @return object $this |
50
|
|
|
*/ |
51
|
6 |
|
public function addMiddleware( $middleware ) { |
52
|
6 |
|
$middleware = Mixed::toArray( $middleware, true ); |
53
|
|
|
|
54
|
6 |
|
foreach ( $middleware as $item ) { |
55
|
6 |
|
if ( ! $this->isMiddleware( $item ) ) { |
56
|
1 |
|
throw new Exception( 'Passed middleware must be a callable or the name or instance of a class which implements the ' . MiddlewareInterface::class . ' interface.' ); |
57
|
|
|
} |
58
|
5 |
|
} |
59
|
|
|
|
60
|
5 |
|
$this->middleware = array_merge( $this->getMiddleware(), $middleware ); |
61
|
5 |
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Alias for addMiddleware |
66
|
|
|
* |
67
|
|
|
* @codeCoverageIgnore |
68
|
|
|
* @param string|callable|\Obsidian\Middleware\middlewareInterface|array $middleware |
69
|
|
|
* @return object $this |
70
|
|
|
*/ |
71
|
|
|
public function add( $middleware ) { |
72
|
|
|
return call_user_func_array( [$this, 'addMiddleware'], func_get_args() ); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Execute an array of middleware recursively (last in, first out) |
77
|
|
|
* |
78
|
|
|
* @param array $middleware |
79
|
|
|
* @param \Obsidian\Request $request |
80
|
|
|
* @param Closure $next |
81
|
|
|
* @return ResponseInterface |
82
|
|
|
*/ |
83
|
5 |
|
public function executeMiddleware( $middleware, $request, Closure $next ) { |
84
|
5 |
|
$top_middleware = array_pop( $middleware ); |
85
|
|
|
|
86
|
5 |
|
if ( $top_middleware === null ) { |
87
|
5 |
|
return $next( $request ); |
88
|
|
|
} |
89
|
|
|
|
90
|
4 |
|
$top_middleware_next = function( $request ) use ( $middleware, $next ) { |
91
|
4 |
|
return $this->executeMiddleware( $middleware, $request, $next ); |
92
|
4 |
|
}; |
93
|
|
|
|
94
|
4 |
|
return Mixed::value( $top_middleware, [$request, $top_middleware_next], 'handle' ); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|