Skip to content

Commit

Permalink
Merge ebe1b21 into 51fe4b6
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthimatiker committed Jun 21, 2014
2 parents 51fe4b6 + ebe1b21 commit 70288b2
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/Athletic/Common/DICBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,15 @@ private function setupFormatter()
$formatter = $cmdLine->getFormatter();

if (isset($formatter) === true) {
$this->athletic['formatterClass'] = "\\Athletic\\Formatters\\$formatter";
if (!class_exists($formatter)) {
// Use a built-in formatter.
$formatter = "\\Athletic\\Formatters\\$formatter";
}
$this->athletic['formatterClass'] = $formatter;
} else {
$this->athletic['formatterClass'] = '\Athletic\Formatters\DefaultFormatter';
}
$this->assertValidFormatterClass($this->athletic['formatterClass']);

$this->athletic['formatter'] = function ($dic) {
return new $dic['formatterClass']();
Expand Down Expand Up @@ -197,4 +202,24 @@ private function setupErrorHandler()
return new $dic['errorHandlerClass']($dic['command'], $dic['errorExceptionFactory']);
};
}

/**
* Asserts that the provided class can be used as output formatter.
*
* @param string $formatterClass
* @throws \InvalidArgumentException If the class doe not meet the requirements (interface and empty constructor)
*/
private function assertValidFormatterClass($formatterClass)
{
$formatterInfo = new \ReflectionClass($formatterClass);
if (!$formatterInfo->implementsInterface('Athletic\Formatters\FormatterInterface')) {
$message = sprintf('%s does not implement the formatter interface.', $formatterClass);
throw new \InvalidArgumentException($message);
}
$constructor = $formatterInfo->getConstructor();
if ($constructor !== null && $constructor->getNumberOfRequiredParameters() > 0) {
$message = sprintf('Formatter %s must provide a constructor without required parameters.', $formatterClass);
throw new \InvalidArgumentException($message);
}
}
}

0 comments on commit 70288b2

Please sign in to comment.