diff --git a/index.php b/index.php index 47cc9f9d..8ce871c2 100755 --- a/index.php +++ b/index.php @@ -16,38 +16,42 @@ use PHPDraft\Parse\JsonToHTML; define('VERSION', '0'); -$values = UI::main($argv); - -$apib = new ApibFileParser($values['file']); -$apib = $apib->parse(); - -$json = new DrafterAPI($apib); -if (!(defined('DRAFTER_ONLINE_MODE') && DRAFTER_ONLINE_MODE === 1)) +try { - try - { - $json = new Drafter($apib); - } - catch (RuntimeException $exception) + $values = UI::main($argv); + $apib = new ApibFileParser($values['file']); + $apib = $apib->parse(); + + $json = new DrafterAPI($apib); + if (!(defined('DRAFTER_ONLINE_MODE') && DRAFTER_ONLINE_MODE === 1)) { - file_put_contents('php://stderr', $exception->getMessage() . "\n"); - $options = [ - 'y' => 'Yes', - 'n' => 'No', - ]; - $answer = UI::ask('Do you want to use the online version? [y/n]', $options, 'y'); - if (!$answer) + try { - file_put_contents('php://stderr', 'Could not find a suitable drafter version'); - exit(1); + $json = new Drafter($apib); + } + catch (\PHPDraft\Parse\ResourceException $exception) + { + file_put_contents('php://stderr', $exception->getMessage() . "\n"); + $options = [ + 'y' => 'Yes', + 'n' => 'No', + ]; + $answer = UI::ask('Do you want to use the online version? [y/n]', $options, 'y'); + if (!$answer) + { + throw new \RuntimeException('Could not find a suitable drafter version', 1); + } } } -} - -$html = new JsonToHTML($json->parseToJson()); -$html->sorting = $values['sorting']; -$generator = $html->get_html($values['template'], $values['image'], $values['css'], $values['js']); + $html = new JsonToHTML($json->parseToJson()); + $html->sorting = $values['sorting']; + $generator = $html->get_html($values['template'], $values['image'], $values['css'], $values['js']); +} catch (RuntimeException $exception) +{ + file_put_contents('php://stderr', $exception->getMessage().PHP_EOL); + exit($exception->getCode()); +} function phpdraft_var_dump(...$vars) { diff --git a/src/PHPDraft/In/ApibFileParser.php b/src/PHPDraft/In/ApibFileParser.php index b699055a..6460275d 100644 --- a/src/PHPDraft/In/ApibFileParser.php +++ b/src/PHPDraft/In/ApibFileParser.php @@ -91,8 +91,7 @@ private function get_apib($filename) private function file_check($filename) { if (!file_exists($filename)) { - file_put_contents('php://stderr', "API File not found: $filename\n"); - exit(1); + throw new \RuntimeException("API File not found: $filename", 1); } } diff --git a/src/PHPDraft/In/Tests/ApibFileParserTest.php b/src/PHPDraft/In/Tests/ApibFileParserTest.php index af50148d..7efeacf5 100644 --- a/src/PHPDraft/In/Tests/ApibFileParserTest.php +++ b/src/PHPDraft/In/Tests/ApibFileParserTest.php @@ -51,6 +51,22 @@ public function testFilenameSetup() $this->assertSame(__DIR__ . '/ApibFileParserTest.php', $property->getValue($this->class)); } + /** + * Test if exception when the file doesn't exist + * @expectedException \RuntimeException + * @expectedExceptionCode 1 + * @expectedExceptionMessageRegExp "API File not found: [a-zA-Z0-9\/]*\/drafter\/non_existing_including_apib" + * + * @return void + */ + public function testFilenameSetupWrong() + { + $property = $this->reflection->getProperty('filename'); + $property->setAccessible(true); + $property->setValue($this->class, TEST_STATICS . '/drafter/non_existing_including_apib'); + $this->class->parse(); + } + /** * Test if setup is successful * @return void diff --git a/src/PHPDraft/Model/Elements/ArrayStructureElement.php b/src/PHPDraft/Model/Elements/ArrayStructureElement.php index d769392d..32020193 100644 --- a/src/PHPDraft/Model/Elements/ArrayStructureElement.php +++ b/src/PHPDraft/Model/Elements/ArrayStructureElement.php @@ -56,7 +56,7 @@ public function parse($object, &$dependencies) */ function __toString() { - $return = '