use Symfony\Component\Console\Exception\ExceptionInterface;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Helper\DebugFormatterHelper;
+use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Helper\ProcessHelper;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
* @param OutputInterface $output An Output instance
*
* @return int 0 if everything went fine, or an error code
+ *
+ * @throws \Exception When running fails. Bypass this when {@link setCatchExceptions()}.
*/
public function run(InputInterface $input = null, OutputInterface $output = null)
{
$this->configureIO($input, $output);
try {
+ $e = null;
$exitCode = $this->doRun($input, $output);
- } catch (\Exception $e) {
- if (!$this->catchExceptions) {
- throw $e;
+ } catch (\Exception $x) {
+ $e = $x;
+ } catch (\Throwable $x) {
+ $e = new FatalThrowableError($x);
+ }
+
+ if (null !== $e) {
+ if (!$this->catchExceptions || !$x instanceof \Exception) {
+ throw $x;
}
if ($output instanceof ConsoleOutputInterface) {
$input = new ArrayInput(array('command' => $this->defaultCommand));
}
+ $this->runningCommand = null;
// the command name MUST be the first element of the input
$command = $this->find($name);
do {
$title = sprintf(' [%s] ', get_class($e));
- $len = $this->stringWidth($title);
+ $len = Helper::strlen($title);
$width = $this->getTerminalWidth() ? $this->getTerminalWidth() - 1 : PHP_INT_MAX;
// HHVM only accepts 32 bits integer in str_split, even when PHP_INT_MAX is a 64 bit integer: https://github.com/facebook/hhvm/issues/1327
foreach (preg_split('/\r?\n/', $e->getMessage()) as $line) {
foreach ($this->splitStringByWidth($line, $width - 4) as $line) {
// pre-format lines to get the right string length
- $lineLength = $this->stringWidth($line) + 4;
+ $lineLength = Helper::strlen($line) + 4;
$lines[] = array($line, $lineLength);
$len = max($lineLength, $len);
$messages = array();
$messages[] = $emptyLine = sprintf('<error>%s</error>', str_repeat(' ', $len));
- $messages[] = sprintf('<error>%s%s</error>', $title, str_repeat(' ', max(0, $len - $this->stringWidth($title))));
+ $messages[] = sprintf('<error>%s%s</error>', $title, str_repeat(' ', max(0, $len - Helper::strlen($title))));
foreach ($lines as $line) {
$messages[] = sprintf('<error> %s %s</error>', OutputFormatter::escape($line[0]), str_repeat(' ', $len - $line[1]));
}
}
if (null === $this->dispatcher) {
- try {
- return $command->run($input, $output);
- } catch (\Exception $e) {
- throw $e;
- } catch (\Throwable $e) {
- throw new FatalThrowableError($e);
- }
+ return $command->run($input, $output);
}
// bind before the console.command event, so the listeners have access to input options/arguments
}
$event = new ConsoleCommandEvent($command, $input, $output);
- $this->dispatcher->dispatch(ConsoleEvents::COMMAND, $event);
+ $e = null;
- if ($event->commandShouldRun()) {
- try {
- $e = null;
+ try {
+ $this->dispatcher->dispatch(ConsoleEvents::COMMAND, $event);
+
+ if ($event->commandShouldRun()) {
$exitCode = $command->run($input, $output);
- } catch (\Exception $x) {
- $e = $x;
- } catch (\Throwable $x) {
- $e = new FatalThrowableError($x);
+ } else {
+ $exitCode = ConsoleCommandEvent::RETURN_CODE_DISABLED;
}
- if (null !== $e) {
- $event = new ConsoleExceptionEvent($command, $input, $output, $e, $e->getCode());
- $this->dispatcher->dispatch(ConsoleEvents::EXCEPTION, $event);
-
- if ($e !== $event->getException()) {
- $x = $e = $event->getException();
- }
-
- $event = new ConsoleTerminateEvent($command, $input, $output, $e->getCode());
- $this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event);
+ } catch (\Exception $e) {
+ } catch (\Throwable $e) {
+ }
+ if (null !== $e) {
+ $x = $e instanceof \Exception ? $e : new FatalThrowableError($e);
+ $event = new ConsoleExceptionEvent($command, $input, $output, $x, $x->getCode());
+ $this->dispatcher->dispatch(ConsoleEvents::EXCEPTION, $event);
- throw $x;
+ if ($x !== $event->getException()) {
+ $e = $event->getException();
}
- } else {
- $exitCode = ConsoleCommandEvent::RETURN_CODE_DISABLED;
+ $exitCode = $e->getCode();
}
$event = new ConsoleTerminateEvent($command, $input, $output, $exitCode);
$this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event);
+ if (null !== $e) {
+ throw $e;
+ }
+
return $event->getExitCode();
}
$this->defaultCommand = $commandName;
}
- private function stringWidth($string)
- {
- if (false === $encoding = mb_detect_encoding($string, null, true)) {
- return strlen($string);
- }
-
- return mb_strwidth($string, $encoding);
- }
-
private function splitStringByWidth($string, $width)
{
// str_split is not suitable for multi-byte characters, we should use preg_split to get char array properly.