1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Borobudur-Kernel package. |
4
|
|
|
* |
5
|
|
|
* (c) Hexacodelabs <http://hexacodelabs.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Borobudur\Kernel\Middleware; |
12
|
|
|
|
13
|
|
|
use Borobudur\Http\Request; |
14
|
|
|
use Borobudur\Http\Response; |
15
|
|
|
use Borobudur\Routing\RequestContext; |
16
|
|
|
use Closure; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Iqbal Maulana <[email protected]> |
20
|
|
|
* @created 8/15/15 |
21
|
|
|
*/ |
22
|
|
|
class MiddlewareInvoker |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private $excepts = array(); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $only = array(); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var MiddlewareInterface |
36
|
|
|
*/ |
37
|
|
|
private $middleware; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Constructor. |
41
|
|
|
* |
42
|
|
|
* @param MiddlewareInterface $middleware |
43
|
|
|
*/ |
44
|
|
|
public function __construct(MiddlewareInterface $middleware) |
45
|
|
|
{ |
46
|
|
|
$this->middleware = $middleware; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Do not invoke middleware for specified actions. |
51
|
|
|
* |
52
|
|
|
* @param array $actions |
53
|
|
|
* |
54
|
|
|
* @return $this |
55
|
|
|
*/ |
56
|
|
|
public function except(array $actions) |
57
|
|
|
{ |
58
|
|
|
if (!empty($actions)) { |
59
|
|
|
$this->excepts = $actions; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Invoke middleware only for specified actions. |
67
|
|
|
* |
68
|
|
|
* @param array $actions |
69
|
|
|
* |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
|
|
public function only(array $actions) |
73
|
|
|
{ |
74
|
|
|
if (!empty($actions)) { |
75
|
|
|
$this->only = $actions; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Invoke middleware. |
83
|
|
|
* |
84
|
|
|
* @param Request $request |
85
|
|
|
* @param RequestContext $context |
86
|
|
|
* @param Closure $next |
87
|
|
|
* @param string|null $action |
88
|
|
|
* |
89
|
|
|
* @return Response|null |
90
|
|
|
*/ |
91
|
|
|
public function __invoke(Request $request, RequestContext $context, Closure $next, $action = null) |
92
|
|
|
{ |
93
|
|
|
if (null !== $action) { |
94
|
|
|
if ($this->isExcept($action)) { |
95
|
|
|
return $next(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if ($this->isNotOnly($action)) { |
99
|
|
|
return $next(); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $this->middleware->handle($request, $context, $next); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Check if action in exception. |
108
|
|
|
* |
109
|
|
|
* @param string|null $action |
110
|
|
|
* |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
private function isExcept($action) |
114
|
|
|
{ |
115
|
|
|
return !empty($this->excepts) && in_array($action, $this->excepts); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Check if action in not only execute. |
120
|
|
|
* |
121
|
|
|
* @param string $action |
122
|
|
|
* |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
|
private function isNotOnly($action) |
126
|
|
|
{ |
127
|
|
|
return !empty($this->only) && !in_array($action, $this->only); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|