diff --git a/phpdraft b/phpdraft index 8ce871c2..3a6f487b 100755 --- a/phpdraft +++ b/phpdraft @@ -9,6 +9,7 @@ set_include_path(get_include_path() . ":" . __DIR__ . '/src/'); */ //require_once 'PHPDraft/Core/Autoloader.php'; require_once 'vendor/autoload.php'; + use PHPDraft\In\ApibFileParser; use PHPDraft\Out\UI; use PHPDraft\Parse\Drafter; @@ -18,41 +19,66 @@ use PHPDraft\Parse\JsonToHTML; define('VERSION', '0'); try { - $values = UI::main($argv); - $apib = new ApibFileParser($values['file']); - $apib = $apib->parse(); + $values = UI::main($argv); + $apib = new ApibFileParser($values['file']); + $apib = $apib->parse(); + $offline = FALSE; + $online = FALSE; - $json = new DrafterAPI($apib); - if (!(defined('DRAFTER_ONLINE_MODE') && DRAFTER_ONLINE_MODE === 1)) + try + { + $json = new Drafter($apib); + $offline = TRUE; + } + catch (\PHPDraft\Parse\ResourceException $exception) + { + file_put_contents('php://stderr', "Offline drafter not available.\n"); + if (!defined('DRAFTER_ONLINE_MODE') || DRAFTER_ONLINE_MODE !== 1) + { + ask_version(); + } + } + if (!$offline) { try { - $json = new Drafter($apib); + $json = new DrafterAPI($apib); + $online = TRUE; } 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); - } } } + if (!$online && !$offline) + { + 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']); -} catch (RuntimeException $exception) +} +catch (RuntimeException $exception) { - file_put_contents('php://stderr', $exception->getMessage().PHP_EOL); + file_put_contents('php://stderr', $exception->getMessage() . PHP_EOL); exit($exception->getCode()); } +function ask_version() +{ + $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); + } +} + function phpdraft_var_dump(...$vars) { if (defined('__PHPDRAFT_PHAR__')) diff --git a/src/PHPDraft/Out/UI.php b/src/PHPDraft/Out/UI.php index 75f4b8d5..d622d265 100644 --- a/src/PHPDraft/Out/UI.php +++ b/src/PHPDraft/Out/UI.php @@ -44,7 +44,7 @@ class UI */ public static function main($argv = []) { - $options = getopt('f:t:i:c:j:s:hvuyo'); + $options = getopt('f:t:i:c:j:s:hvyo'); if (!isset($argv[1])) { file_put_contents('php://stderr', 'Not enough arguments' . PHP_EOL); @@ -116,11 +116,14 @@ public function help() echo 'This is a parser for API Blueprint files in PHP.' . PHP_EOL . PHP_EOL; echo 'The following options can be used:.' . PHP_EOL; echo "\t-f\tSpecifies the file to parse." . PHP_EOL; + echo "\t-y\tAlways accept using the online mode." . PHP_EOL; + echo "\t-o\tAlways use the online mode." . PHP_EOL; echo "\t-t\tSpecifies the template to use. (defaults to 'default')" . PHP_EOL; echo "\t-s\tSort displayed values [All|None|Structures|Webservices] (defaults to the way the objects are in the file)" . PHP_EOL; echo "\t-i\tSpecifies an image to display in the header." . PHP_EOL; echo "\t-c\tSpecifies a CSS file to include (value is put in a link element without checking)." . PHP_EOL; echo "\t-j\tSpecifies a JS file to include (value is put in a script element without checking)." . PHP_EOL; + echo "\t-v\tPrint the version for PHPDraft." . PHP_EOL; echo "\t-h\tDisplays this text." . PHP_EOL; } diff --git a/tests/statics/drafter/help b/tests/statics/drafter/help index e9d62fc0..7f5dd174 100644 --- a/tests/statics/drafter/help +++ b/tests/statics/drafter/help @@ -2,9 +2,12 @@ This is a parser for API Blueprint files in PHP. The following options can be used:. -f Specifies the file to parse. + -y Always accept using the online mode. + -o Always use the online mode. -t Specifies the template to use. (defaults to 'default') -s Sort displayed values [All|None|Structures|Webservices] (defaults to the way the objects are in the file) -i Specifies an image to display in the header. -c Specifies a CSS file to include (value is put in a link element without checking). -j Specifies a JS file to include (value is put in a script element without checking). + -v Print the version for PHPDraft. -h Displays this text.