/
DispatchListener.php
260 lines (236 loc) · 8.34 KB
/
DispatchListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
/**
* @see https://github.com/laminas/laminas-mvc for the canonical source repository
* @copyright https://github.com/laminas/laminas-mvc/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-mvc/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\Mvc;
use ArrayObject;
use Laminas\EventManager\AbstractListenerAggregate;
use Laminas\EventManager\EventManagerInterface;
use Laminas\Router\RouteMatch;
use Laminas\ServiceManager\Exception\InvalidServiceException;
use Laminas\Stdlib\ArrayUtils;
/**
* Default dispatch listener
*
* Pulls controllers from the service manager's "ControllerManager" service.
*
* If the controller cannot be found a "404" result is set up. Otherwise it
* will continue to try to load the controller.
*
* If the controller is not dispatchable it sets up a "404" result. In case
* of any other exceptions it trigger the "dispatch.error" event in an attempt
* to return a 500 status.
*
* If the controller subscribes to InjectApplicationEventInterface, it injects
* the current MvcEvent into the controller.
*
* It then calls the controller's "dispatch" method, passing it the request and
* response. If an exception occurs, it triggers the "dispatch.error" event,
* in an attempt to return a 500 status.
*
* The return value of dispatching the controller is placed into the result
* property of the MvcEvent, and returned.
*/
class DispatchListener extends AbstractListenerAggregate
{
/**
* @var Controller\ControllerManager
*/
private $controllerManager;
/**
* @param Controller\ControllerManager $controllerManager
*/
public function __construct(Controller\ControllerManager $controllerManager)
{
$this->controllerManager = $controllerManager;
}
/**
* Attach listeners to an event manager
*
* @param EventManagerInterface $events
* @param int $priority
* @return void
*/
public function attach(EventManagerInterface $events, $priority = 1)
{
$this->listeners[] = $events->attach(MvcEvent::EVENT_DISPATCH, [$this, 'onDispatch']);
if (function_exists('zend_monitor_custom_event_ex')) {
$this->listeners[] = $events->attach(MvcEvent::EVENT_DISPATCH_ERROR, [$this, 'reportMonitorEvent']);
}
}
/**
* Listen to the "dispatch" event
*
* @param MvcEvent $e
* @return mixed
*/
public function onDispatch(MvcEvent $e)
{
if (null !== $e->getResult()) {
return;
}
$routeMatch = $e->getRouteMatch();
$controllerName = $routeMatch instanceof RouteMatch
? $routeMatch->getParam('controller', 'not-found')
: 'not-found';
$application = $e->getApplication();
$controllerManager = $this->controllerManager;
// Query abstract controllers, too!
if (! $controllerManager->has($controllerName)) {
$return = $this->marshalControllerNotFoundEvent(
$application::ERROR_CONTROLLER_NOT_FOUND,
$controllerName,
$e,
$application
);
return $this->complete($return, $e);
}
try {
$controller = $controllerManager->get($controllerName);
} catch (Exception\InvalidControllerException $exception) {
$return = $this->marshalControllerNotFoundEvent(
$application::ERROR_CONTROLLER_INVALID,
$controllerName,
$e,
$application,
$exception
);
return $this->complete($return, $e);
} catch (InvalidServiceException $exception) {
$return = $this->marshalControllerNotFoundEvent(
$application::ERROR_CONTROLLER_INVALID,
$controllerName,
$e,
$application,
$exception
);
return $this->complete($return, $e);
} catch (\Throwable $exception) {
$return = $this->marshalBadControllerEvent($controllerName, $e, $application, $exception);
return $this->complete($return, $e);
} catch (\Exception $exception) { // @TODO clean up once PHP 7 requirement is enforced
$return = $this->marshalBadControllerEvent($controllerName, $e, $application, $exception);
return $this->complete($return, $e);
}
if ($controller instanceof InjectApplicationEventInterface) {
$controller->setEvent($e);
}
$request = $e->getRequest();
$response = $application->getResponse();
$caughtException = null;
try {
$return = $controller->dispatch($request, $response);
} catch (\Throwable $ex) {
$caughtException = $ex;
} catch (\Exception $ex) { // @TODO clean up once PHP 7 requirement is enforced
$caughtException = $ex;
}
if ($caughtException !== null) {
$e->setName(MvcEvent::EVENT_DISPATCH_ERROR);
$e->setError($application::ERROR_EXCEPTION);
$e->setController($controllerName);
$e->setControllerClass(get_class($controller));
$e->setParam('exception', $caughtException);
$return = $application->getEventManager()->triggerEvent($e)->last();
if (! $return) {
$return = $e->getResult();
}
}
return $this->complete($return, $e);
}
/**
* @param MvcEvent $e
*/
public function reportMonitorEvent(MvcEvent $e)
{
$error = $e->getError();
$exception = $e->getParam('exception');
// @TODO clean up once PHP 7 requirement is enforced
if ($exception instanceof \Exception || $exception instanceof \Throwable) {
zend_monitor_custom_event_ex(
$error,
$exception->getMessage(),
'Laminas Exception',
['code' => $exception->getCode(), 'trace' => $exception->getTraceAsString()]
);
}
}
/**
* Complete the dispatch
*
* @param mixed $return
* @param MvcEvent $event
* @return mixed
*/
protected function complete($return, MvcEvent $event)
{
if (! is_object($return)) {
if (ArrayUtils::hasStringKeys($return)) {
$return = new ArrayObject($return, ArrayObject::ARRAY_AS_PROPS);
}
}
$event->setResult($return);
return $return;
}
/**
* Marshal a controller not found exception event
*
* @param string $type
* @param string $controllerName
* @param MvcEvent $event
* @param Application $application
* @param \Throwable|\Exception $exception
* @return mixed
*/
protected function marshalControllerNotFoundEvent(
$type,
$controllerName,
MvcEvent $event,
Application $application,
$exception = null
) {
$event->setName(MvcEvent::EVENT_DISPATCH_ERROR);
$event->setError($type);
$event->setController($controllerName);
$event->setControllerClass('invalid controller class or alias: ' . $controllerName);
if ($exception !== null) {
$event->setParam('exception', $exception);
}
$events = $application->getEventManager();
$results = $events->triggerEvent($event);
$return = $results->last();
if (! $return) {
$return = $event->getResult();
}
return $return;
}
/**
* Marshal a bad controller exception event
*
* @param string $controllerName
* @param MvcEvent $event
* @param Application $application
* @param \Throwable|\Exception $exception
* @return mixed
*/
protected function marshalBadControllerEvent(
$controllerName,
MvcEvent $event,
Application $application,
$exception
) {
$event->setName(MvcEvent::EVENT_DISPATCH_ERROR);
$event->setError($application::ERROR_EXCEPTION);
$event->setController($controllerName);
$event->setParam('exception', $exception);
$events = $application->getEventManager();
$results = $events->triggerEvent($event);
$return = $results->last();
if (! $return) {
return $event->getResult();
}
return $return;
}
}