Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ProcessHelper.php

class ProcessHelper

The ProcessHelper class provides helpers to run external processes.

@author Fabien Potencier <fabien@symfony.com>

@final

Hierarchy

  • class \Symfony\Component\Console\Helper\Helper implements \Symfony\Component\Console\Helper\HelperInterface
    • class \Symfony\Component\Console\Helper\ProcessHelper extends \Symfony\Component\Console\Helper\Helper

Expanded class hierarchy of ProcessHelper

1 file declares its use of ProcessHelper
Application.php in vendor/symfony/console/Application.php

File

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

Namespace

Symfony\Component\Console\Helper
View source
class ProcessHelper extends Helper {
    
    /**
     * 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
     */
    public function run(OutputInterface $output, array|Process $cmd, ?string $error = null, ?callable $callback = null, int $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE) : Process {
        if (!class_exists(Process::class)) {
            throw new \LogicException('The ProcessHelper cannot be run as the Process component is not installed. Try running "compose require symfony/process".');
        }
        if ($output instanceof ConsoleOutputInterface) {
            $output = $output->getErrorOutput();
        }
        $formatter = $this->getHelperSet()
            ->get('debug_formatter');
        if ($cmd instanceof Process) {
            $cmd = [
                $cmd,
            ];
        }
        if (\is_string($cmd[0] ?? null)) {
            $process = new Process($cmd);
            $cmd = [];
        }
        elseif (($cmd[0] ?? null) instanceof Process) {
            $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 array|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
     *
     * @throws ProcessFailedException
     *
     * @see run()
     */
    public function mustRun(OutputInterface $output, array|Process $cmd, ?string $error = null, ?callable $callback = null, int $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE) : Process {
        $process = $this->run($output, $cmd, $error, $callback, $verbosity);
        if (!$process->isSuccessful()) {
            throw new ProcessFailedException($process);
        }
        return $process;
    }
    
    /**
     * Wraps a Process callback to add debugging output.
     */
    public function wrapCallback(OutputInterface $output, Process $process, ?callable $callback = null) : callable {
        if ($output instanceof ConsoleOutputInterface) {
            $output = $output->getErrorOutput();
        }
        $formatter = $this->getHelperSet()
            ->get('debug_formatter');
        return function ($type, $buffer) use ($output, $process, $callback, $formatter) {
            $output->write($formatter->progress(spl_object_hash($process), $this->escapeString($buffer), Process::ERR === $type));
            if (null !== $callback) {
                $callback($type, $buffer);
            }
        };
    }
    private function escapeString(string $str) : string {
        return str_replace('<', '\\<', $str);
    }
    public function getName() : string {
        return 'process';
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
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::length public static function Returns the length of a string, using mb_strlen if it is available.
The length is related to how many bytes the string will use.
Helper::removeDecoration public static function
Helper::setHelperSet public function Sets the helper set associated with this helper. Overrides HelperInterface::setHelperSet
Helper::substr public static function Returns the subset of a string, using mb_substr if it is available.
Helper::width public static function Returns the width of a string, using mb_strwidth if it is available.
The width is how many characters positions the string will use.
ProcessHelper::escapeString private function
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.

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal