|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Blitz PHP framework. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2022 Dimitri Sitchet Tomkeu <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view |
|
9
|
|
|
* the LICENSE file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace BlitzPHP\Debug; |
|
13
|
|
|
|
|
14
|
|
|
use BlitzPHP\Container\Services; |
|
15
|
|
|
use BlitzPHP\View\View; |
|
16
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
|
17
|
|
|
use Throwable; |
|
18
|
|
|
use Whoops\Handler\Handler; |
|
|
|
|
|
|
19
|
|
|
use Whoops\Handler\HandlerInterface; |
|
|
|
|
|
|
20
|
|
|
use Whoops\Handler\JsonResponseHandler; |
|
|
|
|
|
|
21
|
|
|
use Whoops\Handler\PlainTextHandler; |
|
|
|
|
|
|
22
|
|
|
use Whoops\Handler\PrettyPageHandler; |
|
|
|
|
|
|
23
|
|
|
use Whoops\Inspector\InspectorInterface; |
|
|
|
|
|
|
24
|
|
|
use Whoops\Run; |
|
|
|
|
|
|
25
|
|
|
use Whoops\RunInterface; |
|
|
|
|
|
|
26
|
|
|
use Whoops\Util\Misc; |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Gestionnaire d'exceptions |
|
30
|
|
|
*/ |
|
31
|
|
|
class ExceptionManager |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* Gestionnaire d'exceptions de type http (404, 500) qui peuvent avoir une page d'erreur personnalisée. |
|
35
|
|
|
*/ |
|
36
|
|
|
public static function registerHttpErrors(Run $debugger, array $config): Run |
|
37
|
|
|
{ |
|
38
|
|
|
return $debugger->pushHandler(static function (Throwable $exception, InspectorInterface $inspector, RunInterface $run) use($config) { |
|
|
|
|
|
|
39
|
|
|
if (true === $config['log']) { |
|
40
|
|
|
if (! in_array($exception->getCode(), $config['ignore_codes'], true)) { |
|
41
|
|
|
Services::logger()->error($exception); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$files = array_map(fn(SplFileInfo $file) => $file->getFilenameWithoutExtension(), Services::fs()->files($config['error_view_path'])); |
|
46
|
|
|
|
|
47
|
|
|
if (in_array((string)$exception->getCode(), $files, true)) { |
|
48
|
|
|
$view = new View(); |
|
49
|
|
|
$view->setAdapter(config('view.active_adapter', 'native'), ['view_path_locator' => $config['error_view_path']]) |
|
|
|
|
|
|
50
|
|
|
->display((string)$exception->getCode()) |
|
51
|
|
|
->setData(['message' => $exception->getMessage()]) |
|
52
|
|
|
->render(); |
|
53
|
|
|
|
|
54
|
|
|
return Handler::QUIT; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return Handler::DONE; |
|
58
|
|
|
}); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Gestionnaire d'applications fournis par le developpeur. |
|
63
|
|
|
*/ |
|
64
|
|
|
public static function registerAppHandlers(Run $debugger, array $config): Run |
|
65
|
|
|
{ |
|
66
|
|
|
|
|
67
|
|
|
foreach ($config['handlers'] ?? [] as $handler) { |
|
68
|
|
|
|
|
69
|
|
|
if (is_callable($handler)) { |
|
70
|
|
|
$debugger->pushHandler($handler); |
|
71
|
|
|
} else if(is_string($handler) && class_exists($handler)) { |
|
72
|
|
|
$class = Services::container()->make($handler); |
|
73
|
|
|
if (is_callable($class) || $class instanceof HandlerInterface) { |
|
74
|
|
|
$debugger->pushHandler($class); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $debugger; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Gestionnaire d'erreurs globales whoops |
|
84
|
|
|
*/ |
|
85
|
|
|
public static function registerWhoopsHandler(Run $debugger, array $config): Run |
|
86
|
|
|
{ |
|
87
|
|
|
if (Misc::isCommandLine()) { |
|
88
|
|
|
$debugger->pushHandler(new PlainTextHandler()); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if (! is_online()) { |
|
92
|
|
|
if (Misc::isAjaxRequest()) { |
|
93
|
|
|
$debugger->pushHandler(new JsonResponseHandler()); |
|
94
|
|
|
} else { |
|
95
|
|
|
$handler = new PrettyPageHandler(); |
|
96
|
|
|
|
|
97
|
|
|
$handler->setEditor($config['editor'] ?: PrettyPageHandler::EDITOR_VSCODE); |
|
98
|
|
|
$handler->setPageTitle($config['title'] ?: $handler->getPageTitle()); |
|
99
|
|
|
$handler->setApplicationRootPath(APP_PATH); |
|
100
|
|
|
$handler->setApplicationPaths([APP_PATH, SYST_PATH, VENDOR_PATH]); |
|
101
|
|
|
|
|
102
|
|
|
$handler = self::setBlacklist($handler, $config['blacklist']); |
|
103
|
|
|
|
|
104
|
|
|
foreach ($config['data'] as $label => $data) { |
|
105
|
|
|
if (is_array($data)) { |
|
106
|
|
|
$handler->addDataTable($label, $data); |
|
107
|
|
|
} elseif (is_callable($data)) { |
|
108
|
|
|
$handler->addDataTableCallback($label, $data); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$debugger->pushHandler($handler); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $debugger; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Enregistre les elements blacklisté dans l'affichage du rapport d'erreur |
|
122
|
|
|
*/ |
|
123
|
|
|
private static function setBlacklist(PrettyPageHandler $handler, array $blacklists): PrettyPageHandler |
|
124
|
|
|
{ |
|
125
|
|
|
foreach ($blacklists as $blacklist) { |
|
126
|
|
|
[$name, $key] = explode('/', $blacklist) + [1 => '*']; |
|
127
|
|
|
|
|
128
|
|
|
if ($name[0] !== '_') { |
|
129
|
|
|
$name = '_' . $name; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$name = strtoupper($name); |
|
133
|
|
|
|
|
134
|
|
|
if ($key !== '*') { |
|
135
|
|
|
foreach (explode(',', $key) as $k) { |
|
136
|
|
|
$handler->blacklist($name, $k); |
|
137
|
|
|
} |
|
138
|
|
|
} else { |
|
139
|
|
|
$values = match($name) { |
|
140
|
|
|
'_GET' => $_GET, |
|
141
|
|
|
'_POST' => $_POST, |
|
142
|
|
|
'_COOKIE' => $_COOKIE, |
|
143
|
|
|
'_SERVER' => $_SERVER, |
|
144
|
|
|
'_ENV' => $_ENV, |
|
145
|
|
|
'_FILES' => $_FILES ?? [], |
|
146
|
|
|
'_SESSION' => $_SESSION ?? [], |
|
147
|
|
|
default => [], |
|
148
|
|
|
}; |
|
149
|
|
|
foreach ($values as $key => $value) { |
|
150
|
|
|
$handler->blacklist($name, $key); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
return $handler; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
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