[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/console/Helper/ -> ProcessHelper.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\Console\Helper;
  13  
  14  use Symfony\Component\Console\Output\ConsoleOutputInterface;
  15  use Symfony\Component\Console\Output\OutputInterface;
  16  use Symfony\Component\Process\Exception\ProcessFailedException;
  17  use Symfony\Component\Process\Process;
  18  use Symfony\Component\Process\ProcessBuilder;
  19  
  20  /**
  21   * The ProcessHelper class provides helpers to run external processes.
  22   *
  23   * @author Fabien Potencier <fabien@symfony.com>
  24   */
  25  class ProcessHelper extends Helper
  26  {
  27      /**
  28       * Runs an external process.
  29       *
  30       * @param OutputInterface      $output    An OutputInterface instance
  31       * @param string|array|Process $cmd       An instance of Process or an array of arguments to escape and run or a command to run
  32       * @param string|null          $error     An error message that must be displayed if something went wrong
  33       * @param callable|null        $callback  A PHP callback to run whenever there is some
  34       *                                        output available on STDOUT or STDERR
  35       * @param int                  $verbosity The threshold for verbosity
  36       *
  37       * @return Process The process that ran
  38       */
  39      public function run(OutputInterface $output, $cmd, $error = null, $callback = null, $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE)
  40      {
  41          if ($output instanceof ConsoleOutputInterface) {
  42              $output = $output->getErrorOutput();
  43          }
  44  
  45          $formatter = $this->getHelperSet()->get('debug_formatter');
  46  
  47          if (\is_array($cmd)) {
  48              $process = ProcessBuilder::create($cmd)->getProcess();
  49          } elseif ($cmd instanceof Process) {
  50              $process = $cmd;
  51          } else {
  52              $process = new Process($cmd);
  53          }
  54  
  55          if ($verbosity <= $output->getVerbosity()) {
  56              $output->write($formatter->start(spl_object_hash($process), $this->escapeString($process->getCommandLine())));
  57          }
  58  
  59          if ($output->isDebug()) {
  60              $callback = $this->wrapCallback($output, $process, $callback);
  61          }
  62  
  63          $process->run($callback);
  64  
  65          if ($verbosity <= $output->getVerbosity()) {
  66              $message = $process->isSuccessful() ? 'Command ran successfully' : sprintf('%s Command did not run successfully', $process->getExitCode());
  67              $output->write($formatter->stop(spl_object_hash($process), $message, $process->isSuccessful()));
  68          }
  69  
  70          if (!$process->isSuccessful() && null !== $error) {
  71              $output->writeln(sprintf('<error>%s</error>', $this->escapeString($error)));
  72          }
  73  
  74          return $process;
  75      }
  76  
  77      /**
  78       * Runs the process.
  79       *
  80       * This is identical to run() except that an exception is thrown if the process
  81       * exits with a non-zero exit code.
  82       *
  83       * @param OutputInterface $output   An OutputInterface instance
  84       * @param string|Process  $cmd      An instance of Process or a command to run
  85       * @param string|null     $error    An error message that must be displayed if something went wrong
  86       * @param callable|null   $callback A PHP callback to run whenever there is some
  87       *                                  output available on STDOUT or STDERR
  88       *
  89       * @return Process The process that ran
  90       *
  91       * @throws ProcessFailedException
  92       *
  93       * @see run()
  94       */
  95      public function mustRun(OutputInterface $output, $cmd, $error = null, $callback = null)
  96      {
  97          $process = $this->run($output, $cmd, $error, $callback);
  98  
  99          if (!$process->isSuccessful()) {
 100              throw new ProcessFailedException($process);
 101          }
 102  
 103          return $process;
 104      }
 105  
 106      /**
 107       * Wraps a Process callback to add debugging output.
 108       *
 109       * @param OutputInterface $output   An OutputInterface interface
 110       * @param Process         $process  The Process
 111       * @param callable|null   $callback A PHP callable
 112       *
 113       * @return callable
 114       */
 115      public function wrapCallback(OutputInterface $output, Process $process, $callback = null)
 116      {
 117          if ($output instanceof ConsoleOutputInterface) {
 118              $output = $output->getErrorOutput();
 119          }
 120  
 121          $formatter = $this->getHelperSet()->get('debug_formatter');
 122  
 123          $that = $this;
 124  
 125          return function ($type, $buffer) use ($output, $process, $callback, $formatter, $that) {
 126              $output->write($formatter->progress(spl_object_hash($process), $that->escapeString($buffer), Process::ERR === $type));
 127  
 128              if (null !== $callback) {
 129                  \call_user_func($callback, $type, $buffer);
 130              }
 131          };
 132      }
 133  
 134      /**
 135       * This method is public for PHP 5.3 compatibility, it should be private.
 136       *
 137       * @internal
 138       */
 139      public function escapeString($str)
 140      {
 141          return str_replace('<', '\\<', $str);
 142      }
 143  
 144      /**
 145       * {@inheritdoc}
 146       */
 147      public function getName()
 148      {
 149          return 'process';
 150      }
 151  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1