-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathModule.php
140 lines (116 loc) · 3.55 KB
/
Module.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
<?php
namespace Main;
use Zend\Mvc\MvcEvent;
/**
*
*/
class Module
{
/**
* @param MvcEvent $e
*/
public function onBootstrap($e)
{
/** @var \Zend\ModuleManager\ModuleManager $moduleManager */
$moduleManager = $e->getApplication()->getServiceManager()->get('modulemanager');
/** @var \Zend\EventManager\SharedEventManager $sharedEvents */
$sharedEvents = $moduleManager->getEventManager()->getSharedManager();
$sharedEvents->attach('Zend\Mvc\Controller\AbstractRestfulController', MvcEvent::EVENT_DISPATCH, array($this, 'postProcess'), -100);
$sharedEvents->attach('Zend\Mvc\Application', MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'errorProcess'), 999);
}
/**
* return array
*/
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php',
),
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
/**
* @return array
*/
public function getConfig()
{
return include __DIR__ . '/configs/module.config.php';
}
/**
* @param MvcEvent $e
* @return null|\Zend\Http\PhpEnvironment\Response
*/
public function postProcess(MvcEvent $e)
{
$routeMatch = $e->getRouteMatch();
$formatter = $routeMatch->getParam('formatter', false);
/** @var \Zend\Di\Di $di */
$di = $e->getTarget()->getServiceLocator()->get('di');
if ($formatter !== false) {
if ($e->getResult() instanceof \Zend\View\Model\ViewModel) {
if (is_array($e->getResult()->getVariables())) {
$vars = $e->getResult()->getVariables();
} else {
$vars = null;
}
} else {
$vars = $e->getResult();
}
/** @var PostProcessor\AbstractPostProcessor $postProcessor */
$postProcessor = $di->get($formatter . '-pp', array(
'response' => $e->getResponse(),
'vars' => $vars,
));
$postProcessor->process();
return $postProcessor->getResponse();
}
return null;
}
/**
* @param MvcEvent $e
* @return null|\Zend\Http\PhpEnvironment\Response
*/
public function errorProcess(MvcEvent $e)
{
/** @var \Zend\Di\Di $di */
$di = $e->getApplication()->getServiceManager()->get('di');
$eventParams = $e->getParams();
/** @var array $configuration */
$configuration = $e->getApplication()->getConfig();
$vars = array();
if (isset($eventParams['exception'])) {
/** @var \Exception $exception */
$exception = $eventParams['exception'];
if ($configuration['errors']['show_exceptions']['message']) {
$vars['error-message'] = $exception->getMessage();
}
if ($configuration['errors']['show_exceptions']['trace']) {
$vars['error-trace'] = $exception->getTrace();
}
}
if (empty($vars)) {
$vars['error'] = 'Something went wrong';
}
/** @var PostProcessor\AbstractPostProcessor $postProcessor */
$postProcessor = $di->get(
$configuration['errors']['post_processor'],
array('vars' => $vars, 'response' => $e->getResponse())
);
$postProcessor->process();
if (
$eventParams['error'] === \Zend\Mvc\Application::ERROR_CONTROLLER_NOT_FOUND ||
$eventParams['error'] === \Zend\Mvc\Application::ERROR_ROUTER_NO_MATCH
) {
$e->getResponse()->setStatusCode(\Zend\Http\PhpEnvironment\Response::STATUS_CODE_501);
} else {
$e->getResponse()->setStatusCode(\Zend\Http\PhpEnvironment\Response::STATUS_CODE_500);
}
$e->stopPropagation();
return $postProcessor->getResponse();
}
}