Skip to content

Commit 9ac9bc0

Browse files
committed
Rework CLI arguments
1 parent b219ece commit 9ac9bc0

12 files changed

+4266
-58
lines changed

index.php

+42-7
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,50 @@
1313
use PHPDraft\Parse\ApibToJson;
1414
use PHPDraft\Parse\JsonToHTML;
1515

16-
if($argc < 1)
16+
$options = getopt("f:t::i::h");
17+
if(!isset($argv[1]))
1718
{
18-
file_put_contents('php://stderr', "Missing file to parse\n");
19-
exit(2);
19+
file_put_contents('php://stderr', 'Not enough arguments'.PHP_EOL);
20+
help();
21+
exit(1);
2022
}
2123

22-
$apib = new ApibFileParser($argv[1]);
24+
if (boolval(preg_match('/^\-/',$argv[1])))
25+
{
26+
if (isset($options['h']))
27+
{
28+
help();
29+
exit(0);
30+
}
31+
elseif (isset($options['f']))
32+
{
33+
$file = $options['f'];
34+
}
35+
else
36+
{
37+
file_put_contents('php://stderr', 'No file to parse'.PHP_EOL);
38+
exit(1);
39+
}
40+
}
41+
else
42+
{
43+
$file = $argv[1];
44+
}
45+
46+
$template = (isset($options['t']) && $options['t']) ? $options['t']: 'default';
47+
$image = (isset($options['i']) && $options['i']) ? $options['i']: NULL;
48+
49+
$apib = new ApibFileParser($file);
2350
$json = new ApibToJson($apib);
24-
$json->parseToJson();
25-
$html = new JsonToHTML($json);
26-
$html->get_html();
51+
$html = new JsonToHTML($json->parseToJson());
52+
$html->get_html($template, $image);
53+
54+
function help()
55+
{
56+
echo 'This is a parser for API Blueprint files in PHP.'.PHP_EOL.PHP_EOL;
57+
echo "The following options can be used:.\n";
58+
echo "\t-f\tSpecifies the file to parse.\n";
59+
echo "\t-t\tSpecifies the template to use. (defaults to 'default')\n";
60+
echo "\t-h\tDisplays this text.\n";
61+
}
2762
?>

src/PHPDraft/In/ApibFileParser.php

+11
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class ApibFileParser
3030
public function __construct($filename = 'index.apib')
3131
{
3232
$this->location = pathinfo($filename, PATHINFO_DIRNAME) . '/';
33+
3334
$this->full_apib = $this->get_apib($filename);
3435
}
3536

@@ -43,6 +44,7 @@ public function __construct($filename = 'index.apib')
4344
*/
4445
function get_apib($filename)
4546
{
47+
$this->file_check($filename);
4648
$file = file_get_contents($filename);
4749
$matches = [];
4850
preg_match_all('<!-- include\(([a-z_.\/]*?).apib\) -->', $file, $matches);
@@ -53,6 +55,15 @@ function get_apib($filename)
5355
return $file;
5456
}
5557

58+
private function file_check($filename)
59+
{
60+
if(!file_exists($filename))
61+
{
62+
file_put_contents('php://stderr', "API File not found: $filename\n");
63+
exit(1);
64+
}
65+
}
66+
5667
/**
5768
* Return the value of the class
5869
*

src/PHPDraft/Model/DataStructureElement.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ function __toString()
128128
$return = '<table class="table table-striped">';
129129
foreach ($this->value as $object)
130130
{
131-
if (get_class($object) === self::class || get_class($object) === ArrayStructureElement::class)
131+
if (is_string($object) || get_class($object) === self::class || get_class($object) === ArrayStructureElement::class)
132132
{
133133
$return .= $object;
134134
}

src/PHPDraft/Out/HTML/default.css

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ h4.response.error var {
4242
padding: 6px 0;
4343
}
4444

45-
body .col-md-10 h2:first-of-type {
45+
body .col-md-10 h2:first-child,
46+
body .col-md-10 h3:first-child
47+
{
4648
margin-top: 0;
4749
}
4850

src/PHPDraft/Out/HTML/default.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,12 @@ class="pull-right <?= $this->get_method_icon($transition->get_method()); ?>"></s
101101
</div>
102102
<div class="col-md-10">
103103
<?php foreach ($base as $category): ?>
104-
<h2><a id="<?= $category->get_href(); ?>"><?= $category->title; ?></a></h2>
105-
<p><?= $category->description; ?></p>
104+
<?php if(!empty($category->title)):?>
105+
<h2><a id="<?= $category->get_href(); ?>"><?= $category->title; ?></a></h2>
106+
<?php endif;?>
107+
<?php if(!empty($category->description)):?>
108+
<p><?= $category->description; ?></p>
109+
<?php endif;?>
106110
<?php foreach ($category->children as $resource): ?>
107111
<h3>
108112
<a id="<?= $resource->get_href(); ?>"><?= $resource->title; ?></a>

src/PHPDraft/Out/TemplateGenerator.php

+33-9
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ class TemplateGenerator
2626
*/
2727
protected $template;
2828

29+
/**
30+
* The image to use as a logo
31+
* @var string
32+
*/
33+
protected $image;
34+
2935
/**
3036
* The base URl of the API
3137
* @var
@@ -42,10 +48,12 @@ class TemplateGenerator
4248
* TemplateGenerator constructor.
4349
*
4450
* @param string $template name of the template to load
51+
* @param string $image Image to use as Logo
4552
*/
46-
public function __construct($template)
53+
public function __construct($template, $image)
4754
{
4855
$this->template = $template;
56+
$this->image = $image;
4957
}
5058

5159
/**
@@ -57,20 +65,34 @@ public function __construct($template)
5765
*/
5866
public function get($object)
5967
{
68+
$include = NULL;
69+
if (stream_resolve_include_path($this->template . DIRECTORY_SEPARATOR . $this->template . '.php'))
70+
{
71+
$include = $this->template . DIRECTORY_SEPARATOR . $this->template . '.php';
72+
}
73+
74+
if (stream_resolve_include_path($this->template . '.php'))
75+
{
76+
$include = $this->template . '.php';
77+
}
78+
79+
if (stream_resolve_include_path('PHPDraft/Out/HTML/' . $this->template . '.php'))
80+
{
81+
$include = 'PHPDraft/Out/HTML/' . $this->template . '.php';
82+
}
83+
if ($include === NULL)
84+
{
85+
file_put_contents('php://stderr', "Couldn't find template '$this->template'\n");
86+
exit(1);
87+
}
88+
6089
//Prepare base data
6190
if (is_array($object->content[0]->content))
6291
{
6392
foreach ($object->content[0]->attributes->meta as $meta)
6493
{
6594
$this->base_data[$meta->content->key->content] = $meta->content->value->content;
6695
}
67-
68-
$this->base_data['TITLE'] = $object->content[0]->meta->title;
69-
}
70-
71-
//Parse specific data
72-
if (is_array($object->content[0]->content))
73-
{
7496
foreach ($object->content[0]->content as $value)
7597
{
7698
if ($value->element === 'copy')
@@ -88,9 +110,11 @@ public function get($object)
88110
$this->base_structures = $cat->structures;
89111
}
90112
}
113+
114+
$this->base_data['TITLE'] = $object->content[0]->meta->title;
91115
}
92116

93-
include_once 'PHPDraft/Out/HTML/' . $this->template . '.php';
117+
include_once $include;
94118
}
95119

96120
/**

src/PHPDraft/Parse/ApibToJson.php

+32-16
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class ApibToJson
2929
*
3030
* @var string
3131
*/
32-
protected $json;
32+
public $json;
3333

3434
/**
3535
* ApibToJson constructor.
@@ -58,35 +58,51 @@ public function parseToJson()
5858
}
5959

6060
file_put_contents($tmp_dir . '/index.apib', $this->apib);
61-
if (!$this->drafter_location()) {
61+
if (!$this->drafter_location())
62+
{
6263
file_put_contents('php://stderr', "Drafter was not installed!\n");
6364
exit(1);
6465
}
6566

66-
shell_exec($this->drafter_location(). ' ' . $tmp_dir . '/index.apib -f json -o ' . $tmp_dir . '/index.json 2> /dev/null');
67-
$this->json = file_get_contents($tmp_dir . '/index.json');
68-
return $this->apib;
67+
shell_exec($this->drafter_location() . ' ' . $tmp_dir . '/index.apib -f json -o ' . $tmp_dir . '/index.json 2> /dev/null');
68+
$this->json = json_decode(file_get_contents($tmp_dir . '/index.json'));
69+
if (json_last_error() !== JSON_ERROR_NONE)
70+
{
71+
file_put_contents('php://stderr', "Drafter generated invalid JSON!\n" . json_last_error_msg() . "\n");
72+
file_put_contents('php://stdout', file_get_contents($tmp_dir . '/index.json') . "\n");
73+
exit(2);
74+
}
75+
$warnings = FALSE;
76+
foreach ($this->json->content as $item)
77+
{
78+
if ($item->element === 'annotation')
79+
{
80+
$warnings = TRUE;
81+
$prefix = strtoupper($item->meta->classes[0]);
82+
$error = $item->content;
83+
file_put_contents('php://stdout', "$prefix: $error\n");
84+
}
85+
}
86+
if ($warnings)
87+
{
88+
file_put_contents('php://stderr', "Parsing encountered errors and stopped\n");
89+
exit(2);
90+
}
91+
92+
return $this->json;
6993
}
7094

7195
/**
7296
* Return drafter location if found
7397
*
7498
* @return bool|string
7599
*/
76-
function drafter_location() {
100+
function drafter_location()
101+
{
77102
$returnVal = shell_exec('which drafter 2> /dev/null');
78103
$returnVal = preg_replace('/^\s+|\n|\r|\s+$/m', '', $returnVal);
79-
return (empty($returnVal) ? FALSE : $returnVal);
80-
}
81104

82-
/**
83-
* JSON representation
84-
*
85-
* @return string
86-
*/
87-
function __toString()
88-
{
89-
return $this->json;
105+
return (empty($returnVal) ? FALSE : $returnVal);
90106
}
91107

92108
}

src/PHPDraft/Parse/JsonToHTML.php

+16-13
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,33 @@ class JsonToHTML
2626
*/
2727
public function __construct($json)
2828
{
29-
$this->object = json_decode($json);
29+
$this->object = $json;
3030
}
3131

3232
/**
33-
* Get the HTML representation of the JSON object
34-
*
35-
* @param string $template Type of template to display.
33+
* Gets the default template HTML
3634
*
37-
* @return string HTML template to display
35+
* @return string
3836
*/
39-
public function get_html($template = 'default')
37+
function __toString()
4038
{
41-
$gen = new TemplateGenerator($template);
42-
return $gen->get($this->object);
39+
return $this->get_html();
4340
}
4441

4542
/**
46-
* Gets the default template HTML
47-
*
48-
* @return string
43+
* Get the HTML representation of the JSON object
44+
*
45+
* @param string $template Type of template to display.
46+
*
47+
* @param string $image Image to use as a logo
48+
*
49+
* @return string HTML template to display
4950
*/
50-
function __toString()
51+
public function get_html($template = 'default', $image = NULL)
5152
{
52-
return $this->get_html();
53+
$gen = new TemplateGenerator($template, $image);
54+
55+
return $gen->get($this->object);
5356
}
5457

5558
}

src/PHPDraft/Parse/Tests/ApibToJsonTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,15 @@ public function testParseToJSON()
7575
$this->assertJsonStringEqualsJsonFile(TEST_STATICS.'/json', $this->class->__toString());
7676
}
7777

78+
/**
79+
* Check if parsing the APIB to JSON gives the expected result
80+
*/
81+
public function testParseToJSONWithErrors()
82+
{
83+
$property = $this->reflection->getProperty('apib');
84+
$property->setAccessible(TRUE);
85+
$property->setValue($this->class, file_get_contents(TEST_STATICS . '/apib_errors'));
86+
$this->class->parseToJson();
87+
$this->assertJsonStringEqualsJsonFile(TEST_STATICS.'/json_errors', $this->class->__toString());
88+
}
7889
}

src/PHPDraft/Parse/Tests/JsonToHTMLTest.php

-9
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,4 @@ public function testSetupCorrectly()
5454
$this->assertEquals(json_decode(file_get_contents(TEST_STATICS . '/json')), $property->getValue($this->class));
5555
}
5656

57-
/**
58-
* Tests if the outputted HTM is as expected.
59-
*/
60-
public function testParseToHTML()
61-
{
62-
$this->expectOutputString(file_get_contents(TEST_STATICS.'/html'));
63-
$this->class->get_html();
64-
}
65-
6657
}

0 commit comments

Comments
 (0)