20
20
use Symfony \Component \Console \ConsoleEvents ;
21
21
use Symfony \Component \Console \Event \ConsoleCommandEvent ;
22
22
use Symfony \Component \Console \Event \ConsoleTerminateEvent ;
23
+ use Symfony \Component \Console \Input \InputInterface ;
23
24
use Symfony \Component \Console \Output \ConsoleOutputInterface ;
24
25
use Symfony \Component \Console \Output \OutputInterface ;
25
26
use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
@@ -52,6 +53,8 @@ final class ConsoleHandler extends AbstractProcessingHandler implements EventSub
52
53
OutputInterface::VERBOSITY_DEBUG => Level::Debug,
53
54
];
54
55
56
+ private ?InputInterface $ input = null ;
57
+
55
58
/**
56
59
* @param OutputInterface|null $output The console output to use (the handler remains disabled when passing null
57
60
* until the output is set, e.g. by using console events)
@@ -64,6 +67,7 @@ public function __construct(
64
67
bool $ bubble = true ,
65
68
array $ verbosityLevelMap = [],
66
69
private array $ consoleFormatterOptions = [],
70
+ private bool $ interactiveOnly = false ,
67
71
) {
68
72
parent ::__construct (Level::Debug, $ bubble );
69
73
@@ -74,7 +78,16 @@ public function __construct(
74
78
75
79
public function isHandling (LogRecord $ record ): bool
76
80
{
77
- return $ this ->updateLevel () && parent ::isHandling ($ record );
81
+ return $ this ->isInteractiveOnlyEnabled () || ($ this ->updateLevel () && parent ::isHandling ($ record ));
82
+ }
83
+
84
+ public function getBubble (): bool
85
+ {
86
+ if ($ this ->isInteractiveOnlyEnabled ()) {
87
+ return false ;
88
+ }
89
+
90
+ return parent ::getBubble ();
78
91
}
79
92
80
93
public function handle (LogRecord $ record ): bool
@@ -84,6 +97,11 @@ public function handle(LogRecord $record): bool
84
97
return $ this ->updateLevel () && parent ::handle ($ record );
85
98
}
86
99
100
+ public function setInput (InputInterface $ input ): void
101
+ {
102
+ $ this ->input = $ input ;
103
+ }
104
+
87
105
/**
88
106
* Sets the console output to use for printing logs.
89
107
*/
@@ -108,6 +126,9 @@ public function close(): void
108
126
*/
109
127
public function onCommand (ConsoleCommandEvent $ event ): void
110
128
{
129
+ $ input = $ event ->getInput ();
130
+ $ this ->setInput ($ input );
131
+
111
132
$ output = $ event ->getOutput ();
112
133
if ($ output instanceof ConsoleOutputInterface) {
113
134
$ output = $ output ->getErrorOutput ();
@@ -173,4 +194,9 @@ private function updateLevel(): bool
173
194
174
195
return true ;
175
196
}
197
+
198
+ private function isInteractiveOnlyEnabled (): bool
199
+ {
200
+ return $ this ->interactiveOnly && $ this ->input && $ this ->input ->isInteractive ();
201
+ }
176
202
}
0 commit comments