Skip to content

Commit

Permalink
Rework CLI arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
SMillerDev committed Sep 5, 2016
1 parent b219ece commit 9ac9bc0
Show file tree
Hide file tree
Showing 12 changed files with 4,266 additions and 58 deletions.
49 changes: 42 additions & 7 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,50 @@
use PHPDraft\Parse\ApibToJson;
use PHPDraft\Parse\JsonToHTML;

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

$apib = new ApibFileParser($argv[1]);
if (boolval(preg_match('/^\-/',$argv[1])))
{
if (isset($options['h']))
{
help();
exit(0);
}
elseif (isset($options['f']))
{
$file = $options['f'];
}
else
{
file_put_contents('php://stderr', 'No file to parse'.PHP_EOL);
exit(1);
}
}
else
{
$file = $argv[1];
}

$template = (isset($options['t']) && $options['t']) ? $options['t']: 'default';
$image = (isset($options['i']) && $options['i']) ? $options['i']: NULL;

$apib = new ApibFileParser($file);
$json = new ApibToJson($apib);
$json->parseToJson();
$html = new JsonToHTML($json);
$html->get_html();
$html = new JsonToHTML($json->parseToJson());
$html->get_html($template, $image);

function help()
{
echo 'This is a parser for API Blueprint files in PHP.'.PHP_EOL.PHP_EOL;
echo "The following options can be used:.\n";
echo "\t-f\tSpecifies the file to parse.\n";
echo "\t-t\tSpecifies the template to use. (defaults to 'default')\n";
echo "\t-h\tDisplays this text.\n";
}
?>
11 changes: 11 additions & 0 deletions src/PHPDraft/In/ApibFileParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class ApibFileParser
public function __construct($filename = 'index.apib')
{
$this->location = pathinfo($filename, PATHINFO_DIRNAME) . '/';

$this->full_apib = $this->get_apib($filename);
}

Expand All @@ -43,6 +44,7 @@ public function __construct($filename = 'index.apib')
*/
function get_apib($filename)
{
$this->file_check($filename);
$file = file_get_contents($filename);
$matches = [];
preg_match_all('<!-- include\(([a-z_.\/]*?).apib\) -->', $file, $matches);
Expand All @@ -53,6 +55,15 @@ function get_apib($filename)
return $file;
}

private function file_check($filename)
{
if(!file_exists($filename))
{
file_put_contents('php://stderr', "API File not found: $filename\n");
exit(1);
}
}

/**
* Return the value of the class
*
Expand Down
2 changes: 1 addition & 1 deletion src/PHPDraft/Model/DataStructureElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function __toString()
$return = '<table class="table table-striped">';
foreach ($this->value as $object)
{
if (get_class($object) === self::class || get_class($object) === ArrayStructureElement::class)
if (is_string($object) || get_class($object) === self::class || get_class($object) === ArrayStructureElement::class)
{
$return .= $object;
}
Expand Down
4 changes: 3 additions & 1 deletion src/PHPDraft/Out/HTML/default.css
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ h4.response.error var {
padding: 6px 0;
}

body .col-md-10 h2:first-of-type {
body .col-md-10 h2:first-child,
body .col-md-10 h3:first-child
{
margin-top: 0;
}

Expand Down
8 changes: 6 additions & 2 deletions src/PHPDraft/Out/HTML/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,12 @@ class="pull-right <?= $this->get_method_icon($transition->get_method()); ?>"></s
</div>
<div class="col-md-10">
<?php foreach ($base as $category): ?>
<h2><a id="<?= $category->get_href(); ?>"><?= $category->title; ?></a></h2>
<p><?= $category->description; ?></p>
<?php if(!empty($category->title)):?>
<h2><a id="<?= $category->get_href(); ?>"><?= $category->title; ?></a></h2>
<?php endif;?>
<?php if(!empty($category->description)):?>
<p><?= $category->description; ?></p>
<?php endif;?>
<?php foreach ($category->children as $resource): ?>
<h3>
<a id="<?= $resource->get_href(); ?>"><?= $resource->title; ?></a>
Expand Down
42 changes: 33 additions & 9 deletions src/PHPDraft/Out/TemplateGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ class TemplateGenerator
*/
protected $template;

/**
* The image to use as a logo
* @var string
*/
protected $image;

/**
* The base URl of the API
* @var
Expand All @@ -42,10 +48,12 @@ class TemplateGenerator
* TemplateGenerator constructor.
*
* @param string $template name of the template to load
* @param string $image Image to use as Logo
*/
public function __construct($template)
public function __construct($template, $image)
{
$this->template = $template;
$this->image = $image;
}

/**
Expand All @@ -57,20 +65,34 @@ public function __construct($template)
*/
public function get($object)
{
$include = NULL;
if (stream_resolve_include_path($this->template . DIRECTORY_SEPARATOR . $this->template . '.php'))
{
$include = $this->template . DIRECTORY_SEPARATOR . $this->template . '.php';
}

if (stream_resolve_include_path($this->template . '.php'))
{
$include = $this->template . '.php';
}

if (stream_resolve_include_path('PHPDraft/Out/HTML/' . $this->template . '.php'))
{
$include = 'PHPDraft/Out/HTML/' . $this->template . '.php';
}
if ($include === NULL)
{
file_put_contents('php://stderr', "Couldn't find template '$this->template'\n");
exit(1);
}

//Prepare base data
if (is_array($object->content[0]->content))
{
foreach ($object->content[0]->attributes->meta as $meta)
{
$this->base_data[$meta->content->key->content] = $meta->content->value->content;
}

$this->base_data['TITLE'] = $object->content[0]->meta->title;
}

//Parse specific data
if (is_array($object->content[0]->content))
{
foreach ($object->content[0]->content as $value)
{
if ($value->element === 'copy')
Expand All @@ -88,9 +110,11 @@ public function get($object)
$this->base_structures = $cat->structures;
}
}

$this->base_data['TITLE'] = $object->content[0]->meta->title;
}

include_once 'PHPDraft/Out/HTML/' . $this->template . '.php';
include_once $include;
}

/**
Expand Down
48 changes: 32 additions & 16 deletions src/PHPDraft/Parse/ApibToJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ApibToJson
*
* @var string
*/
protected $json;
public $json;

/**
* ApibToJson constructor.
Expand Down Expand Up @@ -58,35 +58,51 @@ public function parseToJson()
}

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

shell_exec($this->drafter_location(). ' ' . $tmp_dir . '/index.apib -f json -o ' . $tmp_dir . '/index.json 2> /dev/null');
$this->json = file_get_contents($tmp_dir . '/index.json');
return $this->apib;
shell_exec($this->drafter_location() . ' ' . $tmp_dir . '/index.apib -f json -o ' . $tmp_dir . '/index.json 2> /dev/null');
$this->json = json_decode(file_get_contents($tmp_dir . '/index.json'));
if (json_last_error() !== JSON_ERROR_NONE)
{
file_put_contents('php://stderr', "Drafter generated invalid JSON!\n" . json_last_error_msg() . "\n");
file_put_contents('php://stdout', file_get_contents($tmp_dir . '/index.json') . "\n");
exit(2);
}
$warnings = FALSE;
foreach ($this->json->content as $item)
{
if ($item->element === 'annotation')
{
$warnings = TRUE;
$prefix = strtoupper($item->meta->classes[0]);
$error = $item->content;
file_put_contents('php://stdout', "$prefix: $error\n");
}
}
if ($warnings)
{
file_put_contents('php://stderr', "Parsing encountered errors and stopped\n");
exit(2);
}

return $this->json;
}

/**
* Return drafter location if found
*
* @return bool|string
*/
function drafter_location() {
function drafter_location()
{
$returnVal = shell_exec('which drafter 2> /dev/null');
$returnVal = preg_replace('/^\s+|\n|\r|\s+$/m', '', $returnVal);
return (empty($returnVal) ? FALSE : $returnVal);
}

/**
* JSON representation
*
* @return string
*/
function __toString()
{
return $this->json;
return (empty($returnVal) ? FALSE : $returnVal);
}

}
29 changes: 16 additions & 13 deletions src/PHPDraft/Parse/JsonToHTML.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,33 @@ class JsonToHTML
*/
public function __construct($json)
{
$this->object = json_decode($json);
$this->object = $json;
}

/**
* Get the HTML representation of the JSON object
*
* @param string $template Type of template to display.
* Gets the default template HTML
*
* @return string HTML template to display
* @return string
*/
public function get_html($template = 'default')
function __toString()
{
$gen = new TemplateGenerator($template);
return $gen->get($this->object);
return $this->get_html();
}

/**
* Gets the default template HTML
*
* @return string
* Get the HTML representation of the JSON object
*
* @param string $template Type of template to display.
*
* @param string $image Image to use as a logo
*
* @return string HTML template to display
*/
function __toString()
public function get_html($template = 'default', $image = NULL)
{
return $this->get_html();
$gen = new TemplateGenerator($template, $image);

return $gen->get($this->object);
}

}
11 changes: 11 additions & 0 deletions src/PHPDraft/Parse/Tests/ApibToJsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,15 @@ public function testParseToJSON()
$this->assertJsonStringEqualsJsonFile(TEST_STATICS.'/json', $this->class->__toString());
}

/**
* Check if parsing the APIB to JSON gives the expected result
*/
public function testParseToJSONWithErrors()
{
$property = $this->reflection->getProperty('apib');
$property->setAccessible(TRUE);
$property->setValue($this->class, file_get_contents(TEST_STATICS . '/apib_errors'));
$this->class->parseToJson();
$this->assertJsonStringEqualsJsonFile(TEST_STATICS.'/json_errors', $this->class->__toString());
}
}
9 changes: 0 additions & 9 deletions src/PHPDraft/Parse/Tests/JsonToHTMLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,4 @@ public function testSetupCorrectly()
$this->assertEquals(json_decode(file_get_contents(TEST_STATICS . '/json')), $property->getValue($this->class));
}

/**
* Tests if the outputted HTM is as expected.
*/
public function testParseToHTML()
{
$this->expectOutputString(file_get_contents(TEST_STATICS.'/html'));
$this->class->get_html();
}

}
Loading

0 comments on commit 9ac9bc0

Please sign in to comment.