Skip to content

Commit 6a8d12e

Browse files
committed
Fix menu UI and rewrite CLI usage
Fixes #67 #65 #64 #63
1 parent bcb3a4b commit 6a8d12e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1201
-1840
lines changed

.travis.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@ matrix:
1414
before_script:
1515
- travis_retry composer self-update
1616
- travis_retry composer install --no-interaction --prefer-source --dev
17-
- if [[ ${TRAVIS_PHP_VERSION:0:3} == "5.6" ]]; then pecl install runkit; else pecl install uopz; fi
18-
- if [[ ${TRAVIS_PHP_VERSION:0:3} == "5.6" ]]; then echo "runkit.internal_override=1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi
17+
- pecl install uopz
1918
- export PATH=$PATH:$PWD
2019
script: vendor/bin/phpunit --configuration tests/phpunit.xml --coverage-clover=coverage.xml
2120

2221
after_success:
2322
- bash <(curl -s https://codecov.io/bash)
2423

2524
before_deploy:
26-
- ant phar
25+
- ./vendor/bin/phing phar
2726
- sha256sum build/out/*
2827
deploy:
2928
provider: releases
@@ -35,4 +34,4 @@ deploy:
3534
on:
3635
tags: true
3736
after_deploy:
38-
- ant clean
37+
- ./vendor/bin/phing clean

build.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@
7878
<include name="**/*.php"/>
7979
</fileset>
8080
</copy>
81+
<copy todir="${basedir}/build/phar/phpdraft/src/Garden/Cli">
82+
<fileset dir="vendor/vanilla/garden-cli/src">
83+
<include name="**/*.php"/>
84+
</fileset>
85+
</copy>
8186
<copy todir="${basedir}/build/phar/phpdraft">
8287
<fileset dir="${basedir}">
8388
<include name="**/phpdraft"/>

composer.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@
1919
],
2020
"require": {
2121
"php": "~7.1",
22-
"ql/uri-template": "1.*",
23-
"michelf/php-markdown": "1.*",
24-
"lukasoppermann/http-status": "2.*",
25-
"ext-json": "*"
22+
"ql/uri-template": "~1.0",
23+
"vanilla/garden-cli": "~2.0",
24+
"michelf/php-markdown": "~1.8",
25+
"lukasoppermann/http-status": "~2.0",
26+
"ext-json": "*",
27+
"ext-curl": "*"
2628
},
2729
"require-dev": {
2830
"lunr/halo": "*",
2931
"phpunit/phpunit": "~7.0",
30-
"theseer/autoload": "1.*"
32+
"theseer/autoload": "~1.0",
33+
"phing/phing": "~2.0"
3134
},
3235
"autoload": {
3336
"psr-4": { "PHPDraft\\": "src/PHPDraft" }

phpdraft

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,44 @@ set_include_path(get_include_path() . ":" . __DIR__ . '/src/');
1212
//require_once 'PHPDraft/Core/Autoloader.php';
1313
require_once 'vendor/autoload.php';
1414

15+
use Garden\Cli\Cli;
1516
use PHPDraft\In\ApibFileParser;
16-
use PHPDraft\Out\UI;
17+
use PHPDraft\Out\Sorting;
18+
use PHPDraft\Out\Version;
19+
use PHPDraft\Parse\ExecutionException;
1720
use PHPDraft\Parse\JsonToHTML;
1821
use PHPDraft\Parse\ParserFactory;
22+
use PHPDraft\Parse\ResourceException;
1923

2024
define('VERSION', '0');
2125
try
2226
{
23-
$values = UI::main($argv);
24-
$apib = new ApibFileParser($values['file']);
27+
// Define the cli options.
28+
$cli = new Cli();
29+
$cli->description('Parse API Blueprint files.')
30+
->opt('file:f', 'Specifies the file to parse.', true, 'string')
31+
->opt('yes:y', 'Always accept using the online mode.', false)
32+
->opt('online:o', 'Always use the online mode.', false)
33+
->opt('template:t', 'Specifies the template to use. (defaults to \'default\').', false, 'string')
34+
->opt('sort:s', 'Sort displayed values [All|None|Structures|Webservices] (defaults to the way the objects are in the file).', false, 'string')
35+
->opt('header_image:i', 'Specifies an image to display in the header.', false, 'string')
36+
->opt('css:c', 'Specifies a CSS file to include (value is put in a link element without checking).', false, 'string')
37+
->opt('javascript:j', 'Specifies a JS file to include (value is put in a script element without checking).', false, 'string')
38+
->opt('version:v', 'Print the version for PHPDraft.', false);
39+
40+
// Parse and return cli args.
41+
$args = $cli->parse($argv, FALSE);
42+
if (isset($args['version'])) {
43+
Version::version();
44+
throw new ExecutionException('', 0);
45+
}
46+
47+
define('THIRD_PARTY_ALLOWED', getenv('PHPDRAFT_THIRD_PARTY') !== '0');
48+
if ((isset($args['y']) || isset($args['o'])) && THIRD_PARTY_ALLOWED) {
49+
define('DRAFTER_ONLINE_MODE', 1);
50+
}
51+
52+
$apib = new ApibFileParser($args->getOpt('file'));
2553
$apib = $apib->parse();
2654
$offline = FALSE;
2755
$online = FALSE;
@@ -31,17 +59,31 @@ try
3159
$parser = ParserFactory::get();
3260
$parser = $parser->init($apib);
3361
}
34-
catch (\PHPDraft\Parse\ResourceException $exception)
62+
catch (ResourceException $exception)
3563
{
3664
file_put_contents('php://stderr', "No drafter available.\n");
3765
return;
3866
}
3967

4068
$html = new JsonToHTML($parser->parseToJson());
41-
$html->sorting = $values['sorting'];
42-
$generator = $html->get_html($values['template'], $values['image'], $values['css'], $values['js']);
69+
$name = 'PHPD_SORT_' . strtoupper($args->getOpt('sort', ''));
70+
$html->sorting = Sorting::${$name} ?? -1;
71+
72+
$color1 = getenv('COLOR_PRIMARY') === FALSE ? NULL : getenv('COLOR_PRIMARY');
73+
$color2 = getenv('COLOR_SECONDARY') === FALSE ? NULL : getenv('COLOR_SECONDARY');
74+
$colors = (is_null($color1) || is_null($color2)) ? '' : '__' . $color1 . '__' . $color2;
75+
$generator = $html->get_html(
76+
$args->getOpt('template', 'default') . $colors,
77+
$args['header_image'],
78+
$args['css'],
79+
$args['javascript']);
80+
}
81+
catch (ExecutionException $exception)
82+
{
83+
file_put_contents('php://stderr', $exception->getMessage() . PHP_EOL);
84+
exit($exception->getCode());
4385
}
44-
catch (\PHPDraft\Parse\ExecutionException $exception)
86+
catch (Exception $exception)
4587
{
4688
file_put_contents('php://stderr', $exception->getMessage() . PHP_EOL);
4789
exit($exception->getCode());

src/PHPDraft/In/ApibFileParser.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class ApibFileParser
4242
*
4343
* @param string $filename File to parse
4444
*/
45-
public function __construct($filename = 'index.apib')
45+
public function __construct(string $filename = 'index.apib')
4646
{
4747
$this->filename = $filename;
4848
$this->location = pathinfo($this->filename, PATHINFO_DIRNAME) . '/';
@@ -53,9 +53,11 @@ public function __construct($filename = 'index.apib')
5353
/**
5454
* Get parse the apib file.
5555
*
56+
* @throws ExecutionException
57+
*
5658
* @return $this self reference.
5759
*/
58-
public function parse()
60+
public function parse(): self
5961
{
6062
$this->full_apib = $this->get_apib($this->filename, $this->location);
6163

@@ -73,7 +75,7 @@ public function parse()
7375
*
7476
* @return string The full API blueprint file.
7577
*/
76-
private function get_apib($filename, $rel_path = NULL)
78+
private function get_apib(string $filename, ?string $rel_path = NULL): string
7779
{
7880
$path = $this->file_path($filename, $rel_path);
7981
$file = file_get_contents($path);
@@ -95,22 +97,22 @@ private function get_apib($filename, $rel_path = NULL)
9597
/**
9698
* Check if an APIB file exists.
9799
*
98-
* @param string $filename File to check
100+
* @param string $filename File to check
99101
* @param string|null $rel_path File location to look.
100102
*
101103
* @throws ExecutionException when the file could not be found.
102104
*
103105
* @return string
104106
*/
105-
private function file_path($filename, $rel_path = NULL)
107+
private function file_path(string $filename, ?string $rel_path = NULL): string
106108
{
107109
// Absolute path
108110
if (file_exists($filename)) {
109111
return $filename;
110112
}
111113

112114
// Path relative to the top file
113-
if ($rel_path !== null && file_exists($rel_path . $filename)) {
115+
if ($rel_path !== NULL && file_exists($rel_path . $filename)) {
114116
return $rel_path . $filename;
115117
}
116118

@@ -134,7 +136,7 @@ private function file_path($filename, $rel_path = NULL)
134136
*
135137
* @return string The schema as a string
136138
*/
137-
private function get_schema($url)
139+
private function get_schema(string $url): string
138140
{
139141
$ch = curl_init();
140142
curl_setopt($ch, CURLOPT_URL, $url);
@@ -151,7 +153,7 @@ private function get_schema($url)
151153
*
152154
* @return string
153155
*/
154-
public function __toString()
156+
public function __toString(): string
155157
{
156158
return $this->full_apib;
157159
}

src/PHPDraft/Model/Category.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace PHPDraft\Model;
1111

1212
use PHPDraft\Model\Elements\ObjectStructureElement;
13+
use stdClass;
1314

1415
/**
1516
* Class Category.
@@ -26,11 +27,11 @@ class Category extends HierarchyElement
2627
/**
2728
* Fill class values based on JSON object.
2829
*
29-
* @param \stdClass $object JSON object
30+
* @param stdClass $object JSON object
3031
*
3132
* @return $this self-reference
3233
*/
33-
public function parse($object)
34+
public function parse(stdClass $object)
3435
{
3536
parent::parse($object);
3637
foreach ($object->content as $item) {

src/PHPDraft/Model/Comparable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ interface Comparable
1717
*
1818
* @return bool
1919
*/
20-
public function is_equal_to($b);
20+
public function is_equal_to($b): bool;
2121
}

src/PHPDraft/Model/Elements/ArrayStructureElement.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class ArrayStructureElement extends BasicStructureElement
2222
*
2323
* @return self Self reference
2424
*/
25-
public function parse($object, &$dependencies)
25+
public function parse($object, array &$dependencies): StructureElement
2626
{
2727
$this->element = (isset($object->element)) ? $object->element : 'array';
2828

@@ -52,7 +52,7 @@ public function parse($object, &$dependencies)
5252
*
5353
* @return string
5454
*/
55-
public function __toString()
55+
public function __toString(): string
5656
{
5757
$return = '<ul class="list-group mdl-list">';
5858

@@ -77,7 +77,7 @@ public function __toString()
7777
*
7878
* @return self
7979
*/
80-
protected function new_instance()
80+
protected function new_instance(): StructureElement
8181
{
8282
return new self();
8383
}

src/PHPDraft/Model/Elements/BasicStructureElement.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace PHPDraft\Model\Elements;
1010

1111
use Michelf\MarkdownExtra;
12+
use stdClass;
1213

1314
abstract class BasicStructureElement implements StructureElement
1415
{
@@ -58,26 +59,26 @@ abstract class BasicStructureElement implements StructureElement
5859
/**
5960
* Parse a JSON object to a structure.
6061
*
61-
* @param \stdClass $object An object to parse
62-
* @param array $dependencies Dependencies of this object
62+
* @param stdClass $object An object to parse
63+
* @param array $dependencies Dependencies of this object
6364
*
6465
* @return StructureElement self reference
6566
*/
66-
abstract public function parse($object, &$dependencies);
67+
abstract public function parse($object, array &$dependencies): StructureElement;
6768

6869
/**
6970
* Print a string representation.
7071
*
7172
* @return string
7273
*/
73-
abstract public function __toString();
74+
abstract public function __toString(): string;
7475

7576
/**
7677
* Get a new instance of a class.
7778
*
7879
* @return self
7980
*/
80-
abstract protected function new_instance();
81+
abstract protected function new_instance(): StructureElement;
8182

8283
/**
8384
* Parse common fields to give context.
@@ -87,7 +88,7 @@ abstract protected function new_instance();
8788
*
8889
* @return void
8990
*/
90-
protected function parse_common($object, &$dependencies)
91+
protected function parse_common($object, array &$dependencies): void
9192
{
9293
$this->key = (isset($object->content->key->content)) ? $object->content->key->content : NULL;
9394
$this->type = (isset($object->content->value->element)) ? $object->content->value->element : NULL;
@@ -107,12 +108,14 @@ protected function parse_common($object, &$dependencies)
107108
*
108109
* @return void
109110
*/
110-
public function description_as_html()
111+
public function description_as_html(): void
111112
{
112113
$this->description = MarkdownExtra::defaultTransform($this->description);
113114
}
114115

115116
/**
117+
* Get a string representation of the value.
118+
*
116119
* @return mixed|ObjectStructureElement|ObjectStructureElement[]
117120
*/
118121
public function string_value()

src/PHPDraft/Model/Elements/EnumStructureElement.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class EnumStructureElement extends BasicStructureElement
1919
*
2020
* @return $this
2121
*/
22-
public function parse($object, &$dependencies)
22+
public function parse($object, array &$dependencies): StructureElement
2323
{
2424
$this->element = (isset($object->element)) ? $object->element : 'enum';
2525

@@ -52,7 +52,7 @@ public function parse($object, &$dependencies)
5252
*
5353
* @return string
5454
*/
55-
public function __toString()
55+
public function __toString(): string
5656
{
5757
$return = '<ul class="list-group mdl-list">';
5858

@@ -84,7 +84,7 @@ public function __toString()
8484
*
8585
* @return self
8686
*/
87-
protected function new_instance()
87+
protected function new_instance(): StructureElement
8888
{
8989
return new self();
9090
}

0 commit comments

Comments
 (0)