1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhalconExt\Http; |
4
|
|
|
|
5
|
|
|
use Phalcon\Di\Injectable; |
|
|
|
|
6
|
|
|
use Phalcon\Http\Request; |
7
|
|
|
use Phalcon\Http\Response; |
8
|
|
|
use Phalcon\Mvc\Application; |
|
|
|
|
9
|
|
|
use Phalcon\Mvc\DispatcherInterface as Dispatcher; |
|
|
|
|
10
|
|
|
use Phalcon\Mvc\Micro as MicroApplication; |
|
|
|
|
11
|
|
|
use PhalconExt\Di\ProvidesDi; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* A manager for middlewares. |
15
|
|
|
* |
16
|
|
|
* @author Jitendra Adhikari <[email protected]> |
17
|
|
|
* @license MIT |
18
|
|
|
* |
19
|
|
|
* @link https://github.com/adhocore/phalcon-ext |
20
|
|
|
*/ |
21
|
|
|
class Middlewares |
22
|
|
|
{ |
23
|
|
|
use ProvidesDi; |
24
|
|
|
|
25
|
|
|
protected $middlewares = []; |
26
|
|
|
|
27
|
|
|
public function __construct(array $middlewares) |
28
|
|
|
{ |
29
|
|
|
$this->middlewares = $middlewares; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Wraps app with middlewares and runs the app. |
34
|
|
|
* |
35
|
|
|
* @param Injectable $app The app instance: micro or mvc |
36
|
|
|
*/ |
37
|
|
|
public function wrap(Injectable $app) |
38
|
|
|
{ |
39
|
|
|
if ($app instanceof MicroApplication) { |
40
|
|
|
return $this->handleMicro($app); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (!$app instanceof Application) { |
44
|
|
|
throw new \InvalidArgumentException('The app instance is not one of micro or mvc'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$this->di()->setShared('application', $app); |
48
|
|
|
|
49
|
|
|
$this->handleMvc($app); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Setup before/after handers for micro app and run the app. |
54
|
|
|
* |
55
|
|
|
* @param Injectable $app |
56
|
|
|
* |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
protected function handleMicro(Injectable $app) |
60
|
|
|
{ |
61
|
|
|
$app |
62
|
|
|
->before([$this, 'beforeHandleRequest']) |
63
|
|
|
->after([$this, 'beforeSendResponse']); |
64
|
|
|
|
65
|
|
|
$this->handleApp($app); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Setup event handlers for mvc app and run the app. |
70
|
|
|
* |
71
|
|
|
* @param Injectable $app |
72
|
|
|
* |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
protected function handleMvc(Injectable $app) |
76
|
|
|
{ |
77
|
|
|
$evm = $this->di('eventsManager'); |
78
|
|
|
|
79
|
|
|
$evm->attach('application', $this); |
80
|
|
|
$evm->attach('dispatch', $this); |
81
|
|
|
$app->setEventsManager($evm); |
82
|
|
|
|
83
|
|
|
$this->di('dispatcher')->setEventsManager($evm); |
84
|
|
|
|
85
|
|
|
$this->handleApp($app); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Handles http requests to the app. |
90
|
|
|
* |
91
|
|
|
* @param Injectable $app |
92
|
|
|
* |
93
|
|
|
* @return mixed Whatever is given by $app->handle(). |
94
|
|
|
*/ |
95
|
|
|
protected function handleApp(Injectable $app) |
96
|
|
|
{ |
97
|
|
|
$return = $app->handle(); |
98
|
|
|
|
99
|
|
|
$response = $this->di('response'); |
100
|
|
|
|
101
|
|
|
if (!$response->isSent()) { |
102
|
|
|
$response->send(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $return; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function beforeHandleRequest(): bool |
109
|
|
|
{ |
110
|
|
|
return $this->relay('before'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function beforeSendResponse(): bool |
114
|
|
|
{ |
115
|
|
|
return $this->relay('after'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Sends events to all the middlewares. Aborts if one fails with falsy return value. |
120
|
|
|
* |
121
|
|
|
* @param string $event |
122
|
|
|
* |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
|
protected function relay(string $event): bool |
126
|
|
|
{ |
127
|
|
|
foreach ($this->middlewares as $middleware) { |
128
|
|
|
if (!$this->call($event, $middleware)) { |
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return true; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Call event method on middleware. |
138
|
|
|
* |
139
|
|
|
* @param string $event |
140
|
|
|
* @param mixed $middleware |
141
|
|
|
* |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
|
|
protected function call(string $event, $middleware): bool |
145
|
|
|
{ |
146
|
|
|
if (\is_string($middleware)) { |
147
|
|
|
$middleware = $this->di($middleware); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
if (!\method_exists($middleware, $event)) { |
151
|
|
|
return true; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $middleware->$event($this->di('request'), $this->di('response')); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
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