Skip to content

Commit

Permalink
Allow for online API parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
SMillerDev committed Jan 17, 2017
1 parent 16f5311 commit 90c9454
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 132 deletions.
22 changes: 20 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,31 @@
use PHPDraft\In\ApibFileParser;
use PHPDraft\Out\UI;
use PHPDraft\Parse\Drafter;
use PHPDraft\Parse\DrafterAPI;
use PHPDraft\Parse\JsonToHTML;

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

$apib = new ApibFileParser($values['file']);
$json = new Drafter($apib);
$apib = new ApibFileParser($values['file']);
$json = new DrafterAPI($apib);
if (!(defined('DRAFTER_ONLINE_MODE') && DRAFTER_ONLINE_MODE === 1)) {
try {
$json = new Drafter($apib);
} catch (RuntimeException $exception) {
file_put_contents('php://stderr', $exception->getMessage()."\n");
$options = [
'y' => 'Yes',
'n' => 'No',
];
$answer = UI::ask('Do you want to use the online version? [y/n]', $options, 'y');
if (!$answer) {
file_put_contents('php://stderr', 'Could not find a suitable drafter version');
exit(1);
}
}
}

$html = new JsonToHTML($json->parseToJson());
$html->sorting = $values['sorting'];
$html->get_html($values['template'], $values['image'], $values['css'], $values['js']);
Expand Down
38 changes: 0 additions & 38 deletions src/PHPDraft/Model/Elements/Tests/ArrayStructureTest.php

This file was deleted.

93 changes: 57 additions & 36 deletions src/PHPDraft/Out/UI.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
*/
class UI
{
public static $PHPD_SORT_ALL = 3;
public static $PHPD_SORT_ALL = 3;
public static $PHPD_SORT_WEBSERVICES = 2;
public static $PHPD_SORT_STRUCTURES = 1;
public static $PHPD_SORT_STRUCTURES = 1;
protected $versionStringPrinted;

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

if (!isset($argv[1]))
{
if (!isset($argv[1])) {
file_put_contents('php://stderr', 'Not enough arguments' . PHP_EOL);
self::help();
exit(1);
}

$sorting = -1;
if (isset($options['s']))
{
if (isset($options['s'])) {
$value = strtoupper($options['s']);
if (isset(UI::${'PHPD_SORT_' . $value}))
{
if (isset(UI::${'PHPD_SORT_' . $value})) {
$sorting = UI::${'PHPD_SORT_' . $value};
}
}

if (boolval(preg_match('/^\-/', $argv[1])))
{
if (isset($options['h']))
{
if (boolval(preg_match('/^\-/', $argv[1]))) {
if (isset($options['h'])) {
self::help();
exit(0);
}

if (isset($options['v']))
{
if (isset($options['v'])) {
self::version();
exit(0);
}

if (isset($options['f']))
{
if (isset($options['f'])) {
$file = $options['f'];
} else {
file_put_contents('php://stderr', 'No file to parse' . PHP_EOL);
Expand All @@ -70,11 +63,14 @@ static function main($argv = [])
} else {
$file = $argv[1];
}
if (isset($options['y']) || isset($options['o'])) {
define('DRAFTER_ONLINE_MODE', 1);
}

$template = (isset($options['t']) && $options['t']) ? $options['t'] : 'default';
$image = (isset($options['i']) && $options['i']) ? $options['i'] : NULL;
$css = (isset($options['c']) && $options['c']) ? $options['i'] : NULL;
$js = (isset($options['j']) && $options['j']) ? $options['i'] : NULL;
$image = (isset($options['i']) && $options['i']) ? $options['i'] : null;
$css = (isset($options['c']) && $options['c']) ? $options['i'] : null;
$js = (isset($options['j']) && $options['j']) ? $options['i'] : null;

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

/**
* Print the version string
*
* @return void
*/
private function printVersionString()
{
print self::version() . "\n\n";
}

/**
* Print the series of the update
*
Expand All @@ -144,8 +130,7 @@ private function printVersionString()
*/
public static function series()
{
if (strpos(self::release_id(), '-'))
{
if (strpos(self::release_id(), '-')) {
$version = explode('-', self::release_id())[0];
} else {
$version = self::release_id();
Expand All @@ -161,14 +146,41 @@ public static function series()
*/
public static function getReleaseChannel()
{
if (strpos(self::release_id(), '-') !== FALSE)
{
if (strpos(self::release_id(), '-') !== false) {
return '-nightly';
}

return '';
}

/**
* Ask a question to the user
*
* @param string $message The question
* @param array $options Possible answers
*
* @param string $positive The parameter that gives a positive outcome
*
* @return boolean
*/
public static function ask($message, $options, $positive = 'y')
{
file_put_contents('php://stdout', $message);
do {
$selection = fgetc(STDIN);
} while (trim($selection) == '');

if (array_key_exists(strtolower($selection), $options)) {
return ($selection === $positive);
}
if (array_search($selection, $options)) {
return (array_search($selection, $options) === $positive);
}
file_put_contents('php://stderr', 'That answer wasn\'t expected, try again.'.PHP_EOL.PHP_EOL);

return UI::ask($message, $options, $positive);
}

/**
* Handle the check for a version
*
Expand All @@ -181,8 +193,7 @@ protected function handleVersionCheck()
$this->printVersionString();
$latestVersion = file_get_contents('https://phar.phpdraft.de/latest-version-of/phpdraft');
$isOutdated = version_compare($latestVersion, self::release_id(), '>');
if ($isOutdated)
{
if ($isOutdated) {
print "You are not using the latest version of PHPDraft.\n";
print 'Use "phpdraft --self-upgrade" to install PHPDraft ' . $latestVersion . "\n";
} else {
Expand All @@ -191,4 +202,14 @@ protected function handleVersionCheck()

exit(0);
}

/**
* Print the version string
*
* @return void
*/
private function printVersionString()
{
print self::version() . "\n\n";
}
}
102 changes: 102 additions & 0 deletions src/PHPDraft/Parse/BaseParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/**
* This file contains the BaseParser.php
*
* @package php-drafter\SOMETHING
* @author Sean Molenaar<[email protected]>
*/

//TODO: Change this

namespace PHPDraft\Parse;


abstract class BaseParser
{
/**
* The API Blueprint output (JSON)
*
* @var string
*/
public $json;

/**
* Temp directory
*
* @var array
*/
protected $tmp_dir;

/**
* The API Blueprint input
*
* @var string
*/
protected $apib;

/**
* BaseParser constructor.
*
* @param string $apib API Blueprint text
*/
public function __construct($apib)
{
$this->apib = $apib;
}

/**
* BaseParser destructor.
*/
function __destruct()
{
unset($this->apib);
unset($this->json);
unset($this->tmp_dir);
}


/**
* Parse the API Blueprint text to JSON
*
* @return string API Blueprint text
*/
public function parseToJson()
{
if (!file_exists($this->tmp_dir)) {
mkdir($this->tmp_dir);
}

file_put_contents($this->tmp_dir . '/index.apib', $this->apib);

$this->parse();

if (json_last_error() !== JSON_ERROR_NONE)
{
file_put_contents('php://stdout', 'ERROR: invalid json in ' . $this->tmp_dir . '/index.json');
throw new \RuntimeException('Drafter generated invalid JSON (' . json_last_error_msg() . ')', 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', "<pre>$prefix: $error</pre>\n");
}
}

if ($warnings) {
throw new \RuntimeException('Parsing encountered errors and stopped', 2);
}

return $this->json;
}

/**
* Parses the apib for the selected method
*
* @return void
*/
abstract protected function parse();
}
Loading

0 comments on commit 90c9454

Please sign in to comment.