Skip to content

Commit ae804c0

Browse files
committed
General: Fix internet requirement
Issue #45#closed
1 parent faf970e commit ae804c0

File tree

3 files changed

+50
-18
lines changed

3 files changed

+50
-18
lines changed

phpdraft

+43-17
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ set_include_path(get_include_path() . ":" . __DIR__ . '/src/');
99
*/
1010
//require_once 'PHPDraft/Core/Autoloader.php';
1111
require_once 'vendor/autoload.php';
12+
1213
use PHPDraft\In\ApibFileParser;
1314
use PHPDraft\Out\UI;
1415
use PHPDraft\Parse\Drafter;
@@ -18,41 +19,66 @@ use PHPDraft\Parse\JsonToHTML;
1819
define('VERSION', '0');
1920
try
2021
{
21-
$values = UI::main($argv);
22-
$apib = new ApibFileParser($values['file']);
23-
$apib = $apib->parse();
22+
$values = UI::main($argv);
23+
$apib = new ApibFileParser($values['file']);
24+
$apib = $apib->parse();
25+
$offline = FALSE;
26+
$online = FALSE;
2427

25-
$json = new DrafterAPI($apib);
26-
if (!(defined('DRAFTER_ONLINE_MODE') && DRAFTER_ONLINE_MODE === 1))
28+
try
29+
{
30+
$json = new Drafter($apib);
31+
$offline = TRUE;
32+
}
33+
catch (\PHPDraft\Parse\ResourceException $exception)
34+
{
35+
file_put_contents('php://stderr', "Offline drafter not available.\n");
36+
if (!defined('DRAFTER_ONLINE_MODE') || DRAFTER_ONLINE_MODE !== 1)
37+
{
38+
ask_version();
39+
}
40+
}
41+
if (!$offline)
2742
{
2843
try
2944
{
30-
$json = new Drafter($apib);
45+
$json = new DrafterAPI($apib);
46+
$online = TRUE;
3147
}
3248
catch (\PHPDraft\Parse\ResourceException $exception)
3349
{
3450
file_put_contents('php://stderr', $exception->getMessage() . "\n");
35-
$options = [
36-
'y' => 'Yes',
37-
'n' => 'No',
38-
];
39-
$answer = UI::ask('Do you want to use the online version? [y/n]', $options, 'y');
40-
if (!$answer)
41-
{
42-
throw new \RuntimeException('Could not find a suitable drafter version', 1);
43-
}
4451
}
4552
}
53+
if (!$online && !$offline)
54+
{
55+
throw new \RuntimeException('Could not find a suitable drafter version', 1);
56+
}
57+
4658

4759
$html = new JsonToHTML($json->parseToJson());
4860
$html->sorting = $values['sorting'];
4961
$generator = $html->get_html($values['template'], $values['image'], $values['css'], $values['js']);
50-
} catch (RuntimeException $exception)
62+
}
63+
catch (RuntimeException $exception)
5164
{
52-
file_put_contents('php://stderr', $exception->getMessage().PHP_EOL);
65+
file_put_contents('php://stderr', $exception->getMessage() . PHP_EOL);
5366
exit($exception->getCode());
5467
}
5568

69+
function ask_version()
70+
{
71+
$options = [
72+
'y' => 'Yes',
73+
'n' => 'No',
74+
];
75+
$answer = UI::ask('Do you want to use the online version? [y/n]', $options, 'y');
76+
if (!$answer)
77+
{
78+
throw new \RuntimeException('Could not find a suitable drafter version', 1);
79+
}
80+
}
81+
5682
function phpdraft_var_dump(...$vars)
5783
{
5884
if (defined('__PHPDRAFT_PHAR__'))

src/PHPDraft/Out/UI.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class UI
4444
*/
4545
public static function main($argv = [])
4646
{
47-
$options = getopt('f:t:i:c:j:s:hvuyo');
47+
$options = getopt('f:t:i:c:j:s:hvyo');
4848

4949
if (!isset($argv[1])) {
5050
file_put_contents('php://stderr', 'Not enough arguments' . PHP_EOL);
@@ -116,11 +116,14 @@ public function help()
116116
echo 'This is a parser for API Blueprint files in PHP.' . PHP_EOL . PHP_EOL;
117117
echo 'The following options can be used:.' . PHP_EOL;
118118
echo "\t-f\tSpecifies the file to parse." . PHP_EOL;
119+
echo "\t-y\tAlways accept using the online mode." . PHP_EOL;
120+
echo "\t-o\tAlways use the online mode." . PHP_EOL;
119121
echo "\t-t\tSpecifies the template to use. (defaults to 'default')" . PHP_EOL;
120122
echo "\t-s\tSort displayed values [All|None|Structures|Webservices] (defaults to the way the objects are in the file)" . PHP_EOL;
121123
echo "\t-i\tSpecifies an image to display in the header." . PHP_EOL;
122124
echo "\t-c\tSpecifies a CSS file to include (value is put in a link element without checking)." . PHP_EOL;
123125
echo "\t-j\tSpecifies a JS file to include (value is put in a script element without checking)." . PHP_EOL;
126+
echo "\t-v\tPrint the version for PHPDraft." . PHP_EOL;
124127
echo "\t-h\tDisplays this text." . PHP_EOL;
125128
}
126129

tests/statics/drafter/help

+3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ This is a parser for API Blueprint files in PHP.
22

33
The following options can be used:.
44
-f Specifies the file to parse.
5+
-y Always accept using the online mode.
6+
-o Always use the online mode.
57
-t Specifies the template to use. (defaults to 'default')
68
-s Sort displayed values [All|None|Structures|Webservices] (defaults to the way the objects are in the file)
79
-i Specifies an image to display in the header.
810
-c Specifies a CSS file to include (value is put in a link element without checking).
911
-j Specifies a JS file to include (value is put in a script element without checking).
12+
-v Print the version for PHPDraft.
1013
-h Displays this text.

0 commit comments

Comments
 (0)