1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of byrokrat\giroapp. |
4
|
|
|
* |
5
|
|
|
* byrokrat\giroapp is free software: you can redistribute it and/or |
6
|
|
|
* modify it under the terms of the GNU General Public License as published |
7
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* byrokrat\giroapp is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with byrokrat\giroapp. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
* |
18
|
|
|
* Copyright 2016-18 Hannes Forsgård |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
declare(strict_types = 1); |
22
|
|
|
|
23
|
|
|
namespace byrokrat\giroapp\Listener; |
24
|
|
|
|
25
|
|
|
use byrokrat\giroapp\Event\LogEvent; |
26
|
|
|
use byrokrat\giroapp\Events; |
27
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
28
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Write warning, info and debug messages to output |
32
|
|
|
*/ |
33
|
|
|
final class OutputtingSubscriber implements EventSubscriberInterface |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var OutputInterface |
37
|
|
|
*/ |
38
|
|
|
private $stdout; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var OutputInterface |
42
|
|
|
*/ |
43
|
|
|
private $errout; |
44
|
|
|
|
45
|
|
|
public static function getSubscribedEvents() |
46
|
|
|
{ |
47
|
|
|
return [ |
48
|
|
|
Events::ERROR => ['onError', -10], |
49
|
|
|
Events::WARNING => ['onWarning', -10], |
50
|
|
|
Events::INFO => ['onInfo', -10], |
51
|
|
|
Events::DEBUG => ['onDebug', -10], |
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function __construct(OutputInterface $stdout, OutputInterface $errout) |
56
|
|
|
{ |
57
|
|
|
$this->stdout = $stdout; |
58
|
|
|
$this->errout = $errout; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function onError(LogEvent $event): void |
62
|
|
|
{ |
63
|
|
|
$this->errout->writeln("<error>ERROR: {$event->getMessage()}</error>"); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function onWarning(LogEvent $event): void |
67
|
|
|
{ |
68
|
|
|
$this->errout->writeln("<question>WARNING: {$event->getMessage()}</question>"); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function onInfo(LogEvent $event): void |
72
|
|
|
{ |
73
|
|
|
$this->stdout->writeln($event->getMessage()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function onDebug(LogEvent $event): void |
77
|
|
|
{ |
78
|
|
|
if ($this->stdout->isVerbose()) { |
79
|
|
|
$this->stdout->writeln($event->getMessage()); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|