You are here

class ProcessHelper in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/console/Helper/ProcessHelper.php \Symfony\Component\Console\Helper\ProcessHelper

The ProcessHelper class provides helpers to run external processes.

@author Fabien Potencier <fabien@symfony.com>

Hierarchy

Expanded class hierarchy of ProcessHelper

2 files declare their use of ProcessHelper
Application.php in vendor/symfony/console/Application.php
ProcessHelperTest.php in vendor/symfony/console/Tests/Helper/ProcessHelperTest.php

File

vendor/symfony/console/Helper/ProcessHelper.php, line 25

Namespace

Symfony\Component\Console\Helper
View source
class ProcessHelper extends Helper {

  /**
   * Runs an external process.
   *
   * @param OutputInterface      $output    An OutputInterface instance
   * @param string|array|Process $cmd       An instance of Process or an array of arguments to escape and run or a command to run
   * @param string|null          $error     An error message that must be displayed if something went wrong
   * @param callable|null        $callback  A PHP callback to run whenever there is some
   *                                        output available on STDOUT or STDERR
   * @param int                  $verbosity The threshold for verbosity
   *
   * @return Process The process that ran
   */
  public function run(OutputInterface $output, $cmd, $error = null, $callback = null, $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE) {
    if ($output instanceof ConsoleOutputInterface) {
      $output = $output
        ->getErrorOutput();
    }
    $formatter = $this
      ->getHelperSet()
      ->get('debug_formatter');
    if (is_array($cmd)) {
      $process = ProcessBuilder::create($cmd)
        ->getProcess();
    }
    elseif ($cmd instanceof Process) {
      $process = $cmd;
    }
    else {
      $process = new Process($cmd);
    }
    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);
    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 OutputInterface $output   An OutputInterface instance
   * @param string|Process  $cmd      An instance of Process or a command to run
   * @param string|null     $error    An error message that must be displayed if something went wrong
   * @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()
   */
  public function mustRun(OutputInterface $output, $cmd, $error = null, $callback = null) {
    $process = $this
      ->run($output, $cmd, $error, $callback);
    if (!$process
      ->isSuccessful()) {
      throw new ProcessFailedException($process);
    }
    return $process;
  }

  /**
   * Wraps a Process callback to add debugging output.
   *
   * @param OutputInterface $output   An OutputInterface interface
   * @param Process         $process  The Process
   * @param callable|null   $callback A PHP callable
   *
   * @return callable
   */
  public function wrapCallback(OutputInterface $output, Process $process, $callback = null) {
    if ($output instanceof ConsoleOutputInterface) {
      $output = $output
        ->getErrorOutput();
    }
    $formatter = $this
      ->getHelperSet()
      ->get('debug_formatter');
    $that = $this;
    return function ($type, $buffer) use ($output, $process, $callback, $formatter, $that) {
      $output
        ->write($formatter
        ->progress(spl_object_hash($process), $that
        ->escapeString($buffer), Process::ERR === $type));
      if (null !== $callback) {
        call_user_func($callback, $type, $buffer);
      }
    };
  }

  /**
   * This method is public for PHP 5.3 compatibility, it should be private.
   *
   * @internal
   */
  public function escapeString($str) {
    return str_replace('<', '\\<', $str);
  }

  /**
   * {@inheritdoc}
   */
  public function getName() {
    return 'process';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Helper::$helperSet protected property
Helper::formatMemory public static function
Helper::formatTime public static function
Helper::getHelperSet public function Gets the helper set associated with this helper. Overrides HelperInterface::getHelperSet
Helper::setHelperSet public function Sets the helper set associated with this helper. Overrides HelperInterface::setHelperSet
Helper::strlen public static function Returns the length of a string, using mb_strwidth if it is available.
Helper::strlenWithoutDecoration public static function
ProcessHelper::escapeString public function This method is public for PHP 5.3 compatibility, it should be private.
ProcessHelper::getName public function Returns the canonical name of this helper. Overrides HelperInterface::getName
ProcessHelper::mustRun public function Runs the process.
ProcessHelper::run public function Runs an external process.
ProcessHelper::wrapCallback public function Wraps a Process callback to add debugging output.