2021-05-10 00:23:30 +00:00
< ? php
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2021-08-18 09:22:43 +00:00
namespace RectorPrefix20210818\Symfony\Component\Console\Helper ;
2021-05-10 00:23:30 +00:00
2021-08-18 09:22:43 +00:00
use RectorPrefix20210818\Symfony\Component\Console\Output\ConsoleOutputInterface ;
use RectorPrefix20210818\Symfony\Component\Console\Output\OutputInterface ;
use RectorPrefix20210818\Symfony\Component\Process\Exception\ProcessFailedException ;
use RectorPrefix20210818\Symfony\Component\Process\Process ;
2021-05-10 00:23:30 +00:00
/**
* The ProcessHelper class provides helpers to run external processes .
*
* @ author Fabien Potencier < fabien @ symfony . com >
*
* @ final
*/
2021-08-18 09:22:43 +00:00
class ProcessHelper extends \RectorPrefix20210818\Symfony\Component\Console\Helper\Helper
2021-05-10 00:23:30 +00:00
{
/**
* Runs an external process .
*
* @ param array | Process $cmd An instance of Process or an array of the command and arguments
* @ param callable | null $callback A PHP callback to run whenever there is some
* output available on STDOUT or STDERR
*
* @ return Process The process that ran
2021-07-05 22:50:18 +00:00
* @ param \Symfony\Component\Console\Output\OutputInterface $output
* @ param string | null $error
* @ param int $verbosity
2021-05-10 00:23:30 +00:00
*/
2021-08-18 09:22:43 +00:00
public function run ( $output , $cmd , $error = null , $callback = null , $verbosity = \RectorPrefix20210818\Symfony\Component\Console\Output\OutputInterface :: VERBOSITY_VERY_VERBOSE ) : \RectorPrefix20210818\Symfony\Component\Process\Process
2021-05-10 00:23:30 +00:00
{
2021-08-18 09:22:43 +00:00
if ( ! \class_exists ( \RectorPrefix20210818\Symfony\Component\Process\Process :: class )) {
2021-05-10 00:23:30 +00:00
throw new \LogicException ( 'The ProcessHelper cannot be run as the Process component is not installed. Try running "compose require symfony/process".' );
}
2021-08-18 09:22:43 +00:00
if ( $output instanceof \RectorPrefix20210818\Symfony\Component\Console\Output\ConsoleOutputInterface ) {
2021-05-10 00:23:30 +00:00
$output = $output -> getErrorOutput ();
}
$formatter = $this -> getHelperSet () -> get ( 'debug_formatter' );
2021-08-18 09:22:43 +00:00
if ( $cmd instanceof \RectorPrefix20210818\Symfony\Component\Process\Process ) {
2021-05-10 00:23:30 +00:00
$cmd = [ $cmd ];
}
if ( ! \is_array ( $cmd )) {
2021-08-18 09:22:43 +00:00
throw new \TypeError ( \sprintf ( 'The "command" argument of "%s()" must be an array or a "%s" instance, "%s" given.' , __METHOD__ , \RectorPrefix20210818\Symfony\Component\Process\Process :: class , \get_debug_type ( $cmd )));
2021-05-10 00:23:30 +00:00
}
if ( \is_string ( $cmd [ 0 ] ? ? null )) {
2021-08-18 09:22:43 +00:00
$process = new \RectorPrefix20210818\Symfony\Component\Process\Process ( $cmd );
2021-05-10 00:23:30 +00:00
$cmd = [];
2021-08-18 09:22:43 +00:00
} elseif (( $cmd [ 0 ] ? ? null ) instanceof \RectorPrefix20210818\Symfony\Component\Process\Process ) {
2021-05-10 00:23:30 +00:00
$process = $cmd [ 0 ];
unset ( $cmd [ 0 ]);
} else {
throw new \InvalidArgumentException ( \sprintf ( 'Invalid command provided to "%s()": the command should be an array whose first element is either the path to the binary to run or a "Process" object.' , __METHOD__ ));
}
if ( $verbosity <= $output -> getVerbosity ()) {
$output -> write ( $formatter -> start ( \spl_object_hash ( $process ), $this -> escapeString ( $process -> getCommandLine ())));
}
if ( $output -> isDebug ()) {
$callback = $this -> wrapCallback ( $output , $process , $callback );
}
$process -> run ( $callback , $cmd );
if ( $verbosity <= $output -> getVerbosity ()) {
$message = $process -> isSuccessful () ? 'Command ran successfully' : \sprintf ( '%s Command did not run successfully' , $process -> getExitCode ());
$output -> write ( $formatter -> stop ( \spl_object_hash ( $process ), $message , $process -> isSuccessful ()));
}
if ( ! $process -> isSuccessful () && null !== $error ) {
$output -> writeln ( \sprintf ( '<error>%s</error>' , $this -> escapeString ( $error )));
}
return $process ;
}
/**
* Runs the process .
*
* This is identical to run () except that an exception is thrown if the process
* exits with a non - zero exit code .
*
* @ param string | Process $cmd An instance of Process or a command to run
* @ param callable | null $callback A PHP callback to run whenever there is some
* output available on STDOUT or STDERR
*
* @ return Process The process that ran
*
* @ throws ProcessFailedException
*
* @ see run ()
2021-07-05 22:50:18 +00:00
* @ param \Symfony\Component\Console\Output\OutputInterface $output
* @ param string | null $error
2021-05-10 00:23:30 +00:00
*/
2021-08-18 09:22:43 +00:00
public function mustRun ( $output , $cmd , $error = null , $callback = null ) : \RectorPrefix20210818\Symfony\Component\Process\Process
2021-05-10 00:23:30 +00:00
{
$process = $this -> run ( $output , $cmd , $error , $callback );
if ( ! $process -> isSuccessful ()) {
2021-08-18 09:22:43 +00:00
throw new \RectorPrefix20210818\Symfony\Component\Process\Exception\ProcessFailedException ( $process );
2021-05-10 00:23:30 +00:00
}
return $process ;
}
/**
* Wraps a Process callback to add debugging output .
2021-07-05 22:50:18 +00:00
* @ param \Symfony\Component\Console\Output\OutputInterface $output
* @ param \Symfony\Component\Process\Process $process
* @ param callable | null $callback
2021-05-10 00:23:30 +00:00
*/
2021-07-05 22:50:18 +00:00
public function wrapCallback ( $output , $process , $callback = null ) : callable
2021-05-10 00:23:30 +00:00
{
2021-08-18 09:22:43 +00:00
if ( $output instanceof \RectorPrefix20210818\Symfony\Component\Console\Output\ConsoleOutputInterface ) {
2021-05-10 00:23:30 +00:00
$output = $output -> getErrorOutput ();
}
$formatter = $this -> getHelperSet () -> get ( 'debug_formatter' );
return function ( $type , $buffer ) use ( $output , $process , $callback , $formatter ) {
2021-08-18 09:22:43 +00:00
$output -> write ( $formatter -> progress ( \spl_object_hash ( $process ), $this -> escapeString ( $buffer ), \RectorPrefix20210818\Symfony\Component\Process\Process :: ERR === $type ));
2021-05-10 00:23:30 +00:00
if ( null !== $callback ) {
$callback ( $type , $buffer );
}
};
}
2021-07-05 23:06:30 +00:00
private function escapeString ( string $str ) : string
2021-05-10 00:23:30 +00:00
{
return \str_replace ( '<' , '\\<' , $str );
}
/**
* { @ inheritdoc }
*/
public function getName () : string
{
return 'process' ;
}
}