Skip to content

Commit

Permalink
General: Fix internet requirement
Browse files Browse the repository at this point in the history
Issue #45#closed
  • Loading branch information
SMillerDev committed Oct 16, 2017
1 parent faf970e commit ae804c0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
60 changes: 43 additions & 17 deletions phpdraft
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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__'))
Expand Down
5 changes: 4 additions & 1 deletion src/PHPDraft/Out/UI.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down
3 changes: 3 additions & 0 deletions tests/statics/drafter/help
Original file line number Diff line number Diff line change
Expand Up @@ -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.

0 comments on commit ae804c0

Please sign in to comment.