2021-04-18 22:24:02 +02:00
< ? php
namespace DigiComp\FlowSymfonyBridge\Messenger\Command ;
use Symfony\Component\Console\Command\Command ;
use Symfony\Component\Console\Input\ArgvInput ;
use Symfony\Component\Console\Input\InputArgument ;
use Symfony\Component\Console\Input\InputOption ;
use Symfony\Component\Console\Output\OutputInterface ;
trait RunSymfonyCommandTrait
{
2023-02-18 22:53:05 +01:00
protected function run ( Command $command ) : void
2021-04-18 22:24:02 +02:00
{
$definition = $command -> getDefinition ();
2022-09-18 13:17:53 +02:00
$definition -> setArguments ( \array_merge (
2021-04-18 22:24:02 +02:00
[ new InputArgument ( 'command' , InputArgument :: REQUIRED )],
$definition -> getArguments ()
));
2022-09-18 13:17:53 +02:00
$definition -> setOptions ( \array_merge (
2021-04-18 22:24:02 +02:00
[
new InputOption ( '--verbose' , '-v|vv|vvv' , InputOption :: VALUE_NONE , 'Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug' ),
new InputOption ( '--quiet' , '-q' , InputOption :: VALUE_NONE , 'Do not output any message' ),
],
$definition -> getOptions ()
));
$input = new ArgvInput ( null , $command -> getDefinition ());
$this -> configureIO ( $input , $this -> output -> getOutput ());
$command -> run ( $input , $this -> output -> getOutput ());
}
2023-02-18 22:53:05 +01:00
protected function configureIO ( $input , $output ) : void
2021-04-18 22:24:02 +02:00
{
2022-09-18 13:17:53 +02:00
switch ( $shellVerbosity = ( int ) \getenv ( 'SHELL_VERBOSITY' )) {
2021-04-18 22:24:02 +02:00
case - 1 :
$output -> setVerbosity ( OutputInterface :: VERBOSITY_QUIET );
break ;
case 1 :
$output -> setVerbosity ( OutputInterface :: VERBOSITY_VERBOSE );
break ;
case 2 :
$output -> setVerbosity ( OutputInterface :: VERBOSITY_VERY_VERBOSE );
break ;
case 3 :
$output -> setVerbosity ( OutputInterface :: VERBOSITY_DEBUG );
break ;
default :
$shellVerbosity = 0 ;
break ;
}
if ( true === $input -> hasParameterOption ([ '--quiet' , '-q' ], true )) {
$output -> setVerbosity ( OutputInterface :: VERBOSITY_QUIET );
$shellVerbosity = - 1 ;
} else {
if (
$input -> hasParameterOption ( '-vvv' , true )
|| $input -> hasParameterOption ( '--verbose=3' , true )
|| 3 === $input -> getParameterOption ( '--verbose' , false , true )
) {
$output -> setVerbosity ( OutputInterface :: VERBOSITY_DEBUG );
$shellVerbosity = 3 ;
} elseif (
$input -> hasParameterOption ( '-vv' , true )
|| $input -> hasParameterOption ( '--verbose=2' , true )
|| 2 === $input -> getParameterOption ( '--verbose' , false , true )
) {
$output -> setVerbosity ( OutputInterface :: VERBOSITY_VERY_VERBOSE );
$shellVerbosity = 2 ;
} elseif (
$input -> hasParameterOption ( '-v' , true )
|| $input -> hasParameterOption ( '--verbose=1' , true )
|| $input -> hasParameterOption ( '--verbose' , true )
|| $input -> getParameterOption ( '--verbose' , false , true )
) {
$output -> setVerbosity ( OutputInterface :: VERBOSITY_VERBOSE );
$shellVerbosity = 1 ;
}
}
if ( - 1 === $shellVerbosity ) {
$input -> setInteractive ( false );
}
2022-09-18 13:17:53 +02:00
\putenv ( 'SHELL_VERBOSITY=' . $shellVerbosity );
2021-04-18 22:24:02 +02:00
$_ENV [ 'SHELL_VERBOSITY' ] = $shellVerbosity ;
$_SERVER [ 'SHELL_VERBOSITY' ] = $shellVerbosity ;
}
}