JBZoo\Utils\Cli::exec PHP Method

exec() public static method

Execute cli commands
public static exec ( string $command, array $args = [], null $cwd = null, boolean $verbose = false ) : string
$command string
$args array
$cwd null
$verbose boolean
return string
    public static function exec($command, $args = array(), $cwd = null, $verbose = false)
    {
        if (!class_exists('\\Symfony\\Component\\Process\\Process')) {
            throw new \Exception("Symfony/Process package required for Cli::exec() method");
            // @codeCoverageIgnore
        }
        $cmd = self::build($command, $args);
        $cwd = $cwd ? $cwd = realpath($cwd) : null;
        //@codeCoverageIgnoreStart
        if ($verbose) {
            // Only in testing mode
            if (function_exists('\\JBZoo\\PHPUnit\\cliMessage')) {
                \JBZoo\PHPUnit\cliMessage('Process: ' . $cmd);
                \JBZoo\PHPUnit\cliMessage('CWD: ' . $cwd);
            } else {
                Cli::out('Process: ' . $cmd);
                Cli::out('CWD: ' . $cwd);
            }
        }
        //@codeCoverageIgnoreEnd
        // execute command
        $process = new Process($cmd, $cwd);
        $process->run();
        // executes after the command finishes
        if (!$process->isSuccessful()) {
            throw new ProcessFailedException($process);
        }
        return $process->getOutput();
    }

Usage Example

示例#1
0
 /**
  * Progress wrapper (if use stepmode).
  *
  * @param string $name
  * @param int $total
  * @param int $stepSize
  * @param \Closure $onStart
  * @param \Closure $onStepMode
  * @param \Closure $onFinish
  */
 protected function _progressWrap($name, $total, $stepSize, $onStart, $onStepMode, $onFinish)
 {
     $_this = $this;
     $step = $this->_getOpt('step');
     $profile = $this->_getOpt('profile');
     $stepMode = $this->_getOpt('stepmode');
     $isFinished = false;
     if ($stepMode) {
         if ($step >= 0) {
             $onStepMode($step);
         } else {
             $this->_progressBar($name, $total, $stepSize, function ($currentStep) use($profile, $total, $_this, $name) {
                 $phpBin = Env::getBinary();
                 $binPath = './' . FS::getRelative($_SERVER['SCRIPT_FILENAME'], JPATH_ROOT, '/');
                 $options = array('profile' => $profile, 'step' => (int) $currentStep, 'stepmode' => '', 'q' => '');
                 $command = $phpBin . ' ' . $binPath . ' ' . $name;
                 $result = Cli::exec($command, $options, JPATH_ROOT, false);
                 if (0 && $this->_isDebug()) {
                     $_this->_($result);
                 }
                 return $currentStep <= $total;
             });
             $isFinished = true;
         }
     } else {
         $isFinished = $onStart();
     }
     $onFinish($isFinished);
 }
All Usage Examples Of JBZoo\Utils\Cli::exec