Skip to content

Commit

Permalink
Allow clients to specify a display-error function.
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-1-anderson committed Sep 13, 2016
1 parent e8b6e18 commit c2dc246
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
39 changes: 33 additions & 6 deletions src/CommandProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class CommandProcessor
protected $hookManager;
/** var FormatterManager */
protected $formatterManager;
/** var callable */
protected $displayErrorFunction;

public function __construct(HookManager $hookManager)
{
Expand All @@ -42,6 +44,11 @@ public function setFormatterManager(FormatterManager $formatterManager)
$this->formatterManager = $formatterManager;
}

public function setDisplayErrorFunction(callable $fn)
{
$this->displayErrorFunction = $fn;
}

/**
* Return the formatter manager
* @return FormatterManager
Expand Down Expand Up @@ -70,7 +77,7 @@ public function process(
);
return $this->handleResults($output, $names, $result, $annotationData, $options);
} catch (\Exception $e) {
$result = new CommandError($e->getCode(), $e->getMessage());
$result = new CommandError($e->getMessage(), $e->getCode());
return $this->handleResults($output, $names, $result, $annotationData, $options);
}
}
Expand Down Expand Up @@ -116,12 +123,13 @@ public function handleResults(OutputInterface $output, $names, $result, Annotati
// Get the structured output, the output stream and the formatter
$structuredOutput = $this->hookManager()->extractOutput($names, $result);
$output = $this->chooseOutputStream($output, $status);
if (($status == 0) && isset($this->formatterManager)) {
$this->writeUsingFormatter($output, $structuredOutput, $annotationData, $options);
} else {
$this->writeCommandOutput($output, $structuredOutput);
if ($status != 0) {
return $this->writeErrorMessage($output, $status, $structuredOutput, $result);
}
return $status;
if (isset($this->formatterManager)) {
return $this->writeUsingFormatter($output, $structuredOutput, $annotationData, $options);
}
return $this->writeCommandOutput($output, $structuredOutput);
}

/**
Expand Down Expand Up @@ -198,6 +206,24 @@ protected function writeUsingFormatter(OutputInterface $output, $structuredOutpu
$structuredOutput,
$formatterOptions
);
return 0;
}

/**
* Description
* @param type $output
* @param type $status
* @param type $structuredOutput
* @return type
*/
protected function writeErrorMessage($output, $status, $structuredOutput, $originalResult)
{
if (isset($this->displayErrorFunction)) {
call_user_func($this->displayErrorFunction, $output, $structuredOutput, $status, $originalResult);
} else {
$this->writeCommandOutput($output, $structuredOutput);
}
return $status;
}

/**
Expand All @@ -212,6 +238,7 @@ protected function writeCommandOutput(
if (is_string($structuredOutput)) {
$output->writeln($structuredOutput);
}
return 0;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/src/ExampleCommandFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ public function testHello($who)
return "Hello, $who.";
}

public function testException($what)
{
throw new \Exception($what);
}

/**
* @hook validate test:hello
*/
Expand All @@ -199,6 +204,9 @@ public function validateTestHello($args)
if ($args['who'] == 'Donald Duck') {
return new CommandError("I won't say hello to Donald Duck.");
}
if ($args['who'] == 'Drumph') {
throw new \Exception('Irrational value error.');
}
}

/**
Expand Down
13 changes: 13 additions & 0 deletions tests/testAnnotatedCommandFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,19 @@ function testValidate()

$input = new StringInput('test:hello "Donald Duck"');
$this->assertRunCommandViaApplicationEquals($command, $input, "I won't say hello to Donald Duck.", 1);

$input = new StringInput('test:hello "Drumph"');
$this->assertRunCommandViaApplicationEquals($command, $input, "Irrational value error.", 1);

// Try the last test again with a display error function installed.
$commandFactory->commandProcessor()->setDisplayErrorFunction(
function ($output, $message) {
$output->writeln("*** $message ****");
}
);

$input = new StringInput('test:hello "Drumph"');
$this->assertRunCommandViaApplicationEquals($command, $input, "*** Irrational value error. ****", 1);
}

function assertRunCommandViaApplicationEquals($command, $input, $expectedOutput, $expectedStatusCode = 0)
Expand Down

0 comments on commit c2dc246

Please sign in to comment.