-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module.php
127 lines (107 loc) · 4.03 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
<?php
namespace TCErrorHandler;
use ErrorException;
use Zend\Console\Request as ConsoleRequest;
use Zend\Mvc\Application;
use Zend\Mvc\MvcEvent;
use Zend\Stdlib\ErrorHandler;
class Module
{
public function onBootstrap(MvcEvent $e)
{
if (php_sapi_name() == 'cli') {
return;
}
$eventManager = $e->getApplication()->getEventManager();
// Error catching
ob_start();
error_reporting(E_ALL);
ini_set('display_errors', true);
$eventManager->attach(MvcEvent::EVENT_BOOTSTRAP, array($this, 'startMonitoringErrors'));
$eventManager->attach(MvcEvent::EVENT_DISPATCH, array($this, 'checkForNotFoundAction'));
$eventManager->attach(MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'checkForErrors'));
$eventManager->attach(MvcEvent::EVENT_RENDER_ERROR, array($this, 'checkForErrors'));
$eventManager->attach(MvcEvent::EVENT_RENDER, array($this, 'checkForErrors'));
$eventManager->attach(MvcEvent::EVENT_FINISH, array($this, 'checkForErrors'));
register_shutdown_function(array($this, 'throwFatalError'), $e);
}
public function startMonitoringErrors(MvcEvent $e)
{
ErrorHandler::start(E_ALL);
}
public function checkForNotFoundAction(MvcEvent $e)
{
$action = $e->getRouteMatch()->getParam('action');
if ($action == 'not-found') {
$events = $e->getApplication()->getEventManager();
$events->trigger(MvcEvent::EVENT_DISPATCH_ERROR, $e);
}
}
public function checkForErrors(MvcEvent $e)
{
try {
ErrorHandler::stop(true);
$this->startMonitoringErrors($e);
} catch (ErrorException $exception) {
$this->outputFatalError($exception, $e);
}
return;
}
public function throwFatalError(MvcEvent $e)
{
$error = error_get_last();
if ($error) {
$exception = new ErrorException($error['message'], 0, $error['type'], $error['file'], $error['line']);
$this->outputFatalError($exception, $e);
}
}
public function outputFatalError(ErrorException $exception, MvcEvent $e)
{
// Clean the buffer from previously badly rendered views
if (ob_get_level() >= 1) {
ob_end_clean();
}
$sm = $e->getApplication()->getServiceManager();
$request = $e->getRequest();
$manager = $sm->get('viewManager');
$renderer = $manager->getRenderer();
$config = $sm->get('Config');
$display = isset($config['view_manager']['display_exceptions']) ? $config['view_manager']['display_exceptions'] : null;
$layout = $manager->getLayoutTemplate();
$template = isset($config['view_manager']['exception_template']) ? $config['view_manager']['exception_template'] : null;
$viewType = get_class($manager->getViewModel());
// Console
if ($request instanceof ConsoleRequest || (bool) $display !== true) {
return;
}
// Get layout
$model = new $viewType();
$model->setTemplate($layout);
// Error page
if (null !== $template) {
$content = new $viewType(
array(
'exception' => $exception,
'display_exceptions' => $display
)
);
$content->setTemplate($template);
$result = $renderer->render($content);
$model->setVariables([
'content' => $result,
'exception' => $exception,
]);
}
$this->triggerErrorEvent($exception, $e);
echo $renderer->render($model);
exit;
}
public function triggerErrorEvent(ErrorException $exception, MvcEvent $e)
{
$e->setError(Application::ERROR_EXCEPTION);
$e->setParam('exception', $exception);
$events = $e->getApplication()->getEventManager();
//$events->trigger(MvcEvent::EVENT_RENDER_ERROR, $e);
return;
}
}