|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* balloon |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copryright (c) 2012-2018 gyselroth GmbH (https://gyselroth.com) |
|
9
|
|
|
* @license GPL-3.0 https://opensource.org/licenses/GPL-3.0 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Balloon\Bootstrap; |
|
13
|
|
|
|
|
14
|
|
|
use Bramus\Monolog\Formatter\ColoredLineFormatter; |
|
15
|
|
|
use GetOpt\GetOpt; |
|
16
|
|
|
use Monolog\Handler\FilterHandler; |
|
17
|
|
|
use Monolog\Handler\StreamHandler; |
|
18
|
|
|
use Monolog\Logger; |
|
19
|
|
|
use Psr\Container\ContainerInterface; |
|
20
|
|
|
use Psr\Log\LoggerInterface; |
|
21
|
|
|
|
|
22
|
|
|
class Cli extends AbstractBootstrap |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Getopt. |
|
26
|
|
|
* |
|
27
|
|
|
* @var GetOpt |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $getopt; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Container. |
|
33
|
|
|
* |
|
34
|
|
|
* @var ContainerInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $container; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(LoggerInterface $logger, GetOpt $getopt, ContainerInterface $container) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->logger = $logger; |
|
44
|
|
|
$this->getopt = $getopt; |
|
45
|
|
|
$this->container = $container; |
|
46
|
|
|
$this->reduceLogLevel(); |
|
47
|
|
|
$this->setExceptionHandler(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Process. |
|
52
|
|
|
* |
|
53
|
|
|
* @return Cli |
|
54
|
|
|
*/ |
|
55
|
|
|
public function process() |
|
56
|
|
|
{ |
|
57
|
|
|
$this->getopt->addOption(['v', 'verbose', GetOpt::NO_ARGUMENT, 'Verbose']); |
|
58
|
|
|
$this->getopt->process(); |
|
59
|
|
|
$this->configureLogger($this->getopt->getOption('verbose')); |
|
60
|
|
|
$this->getopt->routeCommand($this->container); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Remove logger. |
|
67
|
|
|
* |
|
68
|
|
|
* @return Cli |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function reduceLogLevel(): self |
|
71
|
|
|
{ |
|
72
|
|
|
foreach ($this->logger->getHandlers() as $handler) { |
|
|
|
|
|
|
73
|
|
|
if ($handler instanceof StreamHandler) { |
|
74
|
|
|
if ($handler->getUrl() === 'php://stderr' || $handler->getUrl() === 'php://stdout') { |
|
75
|
|
|
$handler->setLevel(600); |
|
76
|
|
|
} |
|
77
|
|
|
} elseif ($handler instanceof FilterHandler) { |
|
78
|
|
|
$handler->setAcceptedLevels(1000, 1000); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $this; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Configure cli logger. |
|
87
|
|
|
* |
|
88
|
|
|
* @return Cli |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function configureLogger(?int $level = null): self |
|
91
|
|
|
{ |
|
92
|
|
|
if (null === $level) { |
|
93
|
|
|
$level = 400; |
|
94
|
|
|
} else { |
|
95
|
|
|
$level = (4 - $level) * 100; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$formatter = new ColoredLineFormatter(); |
|
99
|
|
|
$handler = new StreamHandler('php://stderr', Logger::EMERGENCY); |
|
100
|
|
|
$handler->setFormatter($formatter); |
|
101
|
|
|
$this->logger->pushHandler($handler); |
|
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
$handler = new StreamHandler('php://stdout', $level); |
|
104
|
|
|
$filter = new FilterHandler($handler, $level, Logger::ERROR); |
|
|
|
|
|
|
105
|
|
|
$handler->setFormatter($formatter); |
|
106
|
|
|
|
|
107
|
|
|
$this->logger->pushHandler($filter); |
|
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Set exception handler. |
|
114
|
|
|
* |
|
115
|
|
|
* @return Cli |
|
116
|
|
|
*/ |
|
117
|
|
|
protected function setExceptionHandler(): self |
|
118
|
|
|
{ |
|
119
|
|
|
set_exception_handler(function ($e) { |
|
120
|
|
|
$this->logger->emergency('uncaught exception: '.$e->getMessage(), [ |
|
121
|
|
|
'category' => get_class($this), |
|
122
|
|
|
'exception' => $e, |
|
123
|
|
|
]); |
|
124
|
|
|
}); |
|
125
|
|
|
|
|
126
|
|
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: