1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhalconExt\Http; |
4
|
|
|
|
5
|
|
|
use Phalcon\Http\Request; |
|
|
|
|
6
|
|
|
use Phalcon\Http\Response; |
|
|
|
|
7
|
|
|
use Phalcon\Mvc\DispatcherInterface as Dispatcher; |
|
|
|
|
8
|
|
|
use Phalcon\Mvc\Micro as MicroApplication; |
|
|
|
|
9
|
|
|
use Phalcon\Mvc\Micro\MiddlewareInterface; |
|
|
|
|
10
|
|
|
use Phalcon\Mvc\View; |
|
|
|
|
11
|
|
|
use PhalconExt\Di\ProvidesDi; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* A handy base for middlewares. |
15
|
|
|
* |
16
|
|
|
* @author Jitendra Adhikari <[email protected]> |
17
|
|
|
* @license MIT |
18
|
|
|
* |
19
|
|
|
* @link https://github.com/adhocore/phalcon-ext |
20
|
|
|
*/ |
21
|
|
|
abstract class BaseMiddleware implements MiddlewareInterface |
22
|
|
|
{ |
23
|
|
|
use ProvidesDi; |
24
|
|
|
|
25
|
|
|
/** @var array */ |
26
|
|
|
protected $config = []; |
27
|
|
|
|
28
|
|
|
/** @var string */ |
29
|
|
|
protected $configKey; |
30
|
|
|
|
31
|
|
|
public function __construct() |
32
|
|
|
{ |
33
|
|
|
$this->config = $this->di('config')->toArray()[$this->configKey]; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Sets itself to be triggered on before &/or after route execution events. |
38
|
|
|
* |
39
|
|
|
* @return void |
40
|
|
|
*/ |
41
|
|
|
public function boot() |
42
|
|
|
{ |
43
|
|
|
if ($this->isMicro()) { |
44
|
|
|
$this->di('application')->before($this); |
45
|
|
|
$this->di('application')->after([$this, 'callAfter']); |
46
|
|
|
|
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$evm = $this->di('eventsManager'); |
51
|
|
|
|
52
|
|
|
$evm->attach('dispatch:beforeExecuteRoute', $this); |
53
|
|
|
$evm->attach('dispatch:afterExecuteRoute', $this); |
54
|
|
|
|
55
|
|
|
$this->di('dispatcher')->setEventsManager($evm); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Before route handler for both micro and mvc app. |
60
|
|
|
* |
61
|
|
|
* @param Request $request |
62
|
|
|
* @param Response $response |
63
|
|
|
* |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
|
|
abstract public function before(Request $request, Response $response): bool; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* After route handler for both micro and mvc app. |
70
|
|
|
* |
71
|
|
|
* @param Request $request |
72
|
|
|
* @param Response $response |
73
|
|
|
* |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
public function after(Request $request, Response $response): bool |
77
|
|
|
{ |
78
|
|
|
// Implementing class may extend this and do the needful. |
79
|
|
|
return true; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Before route executed in mvc. |
84
|
|
|
* |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
public function beforeExecuteRoute(): bool |
88
|
|
|
{ |
89
|
|
|
return $this->before($this->di('request'), $this->di('response')); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* After route executed in mvc. |
94
|
|
|
* |
95
|
|
|
*@return bool |
96
|
|
|
*/ |
97
|
|
|
public function afterExecuteRoute(): bool |
98
|
|
|
{ |
99
|
|
|
return $this->after($this->di('request'), $this->di('response')); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Before handler for micro app. |
104
|
|
|
* |
105
|
|
|
* @param MicroApplication $app |
106
|
|
|
* |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
|
|
public function call(MicroApplication $app): bool |
110
|
|
|
{ |
111
|
|
|
return $this->before($this->di('request'), $this->di('response')); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* After handler for micro app. |
116
|
|
|
* |
117
|
|
|
* @return bool |
118
|
|
|
*/ |
119
|
|
|
public function callAfter(): bool |
120
|
|
|
{ |
121
|
|
|
return $this->after($this->di('request'), $this->di('response')); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Disable view if possible. |
126
|
|
|
* |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
|
|
protected function disableView() |
130
|
|
|
{ |
131
|
|
|
if ($this->di('view') instanceof View) { |
132
|
|
|
$this->di('view')->disable(); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Abort with failure response. |
138
|
|
|
* |
139
|
|
|
* @param int $status |
140
|
|
|
* |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
|
|
protected function abort(int $status): bool |
144
|
|
|
{ |
145
|
|
|
$this->di('response')->setContent('')->setStatusCode($status)->send(); |
146
|
|
|
|
147
|
|
|
return false; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Checks if current app is micro. |
152
|
|
|
* |
153
|
|
|
* @return bool |
154
|
|
|
*/ |
155
|
|
|
protected function isMicro(): bool |
156
|
|
|
{ |
157
|
|
|
static $isMicro = null; |
158
|
|
|
|
159
|
|
|
if (null !== $isMicro) { |
160
|
|
|
return $isMicro; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if (!$this->di()->has('application')) { |
164
|
|
|
return $isMicro = false; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $isMicro = $this->di('application') instanceof MicroApplication; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get routeName and Url tuple. |
172
|
|
|
* |
173
|
|
|
* @return array [name, 'uri'] |
174
|
|
|
*/ |
175
|
|
|
protected function getRouteNameUri(): array |
176
|
|
|
{ |
177
|
|
|
$router = $this->di('router'); |
178
|
|
|
$route = $router->getMatchedRoute(); |
179
|
|
|
$name = $route ? $route->getName() : null; |
180
|
|
|
|
181
|
|
|
return [$name, $router->getRewriteUri()]; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths