1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* MikoPBX - free phone system for small business |
4
|
|
|
* Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov |
5
|
|
|
* |
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU General Public License as published by |
8
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* This program is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
17
|
|
|
* If not, see <https://www.gnu.org/licenses/>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
declare(strict_types=1); |
21
|
|
|
|
22
|
|
|
namespace MikoPBX\Common\Providers; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
use MikoPBX\Modules\PbxExtensionUtils; |
26
|
|
|
use Throwable; |
27
|
|
|
use Whoops\Handler\JsonResponseHandler; |
28
|
|
|
use Whoops\Handler\PlainTextHandler; |
29
|
|
|
use Whoops\Handler\PrettyPageHandler; |
30
|
|
|
use Whoops\Run; |
31
|
|
|
use Whoops; |
32
|
|
|
use Phalcon\Di\DiInterface; |
33
|
|
|
use Phalcon\Di\ServiceProviderInterface; |
34
|
|
|
/** |
35
|
|
|
* Registers whoops error handler service provider. |
36
|
|
|
* |
37
|
|
|
* @package MikoPBX\Common\Providers |
38
|
|
|
*/ |
39
|
|
|
class WhoopsErrorHandlerProvider implements ServiceProviderInterface |
40
|
|
|
{ |
41
|
|
|
public const SERVICE_NAME = 'whoopsErrorHandler'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Registers whoops error handler service provider |
45
|
|
|
* |
46
|
|
|
* @param DiInterface $di The DI container. |
47
|
|
|
*/ |
48
|
|
|
public function register(DiInterface $di): void |
49
|
|
|
{ |
50
|
|
|
$di->set( |
51
|
|
|
self::SERVICE_NAME, |
52
|
|
|
function () { |
53
|
|
|
$whoops = new Run(); |
54
|
|
|
$whoops->pushHandler(function ($exception, $inspector, $run) { |
55
|
|
|
return WhoopsErrorHandlerProvider::handleModuleException($exception, $inspector, $run); |
56
|
|
|
}); |
57
|
|
|
if (Whoops\Util\Misc::isAjaxRequest()) { |
58
|
|
|
$handler = new JsonResponseHandler(); |
59
|
|
|
} elseif (Whoops\Util\Misc::isCommandLine()) { |
60
|
|
|
$handler = new PlainTextHandler(); |
61
|
|
|
} else { |
62
|
|
|
$handler = new PrettyPageHandler(); |
63
|
|
|
} |
64
|
|
|
$whoops->appendHandler($handler); |
65
|
|
|
$whoops->register(); |
66
|
|
|
return $whoops; |
67
|
|
|
} |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Handles a Whoops exception and disables the corresponding module. |
73
|
|
|
* |
74
|
|
|
* @param Throwable $exception The exception to handle. |
75
|
|
|
* @param Whoops\Exception\Inspector $inspector The Inspector instance. |
76
|
|
|
* @param Run $run The Run instance. |
77
|
|
|
* @return int The Whoops handler status. |
78
|
|
|
*/ |
79
|
|
|
private static function handleModuleException($exception, $inspector, $run): int |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$exceptionFile = $exception->getFile(); |
82
|
|
|
PbxExtensionUtils::disableBadModule($exceptionFile); |
83
|
|
|
return \Whoops\Handler\Handler::DONE; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Generates an error description for a given exception. |
88
|
|
|
* |
89
|
|
|
* @param Throwable $exception The exception for which to generate the error description. |
90
|
|
|
* @param bool $jsonResult Indicates whether the error description should be in JSON format. |
91
|
|
|
* @return string The generated error description. |
92
|
|
|
*/ |
93
|
|
|
public static function makePrettyErrorDescription(Throwable $exception, bool $jsonResult = false): string |
94
|
|
|
{ |
95
|
|
|
$whoops = new Run(); |
96
|
|
|
if ($jsonResult) { |
97
|
|
|
$whoops->pushHandler(new JsonResponseHandler()); |
98
|
|
|
} else { |
99
|
|
|
$whoops->pushHandler(new PlainTextHandler()); |
100
|
|
|
} |
101
|
|
|
$whoops->register(); |
102
|
|
|
$whoops->allowQuit(false); |
103
|
|
|
return $whoops->handleException($exception); |
104
|
|
|
} |
105
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.