Skip to content

Commit 1c92d34

Browse files
committed
Out: Add run button
1 parent dd29e16 commit 1c92d34

File tree

12 files changed

+335
-40
lines changed

12 files changed

+335
-40
lines changed

index.php

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,42 @@
1616
use PHPDraft\Parse\JsonToHTML;
1717

1818
define('VERSION', '0');
19-
$values = UI::main($argv);
20-
21-
$apib = new ApibFileParser($values['file']);
22-
$apib = $apib->parse();
23-
24-
$json = new DrafterAPI($apib);
25-
if (!(defined('DRAFTER_ONLINE_MODE') && DRAFTER_ONLINE_MODE === 1))
19+
try
2620
{
27-
try
28-
{
29-
$json = new Drafter($apib);
30-
}
31-
catch (RuntimeException $exception)
21+
$values = UI::main($argv);
22+
$apib = new ApibFileParser($values['file']);
23+
$apib = $apib->parse();
24+
25+
$json = new DrafterAPI($apib);
26+
if (!(defined('DRAFTER_ONLINE_MODE') && DRAFTER_ONLINE_MODE === 1))
3227
{
33-
file_put_contents('php://stderr', $exception->getMessage() . "\n");
34-
$options = [
35-
'y' => 'Yes',
36-
'n' => 'No',
37-
];
38-
$answer = UI::ask('Do you want to use the online version? [y/n]', $options, 'y');
39-
if (!$answer)
28+
try
4029
{
41-
file_put_contents('php://stderr', 'Could not find a suitable drafter version');
42-
exit(1);
30+
$json = new Drafter($apib);
31+
}
32+
catch (\PHPDraft\Parse\ResourceException $exception)
33+
{
34+
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+
}
4344
}
4445
}
45-
}
46-
47-
$html = new JsonToHTML($json->parseToJson());
48-
$html->sorting = $values['sorting'];
49-
$generator = $html->get_html($values['template'], $values['image'], $values['css'], $values['js']);
5046

47+
$html = new JsonToHTML($json->parseToJson());
48+
$html->sorting = $values['sorting'];
49+
$generator = $html->get_html($values['template'], $values['image'], $values['css'], $values['js']);
50+
} catch (RuntimeException $exception)
51+
{
52+
file_put_contents('php://stderr', $exception->getMessage().PHP_EOL);
53+
exit($exception->getCode());
54+
}
5155

5256
function phpdraft_var_dump(...$vars)
5357
{

src/PHPDraft/In/ApibFileParser.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ private function get_apib($filename)
9191
private function file_check($filename)
9292
{
9393
if (!file_exists($filename)) {
94-
file_put_contents('php://stderr', "API File not found: $filename\n");
95-
exit(1);
94+
throw new \RuntimeException("API File not found: $filename", 1);
9695
}
9796
}
9897

src/PHPDraft/Model/HTTPRequest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ public function get_curl_command($base_url, $additional = [])
157157
return htmlspecialchars('curl ' . join(' ', $options) . ' ' . escapeshellarg($this->parent->build_url($base_url, true)));
158158
}
159159

160-
161160
/**
162161
* Check if item is the same as other item
163162
*

src/PHPDraft/Model/HTTPResponse.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ public function __construct($parent)
6969
*/
7070
public function parse($object)
7171
{
72-
if (isset($object->attributes)) {
72+
if (isset($object->attributes->statusCode)) {
7373
$this->statuscode = intval($object->attributes->statusCode);
74+
}
75+
if (isset($object->attributes->headers)) {
7476
$this->parse_headers($object->attributes->headers);
7577
}
7678

src/PHPDraft/Model/Transition.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ function parse($object)
8787

8888
$this->href = (isset($object->attributes->href)) ? $object->attributes->href : $this->parent->href;
8989

90+
9091
if (isset($object->attributes->hrefVariables)) {
9192

9293
$deps = [];

src/PHPDraft/Out/HTML/material.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ h2.mdl-card__title-text {
2727
display: inline-block;
2828
width: 100%;
2929
}
30+
li.mdl-list__ite code{
31+
font-size: 12px;
32+
font-weight: normal;
33+
}
34+
3035
.page-content {
3136
width: 80%;
3237
margin: 0px auto;

src/PHPDraft/Out/TemplateGenerator.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ public function get($object)
8787
$include = $this->find_include_file($this->template);
8888
if ($include === NULL)
8989
{
90-
file_put_contents('php://stderr', "Couldn't find template '$this->template'\n");
91-
exit(1);
90+
throw new \RuntimeException("Couldn't find template '$this->template'", 1);
9291
}
9392

9493
//Prepare base data

src/PHPDraft/Out/UI.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static function main($argv = [])
4747
if (!isset($argv[1])) {
4848
file_put_contents('php://stderr', 'Not enough arguments' . PHP_EOL);
4949
self::help();
50-
exit(1);
50+
throw new \RuntimeException('', 1);
5151
}
5252

5353
$sorting = -1;
@@ -61,19 +61,18 @@ public static function main($argv = [])
6161
if (boolval(preg_match('/^\-/', $argv[1]))) {
6262
if (isset($options['h'])) {
6363
self::help();
64-
exit(0);
64+
throw new \RuntimeException('', 0);
6565
}
6666

6767
if (isset($options['v'])) {
6868
self::version();
69-
exit(0);
69+
throw new \RuntimeException('', 0);
7070
}
7171

7272
if (isset($options['f'])) {
7373
$file = $options['f'];
7474
} else {
75-
file_put_contents('php://stderr', 'No file to parse' . PHP_EOL);
76-
exit(1);
75+
throw new \RuntimeException('No file to parse', 1);
7776
}
7877
} else {
7978
$file = $argv[1];

src/PHPDraft/Parse/Drafter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct($apib)
2828

2929
if (!$this->location())
3030
{
31-
throw new \RuntimeException('Drafter was not installed!', 1);
31+
throw new ResourceException('Drafter was not installed!', 1);
3232
}
3333

3434
$this->drafter = $this->location();

src/PHPDraft/Parse/DrafterAPI.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct($apib)
2929
curl_exec($ch);
3030

3131
if (curl_errno($ch) !== CURLE_OK) {
32-
throw new \RuntimeException('Drafter webservice is not available!', 1);
32+
throw new ResourceException('Drafter webservice is not available!', 1);
3333
}
3434
curl_close($ch);
3535
}
@@ -46,7 +46,7 @@ protected function parse()
4646
$response = curl_exec($ch);
4747

4848
if (curl_errno($ch) !== 0) {
49-
throw new \RuntimeException('Drafter webservice failed to parse input', 1);
49+
throw new ResourceException('Drafter webservice failed to parse input', 1);
5050
}
5151

5252
$this->json = json_decode($response);

0 commit comments

Comments
 (0)