-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathphpdraft
More file actions
executable file
·127 lines (114 loc) · 4.49 KB
/
phpdraft
File metadata and controls
executable file
·127 lines (114 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
declare(strict_types=1);
/**
* Set up include path for source handling
*/
set_include_path(get_include_path() . ":" . __DIR__ . '/src/');
/**
* Set up required classes (with the autoloader)
*/
require_once 'vendor/autoload.php';
use Garden\Cli\Cli;
use PHPDraft\In\ApibFileParser;
use PHPDraft\Out\Sorting;
use PHPDraft\Out\Version;
use PHPDraft\Parse\ExecutionException;
use PHPDraft\Parse\ParserFactory;
use PHPDraft\Parse\ResourceException;
define('VERSION', '0');
//define('ID_STATIC', 'SOME_ID');
try
{
// Define the cli options.
$cli = new Cli();
$cli->description('Parse API Blueprint files.')
->opt('help:h', 'This help text', false)
->opt('version:v', 'Print the version for PHPDraft.', false)
->opt('file:f', 'Specifies the file to parse.', false)
->opt('yes:y', 'Always accept using the online mode.', false, 'bool')
->opt('online:o', 'Always use the online mode.', false, 'bool')
->opt('template:t', 'Specifies the template to use. (defaults to \'default\').', false)
->opt('sort:s', 'Sort displayed values [All|None|Structures|Webservices] (defaults to the way the objects are in the file).', false)
->opt('header_image:i', 'Specifies an image to display in the header.', false)
->opt('css:c', 'Specifies a CSS file to include (value is put in a link element without checking).', false)
->opt('javascript:j', 'Specifies a JS file to include (value is put in a script element without checking).', false)
->opt('debug-json-file', 'Input a rendered JSON file for debugging.', false)
->opt('debug-json', 'Input a rendered JSON text for debugging.', false);
// Parse and return cli args.
$args = $cli->parse($argv, FALSE);
if (isset($args['help']) || empty($args->getOpts())) {
$cli->writeHelp();
throw new ExecutionException('', 0);
}
if (isset($args['version'])) {
Version::version();
throw new ExecutionException('', 0);
}
stream_set_blocking(STDIN, false);
$stdin = stream_get_contents(STDIN);
$file = $args->getOpt('file');
if (!empty($stdin) && $file !== NULL) {
throw new ExecutionException('ERROR: Passed data in both file and stdin', 2);
} elseif (!empty($stdin) && $file === NULL) {
$file = tempnam(sys_get_temp_dir(), 'phpdraft');
file_put_contents($file, $stdin);
}
if ($file === NULL || $file === '')
{
throw new ExecutionException('ERROR: File does not exist', 200);
}
if (!($file !== NULL || isset($args['debug-json-file']) || isset($args['debug-json']))) {
throw new ExecutionException('Missing required option: file', 1);
}
define('THIRD_PARTY_ALLOWED', getenv('PHPDRAFT_THIRD_PARTY') !== '0');
if ((isset($args['y']) || isset($args['o'])) && THIRD_PARTY_ALLOWED) {
define('DRAFTER_ONLINE_MODE', 1);
}
if (!isset($args['debug-json-file']) && !isset($args['debug-json'])) {
$apib_parser = new ApibFileParser($file);
$apib = $apib_parser->parse();
$offline = FALSE;
$online = FALSE;
try {
$parser = ParserFactory::getDrafter();
$parser = $parser->init($apib);
$data = $parser->parseToJson();
} catch (ResourceException $exception) {
throw new ExecutionException('No drafter available', 255);
}
} else {
$json_string = $args['debug-json'] ?? file_get_contents($args['debug-json-file']);
$data = json_decode($json_string);
}
$html = ParserFactory::getJson()->init($data);
$name = 'PHPD_SORT_' . strtoupper($args->getOpt('sort', ''));
$html->sorting = Sorting::${$name} ?? -1;
$color1 = getenv('COLOR_PRIMARY') === FALSE ? NULL : getenv('COLOR_PRIMARY');
$color2 = getenv('COLOR_SECONDARY') === FALSE ? NULL : getenv('COLOR_SECONDARY');
$colors = (is_null($color1) || is_null($color2)) ? '' : '__' . $color1 . '__' . $color2;
$html->build_html(
$args->getOpt('template', 'default') . $colors,
$args['header_image'],
$args['css'],
$args['javascript']
);
echo $html;
}
catch (ExecutionException|Exception $exception)
{
file_put_contents('php://stderr', $exception->getMessage() . PHP_EOL);
exit($exception->getCode());
}
function phpdraft_var_dump(...$vars)
{
if (defined('__PHPDRAFT_PHAR__'))
{
return;
}
echo '<pre>';
foreach ($vars as $var)
{
var_dump($var);
}
echo '</pre>';
}