This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
/
DispatchListener.php
221 lines (201 loc) · 7.11 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
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Mvc;
use ArrayObject;
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\ListenerAggregateInterface;
use Zend\Mvc\Exception\InvalidControllerException;
use Zend\Stdlib\ArrayUtils;
/**
* Default dispatch listener
*
* Pulls controllers from the service manager's "ControllerLoader" 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 implements ListenerAggregateInterface
{
/**
* @var \Zend\Stdlib\CallbackHandler[]
*/
protected $listeners = array();
/**
* Attach listeners to an event manager
*
* @param EventManagerInterface $events
* @return void
*/
public function attach(EventManagerInterface $events)
{
$this->listeners[] = $events->attach(MvcEvent::EVENT_DISPATCH, array($this, 'onDispatch'));
if (function_exists('zend_monitor_custom_event_ex')) {
$this->listeners[] = $events->attach(MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'reportMonitorEvent'));
}
}
/**
* Detach listeners from an event manager
*
* @param EventManagerInterface $events
* @return void
*/
public function detach(EventManagerInterface $events)
{
foreach ($this->listeners as $index => $listener) {
if ($events->detach($listener)) {
unset($this->listeners[$index]);
}
}
}
/**
* Listen to the "dispatch" event
*
* @param MvcEvent $e
* @return mixed
*/
public function onDispatch(MvcEvent $e)
{
$routeMatch = $e->getRouteMatch();
$controllerName = $routeMatch->getParam('controller', 'not-found');
$application = $e->getApplication();
$events = $application->getEventManager();
$controllerLoader = $application->getServiceManager()->get('ControllerLoader');
if (!$controllerLoader->has($controllerName)) {
$return = $this->marshalControllerNotFoundEvent($application::ERROR_CONTROLLER_NOT_FOUND, $controllerName, $e, $application);
return $this->complete($return, $e);
}
try {
$controller = $controllerLoader->get($controllerName);
} catch (InvalidControllerException $exception) {
$return = $this->marshalControllerNotFoundEvent($application::ERROR_CONTROLLER_INVALID, $controllerName, $e, $application, $exception);
return $this->complete($return, $e);
} catch (\Exception $exception) {
$return = $this->marshalBadControllerEvent($controllerName, $e, $application, $exception);
return $this->complete($return, $e);
}
$request = $e->getRequest();
$response = $application->getResponse();
if ($controller instanceof InjectApplicationEventInterface) {
$controller->setEvent($e);
}
try {
$return = $controller->dispatch($request, $response);
} catch (\Exception $ex) {
$e->setError($application::ERROR_EXCEPTION)
->setController($controllerName)
->setControllerClass(get_class($controller))
->setParam('exception', $ex);
$results = $events->trigger(MvcEvent::EVENT_DISPATCH_ERROR, $e);
$return = $results->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');
if ($exception instanceof \Exception) {
zend_monitor_custom_event_ex($error, $exception->getMessage(), 'Zend Framework Exception', array('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 \Exception $exception
* @return mixed
*/
protected function marshalControllerNotFoundEvent(
$type,
$controllerName,
MvcEvent $event,
Application $application,
\Exception $exception = null
) {
$event->setError($type)
->setController($controllerName)
->setControllerClass('invalid controller class or alias: ' . $controllerName);
if ($exception !== null) {
$event->setParam('exception', $exception);
}
$events = $application->getEventManager();
$results = $events->trigger(MvcEvent::EVENT_DISPATCH_ERROR, $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 \Exception $exception
* @return mixed
*/
protected function marshalBadControllerEvent(
$controllerName,
MvcEvent $event,
Application $application,
\Exception $exception
) {
$event->setError($application::ERROR_EXCEPTION)
->setController($controllerName)
->setParam('exception', $exception);
$events = $application->getEventManager();
$results = $events->trigger(MvcEvent::EVENT_DISPATCH_ERROR, $event);
$return = $results->last();
if (! $return) {
$return = $event->getResult();
}
return $return;
}
}