Skip to content

Commit 90c9454

Browse files
committed
Allow for online API parsing
1 parent 16f5311 commit 90c9454

File tree

6 files changed

+270
-132
lines changed

6 files changed

+270
-132
lines changed

index.php

+20-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,31 @@
1111
use PHPDraft\In\ApibFileParser;
1212
use PHPDraft\Out\UI;
1313
use PHPDraft\Parse\Drafter;
14+
use PHPDraft\Parse\DrafterAPI;
1415
use PHPDraft\Parse\JsonToHTML;
1516

1617
define('VERSION', '0');
1718
$values = UI::main($argv);
1819

19-
$apib = new ApibFileParser($values['file']);
20-
$json = new Drafter($apib);
20+
$apib = new ApibFileParser($values['file']);
21+
$json = new DrafterAPI($apib);
22+
if (!(defined('DRAFTER_ONLINE_MODE') && DRAFTER_ONLINE_MODE === 1)) {
23+
try {
24+
$json = new Drafter($apib);
25+
} catch (RuntimeException $exception) {
26+
file_put_contents('php://stderr', $exception->getMessage()."\n");
27+
$options = [
28+
'y' => 'Yes',
29+
'n' => 'No',
30+
];
31+
$answer = UI::ask('Do you want to use the online version? [y/n]', $options, 'y');
32+
if (!$answer) {
33+
file_put_contents('php://stderr', 'Could not find a suitable drafter version');
34+
exit(1);
35+
}
36+
}
37+
}
38+
2139
$html = new JsonToHTML($json->parseToJson());
2240
$html->sorting = $values['sorting'];
2341
$html->get_html($values['template'], $values['image'], $values['css'], $values['js']);

src/PHPDraft/Model/Elements/Tests/ArrayStructureTest.php

-38
This file was deleted.

src/PHPDraft/Out/UI.php

+57-36
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
*/
1414
class UI
1515
{
16-
public static $PHPD_SORT_ALL = 3;
16+
public static $PHPD_SORT_ALL = 3;
1717
public static $PHPD_SORT_WEBSERVICES = 2;
18-
public static $PHPD_SORT_STRUCTURES = 1;
18+
public static $PHPD_SORT_STRUCTURES = 1;
1919
protected $versionStringPrinted;
2020

2121
/**
@@ -27,41 +27,34 @@ class UI
2727
*/
2828
static function main($argv = [])
2929
{
30-
$options = getopt('f:t:i:c:j:s:hvu');
30+
$options = getopt('f:t:i:c:j:s:hvuyo');
3131

32-
if (!isset($argv[1]))
33-
{
32+
if (!isset($argv[1])) {
3433
file_put_contents('php://stderr', 'Not enough arguments' . PHP_EOL);
3534
self::help();
3635
exit(1);
3736
}
3837

3938
$sorting = -1;
40-
if (isset($options['s']))
41-
{
39+
if (isset($options['s'])) {
4240
$value = strtoupper($options['s']);
43-
if (isset(UI::${'PHPD_SORT_' . $value}))
44-
{
41+
if (isset(UI::${'PHPD_SORT_' . $value})) {
4542
$sorting = UI::${'PHPD_SORT_' . $value};
4643
}
4744
}
4845

49-
if (boolval(preg_match('/^\-/', $argv[1])))
50-
{
51-
if (isset($options['h']))
52-
{
46+
if (boolval(preg_match('/^\-/', $argv[1]))) {
47+
if (isset($options['h'])) {
5348
self::help();
5449
exit(0);
5550
}
5651

57-
if (isset($options['v']))
58-
{
52+
if (isset($options['v'])) {
5953
self::version();
6054
exit(0);
6155
}
6256

63-
if (isset($options['f']))
64-
{
57+
if (isset($options['f'])) {
6558
$file = $options['f'];
6659
} else {
6760
file_put_contents('php://stderr', 'No file to parse' . PHP_EOL);
@@ -70,11 +63,14 @@ static function main($argv = [])
7063
} else {
7164
$file = $argv[1];
7265
}
66+
if (isset($options['y']) || isset($options['o'])) {
67+
define('DRAFTER_ONLINE_MODE', 1);
68+
}
7369

7470
$template = (isset($options['t']) && $options['t']) ? $options['t'] : 'default';
75-
$image = (isset($options['i']) && $options['i']) ? $options['i'] : NULL;
76-
$css = (isset($options['c']) && $options['c']) ? $options['i'] : NULL;
77-
$js = (isset($options['j']) && $options['j']) ? $options['i'] : NULL;
71+
$image = (isset($options['i']) && $options['i']) ? $options['i'] : null;
72+
$css = (isset($options['c']) && $options['c']) ? $options['i'] : null;
73+
$js = (isset($options['j']) && $options['j']) ? $options['i'] : null;
7874

7975
return [
8076
'file' => $file,
@@ -125,16 +121,6 @@ static function release_id()
125121
return (VERSION === '0') ? @exec('git describe --tags 2>&1') : VERSION;
126122
}
127123

128-
/**
129-
* Print the version string
130-
*
131-
* @return void
132-
*/
133-
private function printVersionString()
134-
{
135-
print self::version() . "\n\n";
136-
}
137-
138124
/**
139125
* Print the series of the update
140126
*
@@ -144,8 +130,7 @@ private function printVersionString()
144130
*/
145131
public static function series()
146132
{
147-
if (strpos(self::release_id(), '-'))
148-
{
133+
if (strpos(self::release_id(), '-')) {
149134
$version = explode('-', self::release_id())[0];
150135
} else {
151136
$version = self::release_id();
@@ -161,14 +146,41 @@ public static function series()
161146
*/
162147
public static function getReleaseChannel()
163148
{
164-
if (strpos(self::release_id(), '-') !== FALSE)
165-
{
149+
if (strpos(self::release_id(), '-') !== false) {
166150
return '-nightly';
167151
}
168152

169153
return '';
170154
}
171155

156+
/**
157+
* Ask a question to the user
158+
*
159+
* @param string $message The question
160+
* @param array $options Possible answers
161+
*
162+
* @param string $positive The parameter that gives a positive outcome
163+
*
164+
* @return boolean
165+
*/
166+
public static function ask($message, $options, $positive = 'y')
167+
{
168+
file_put_contents('php://stdout', $message);
169+
do {
170+
$selection = fgetc(STDIN);
171+
} while (trim($selection) == '');
172+
173+
if (array_key_exists(strtolower($selection), $options)) {
174+
return ($selection === $positive);
175+
}
176+
if (array_search($selection, $options)) {
177+
return (array_search($selection, $options) === $positive);
178+
}
179+
file_put_contents('php://stderr', 'That answer wasn\'t expected, try again.'.PHP_EOL.PHP_EOL);
180+
181+
return UI::ask($message, $options, $positive);
182+
}
183+
172184
/**
173185
* Handle the check for a version
174186
*
@@ -181,8 +193,7 @@ protected function handleVersionCheck()
181193
$this->printVersionString();
182194
$latestVersion = file_get_contents('https://phar.phpdraft.de/latest-version-of/phpdraft');
183195
$isOutdated = version_compare($latestVersion, self::release_id(), '>');
184-
if ($isOutdated)
185-
{
196+
if ($isOutdated) {
186197
print "You are not using the latest version of PHPDraft.\n";
187198
print 'Use "phpdraft --self-upgrade" to install PHPDraft ' . $latestVersion . "\n";
188199
} else {
@@ -191,4 +202,14 @@ protected function handleVersionCheck()
191202

192203
exit(0);
193204
}
205+
206+
/**
207+
* Print the version string
208+
*
209+
* @return void
210+
*/
211+
private function printVersionString()
212+
{
213+
print self::version() . "\n\n";
214+
}
194215
}

src/PHPDraft/Parse/BaseParser.php

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
/**
3+
* This file contains the BaseParser.php
4+
*
5+
* @package php-drafter\SOMETHING
6+
* @author Sean Molenaar<[email protected]>
7+
*/
8+
9+
//TODO: Change this
10+
11+
namespace PHPDraft\Parse;
12+
13+
14+
abstract class BaseParser
15+
{
16+
/**
17+
* The API Blueprint output (JSON)
18+
*
19+
* @var string
20+
*/
21+
public $json;
22+
23+
/**
24+
* Temp directory
25+
*
26+
* @var array
27+
*/
28+
protected $tmp_dir;
29+
30+
/**
31+
* The API Blueprint input
32+
*
33+
* @var string
34+
*/
35+
protected $apib;
36+
37+
/**
38+
* BaseParser constructor.
39+
*
40+
* @param string $apib API Blueprint text
41+
*/
42+
public function __construct($apib)
43+
{
44+
$this->apib = $apib;
45+
}
46+
47+
/**
48+
* BaseParser destructor.
49+
*/
50+
function __destruct()
51+
{
52+
unset($this->apib);
53+
unset($this->json);
54+
unset($this->tmp_dir);
55+
}
56+
57+
58+
/**
59+
* Parse the API Blueprint text to JSON
60+
*
61+
* @return string API Blueprint text
62+
*/
63+
public function parseToJson()
64+
{
65+
if (!file_exists($this->tmp_dir)) {
66+
mkdir($this->tmp_dir);
67+
}
68+
69+
file_put_contents($this->tmp_dir . '/index.apib', $this->apib);
70+
71+
$this->parse();
72+
73+
if (json_last_error() !== JSON_ERROR_NONE)
74+
{
75+
file_put_contents('php://stdout', 'ERROR: invalid json in ' . $this->tmp_dir . '/index.json');
76+
throw new \RuntimeException('Drafter generated invalid JSON (' . json_last_error_msg() . ')', 2);
77+
}
78+
79+
$warnings = false;
80+
foreach ($this->json->content as $item) {
81+
if ($item->element === 'annotation') {
82+
$warnings = true;
83+
$prefix = strtoupper($item->meta->classes[0]);
84+
$error = $item->content;
85+
file_put_contents('php://stdout', "<pre>$prefix: $error</pre>\n");
86+
}
87+
}
88+
89+
if ($warnings) {
90+
throw new \RuntimeException('Parsing encountered errors and stopped', 2);
91+
}
92+
93+
return $this->json;
94+
}
95+
96+
/**
97+
* Parses the apib for the selected method
98+
*
99+
* @return void
100+
*/
101+
abstract protected function parse();
102+
}

0 commit comments

Comments
 (0)