This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Console/Helper/ProcessHelper.php

143 lines
4.7 KiB
PHP
Raw Normal View History

2014-04-01 17:31:55 +01: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.
*/
namespace Symfony\Component\Console\Helper;
use Symfony\Component\Console\Output\OutputInterface;
2014-04-03 09:19:54 +01:00
use Symfony\Component\Process\Exception\ProcessFailedException;
2014-04-01 17:31:55 +01:00
use Symfony\Component\Process\Process;
2014-04-03 09:19:54 +01:00
use Symfony\Component\Process\ProcessBuilder;
2014-04-01 17:31:55 +01:00
/**
2014-04-03 09:19:54 +01:00
* The ProcessHelper class provides helpers to run external processes.
2014-04-01 17:31:55 +01:00
*
* @author Fabien Potencier <fabien@symfony.com>
*/
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
2014-04-01 17:31:55 +01:00
*
* @return Process The process that ran
*/
public function run(OutputInterface $output, $cmd, $error = null, $callback = null, $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE)
2014-04-01 17:31:55 +01:00
{
$formatter = $this->getHelperSet()->get('debug_formatter');
2014-04-03 09:19:54 +01:00
if (is_array($cmd)) {
$process = ProcessBuilder::create($cmd)->getProcess();
} elseif ($cmd instanceof Process) {
$process = $cmd;
} else {
$process = new Process($cmd);
}
2014-04-01 17:31:55 +01:00
if ($verbosity <= $output->getVerbosity()) {
$output->write($formatter->start(spl_object_hash($process), $this->escapeString($process->getCommandLine())));
2014-04-01 17:31:55 +01:00
}
2014-04-03 09:19:54 +01:00
if ($output->isDebug()) {
2014-04-01 17:31:55 +01:00
$callback = $this->wrapCallback($output, $process, $callback);
}
$process->run($callback);
if ($verbosity <= $output->getVerbosity()) {
2014-04-03 09:19:54 +01:00
$message = $process->isSuccessful() ? 'Command ran successfully' : sprintf('%s Command did not run successfully', $process->getExitCode());
2014-04-01 17:31:55 +01:00
$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)));
2014-04-03 09:19:54 +01:00
}
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);
2014-04-01 17:31:55 +01:00
}
return $process;
}
/**
* Wraps a Process callback to add debugging output.
*
* @param OutputInterface $output An OutputInterface interface
2014-04-03 09:19:54 +01:00
* @param Process $process The Process
2014-04-01 17:31:55 +01:00
* @param callable|null $callback A PHP callable
2014-04-03 09:19:54 +01:00
*
* @return callable
2014-04-01 17:31:55 +01:00
*/
public function wrapCallback(OutputInterface $output, Process $process, $callback = null)
{
$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));
2014-04-01 17:31:55 +01:00
if (null !== $callback) {
2014-04-03 09:19:54 +01:00
call_user_func($callback, $type, $buffer);
2014-04-01 17:31:55 +01:00
}
};
}
/**
* This method is public for PHP 5.3 compatibility, it should be private.
*
* @internal
*/
public function escapeString($str)
{
return str_replace('<', '\\<', $str);
}
2014-04-01 17:31:55 +01:00
/**
2014-04-03 09:19:54 +01:00
* {@inheritdoc}
2014-04-01 17:31:55 +01:00
*/
public function getName()
{
return 'process';
}
}