-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBaseLoggingCommand.php
More file actions
85 lines (68 loc) · 2.39 KB
/
BaseLoggingCommand.php
File metadata and controls
85 lines (68 loc) · 2.39 KB
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
<?php
declare(strict_types=1);
namespace ErrorHeroModule\Command;
use ErrorHeroModule\Handler\Logging;
use ErrorHeroModule\HeroTrait;
use Override;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;
use function ErrorHeroModule\isExcludedException;
abstract class BaseLoggingCommand extends Command
{
use HeroTrait;
private const string DISPLAY_SETTINGS = 'display-settings';
private array $errorHeroModuleConfig;
private Logging $logging;
/**
* MUST BE CALLED after __construct(), as service extends this base class may use dependency injection
*
* With `Laminas\ServiceManager`, you don't need to do anything \m/, there is `initializers` config already for it.
*/
public function init(array $errorHeroModuleConfig, Logging $logging): void
{
$this->errorHeroModuleConfig = $errorHeroModuleConfig;
$this->logging = $logging;
}
#[Override]
public function run(InputInterface $input, OutputInterface $output): int
{
try {
$this->phpError();
return parent::run($input, $output);
} catch (Throwable $throwable) {
}
$this->exceptionError($throwable);
// show default view if display_errors setting = 0.
return $this->showDefaultConsoleView($output);
}
private function exceptionError(Throwable $throwable): void
{
if (
isset($this->errorHeroModuleConfig[self::DISPLAY_SETTINGS]['exclude-exceptions'])
&& isExcludedException(
$this->errorHeroModuleConfig[self::DISPLAY_SETTINGS]['exclude-exceptions'],
$throwable
)
) {
throw $throwable;
}
$this->logging->handleErrorException($throwable);
if ($this->errorHeroModuleConfig[self::DISPLAY_SETTINGS]['display_errors']) {
throw $throwable;
}
}
private function showDefaultConsoleView(OutputInterface $output): int
{
$table = new Table($output);
$table->setColumnMaxWidth(0, 147);
$table
->setRows([
[$this->errorHeroModuleConfig[self::DISPLAY_SETTINGS]['console']['message']],
]);
$table->render();
return 1;
}
}