@@ -12,16 +12,44 @@ set_include_path(get_include_path() . ":" . __DIR__ . '/src/');
1212//require_once 'PHPDraft/Core/Autoloader.php';
1313require_once 'vendor/autoload.php ' ;
1414
15+ use Garden \Cli \Cli ;
1516use PHPDraft \In \ApibFileParser ;
16- use PHPDraft \Out \UI ;
17+ use PHPDraft \Out \Sorting ;
18+ use PHPDraft \Out \Version ;
19+ use PHPDraft \Parse \ExecutionException ;
1720use PHPDraft \Parse \JsonToHTML ;
1821use PHPDraft \Parse \ParserFactory ;
22+ use PHPDraft \Parse \ResourceException ;
1923
2024define ('VERSION ' , '0 ' );
2125try
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 ;
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 ());
0 commit comments