2020use Symfony \Component \Console \ConsoleEvents ;
2121use Symfony \Component \Console \Event \ConsoleCommandEvent ;
2222use Symfony \Component \Console \Event \ConsoleTerminateEvent ;
23+ use Symfony \Component \Console \Input \InputInterface ;
2324use Symfony \Component \Console \Output \ConsoleOutputInterface ;
2425use Symfony \Component \Console \Output \OutputInterface ;
2526use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
@@ -64,6 +65,8 @@ public function __construct(
6465 bool $ bubble = true ,
6566 array $ verbosityLevelMap = [],
6667 private array $ consoleFormatterOptions = [],
68+ private ?InputInterface $ input = null ,
69+ private bool $ interactiveOnly = false ,
6770 ) {
6871 parent ::__construct (Level::Debug, $ bubble );
6972
@@ -74,7 +77,16 @@ public function __construct(
7477
7578 public function isHandling (LogRecord $ record ): bool
7679 {
77- return $ this ->updateLevel () && parent ::isHandling ($ record );
80+ return $ this ->isInteractiveOnlyEnabled () || ($ this ->updateLevel () && parent ::isHandling ($ record ));
81+ }
82+
83+ public function getBubble (): bool
84+ {
85+ if ($ this ->isInteractiveOnlyEnabled ()) {
86+ return false ;
87+ }
88+
89+ return parent ::getBubble ();
7890 }
7991
8092 public function handle (LogRecord $ record ): bool
@@ -84,6 +96,11 @@ public function handle(LogRecord $record): bool
8496 return $ this ->updateLevel () && parent ::handle ($ record );
8597 }
8698
99+ public function setInput (InputInterface $ input ): void
100+ {
101+ $ this ->input = $ input ;
102+ }
103+
87104 /**
88105 * Sets the console output to use for printing logs.
89106 */
@@ -108,6 +125,9 @@ public function close(): void
108125 */
109126 public function onCommand (ConsoleCommandEvent $ event ): void
110127 {
128+ $ input = $ event ->getInput ();
129+ $ this ->setInput ($ input );
130+
111131 $ output = $ event ->getOutput ();
112132 if ($ output instanceof ConsoleOutputInterface) {
113133 $ output = $ output ->getErrorOutput ();
@@ -173,4 +193,9 @@ private function updateLevel(): bool
173193
174194 return true ;
175195 }
196+
197+ private function isInteractiveOnlyEnabled (): bool
198+ {
199+ return $ this ->interactiveOnly && $ this ->input !== null && $ this ->input ->isInteractive ();
200+ }
176201}
0 commit comments