1313 */
1414class UI
1515{
16- public static $ PHPD_SORT_ALL = 3 ;
16+ public static $ PHPD_SORT_ALL = 3 ;
1717 public static $ PHPD_SORT_WEBSERVICES = 2 ;
18- public static $ PHPD_SORT_STRUCTURES = 1 ;
18+ public static $ PHPD_SORT_STRUCTURES = 1 ;
1919 protected $ versionStringPrinted ;
2020
2121 /**
@@ -27,41 +27,34 @@ class UI
2727 */
2828 static function main ($ argv = [])
2929 {
30- $ options = getopt ('f:t:i:c:j:s:hvu ' );
30+ $ options = getopt ('f:t:i:c:j:s:hvuyo ' );
3131
32- if (!isset ($ argv [1 ]))
33- {
32+ if (!isset ($ argv [1 ])) {
3433 file_put_contents ('php://stderr ' , 'Not enough arguments ' . PHP_EOL );
3534 self ::help ();
3635 exit (1 );
3736 }
3837
3938 $ sorting = -1 ;
40- if (isset ($ options ['s ' ]))
41- {
39+ if (isset ($ options ['s ' ])) {
4240 $ value = strtoupper ($ options ['s ' ]);
43- if (isset (UI ::$ {'PHPD_SORT_ ' . $ value }))
44- {
41+ if (isset (UI ::$ {'PHPD_SORT_ ' . $ value })) {
4542 $ sorting = UI ::$ {'PHPD_SORT_ ' . $ value };
4643 }
4744 }
4845
49- if (boolval (preg_match ('/^\-/ ' , $ argv [1 ])))
50- {
51- if (isset ($ options ['h ' ]))
52- {
46+ if (boolval (preg_match ('/^\-/ ' , $ argv [1 ]))) {
47+ if (isset ($ options ['h ' ])) {
5348 self ::help ();
5449 exit (0 );
5550 }
5651
57- if (isset ($ options ['v ' ]))
58- {
52+ if (isset ($ options ['v ' ])) {
5953 self ::version ();
6054 exit (0 );
6155 }
6256
63- if (isset ($ options ['f ' ]))
64- {
57+ if (isset ($ options ['f ' ])) {
6558 $ file = $ options ['f ' ];
6659 } else {
6760 file_put_contents ('php://stderr ' , 'No file to parse ' . PHP_EOL );
@@ -70,11 +63,14 @@ static function main($argv = [])
7063 } else {
7164 $ file = $ argv [1 ];
7265 }
66+ if (isset ($ options ['y ' ]) || isset ($ options ['o ' ])) {
67+ define ('DRAFTER_ONLINE_MODE ' , 1 );
68+ }
7369
7470 $ template = (isset ($ options ['t ' ]) && $ options ['t ' ]) ? $ options ['t ' ] : 'default ' ;
75- $ image = (isset ($ options ['i ' ]) && $ options ['i ' ]) ? $ options ['i ' ] : NULL ;
76- $ css = (isset ($ options ['c ' ]) && $ options ['c ' ]) ? $ options ['i ' ] : NULL ;
77- $ js = (isset ($ options ['j ' ]) && $ options ['j ' ]) ? $ options ['i ' ] : NULL ;
71+ $ image = (isset ($ options ['i ' ]) && $ options ['i ' ]) ? $ options ['i ' ] : null ;
72+ $ css = (isset ($ options ['c ' ]) && $ options ['c ' ]) ? $ options ['i ' ] : null ;
73+ $ js = (isset ($ options ['j ' ]) && $ options ['j ' ]) ? $ options ['i ' ] : null ;
7874
7975 return [
8076 'file ' => $ file ,
@@ -125,16 +121,6 @@ static function release_id()
125121 return (VERSION === '0 ' ) ? @exec ('git describe --tags 2>&1 ' ) : VERSION ;
126122 }
127123
128- /**
129- * Print the version string
130- *
131- * @return void
132- */
133- private function printVersionString ()
134- {
135- print self ::version () . "\n\n" ;
136- }
137-
138124 /**
139125 * Print the series of the update
140126 *
@@ -144,8 +130,7 @@ private function printVersionString()
144130 */
145131 public static function series ()
146132 {
147- if (strpos (self ::release_id (), '- ' ))
148- {
133+ if (strpos (self ::release_id (), '- ' )) {
149134 $ version = explode ('- ' , self ::release_id ())[0 ];
150135 } else {
151136 $ version = self ::release_id ();
@@ -161,14 +146,41 @@ public static function series()
161146 */
162147 public static function getReleaseChannel ()
163148 {
164- if (strpos (self ::release_id (), '- ' ) !== FALSE )
165- {
149+ if (strpos (self ::release_id (), '- ' ) !== false ) {
166150 return '-nightly ' ;
167151 }
168152
169153 return '' ;
170154 }
171155
156+ /**
157+ * Ask a question to the user
158+ *
159+ * @param string $message The question
160+ * @param array $options Possible answers
161+ *
162+ * @param string $positive The parameter that gives a positive outcome
163+ *
164+ * @return boolean
165+ */
166+ public static function ask ($ message , $ options , $ positive = 'y ' )
167+ {
168+ file_put_contents ('php://stdout ' , $ message );
169+ do {
170+ $ selection = fgetc (STDIN );
171+ } while (trim ($ selection ) == '' );
172+
173+ if (array_key_exists (strtolower ($ selection ), $ options )) {
174+ return ($ selection === $ positive );
175+ }
176+ if (array_search ($ selection , $ options )) {
177+ return (array_search ($ selection , $ options ) === $ positive );
178+ }
179+ file_put_contents ('php://stderr ' , 'That answer wasn \'t expected, try again. ' .PHP_EOL .PHP_EOL );
180+
181+ return UI ::ask ($ message , $ options , $ positive );
182+ }
183+
172184 /**
173185 * Handle the check for a version
174186 *
@@ -181,8 +193,7 @@ protected function handleVersionCheck()
181193 $ this ->printVersionString ();
182194 $ latestVersion = file_get_contents ('https://phar.phpdraft.de/latest-version-of/phpdraft ' );
183195 $ isOutdated = version_compare ($ latestVersion , self ::release_id (), '> ' );
184- if ($ isOutdated )
185- {
196+ if ($ isOutdated ) {
186197 print "You are not using the latest version of PHPDraft. \n" ;
187198 print 'Use "phpdraft --self-upgrade" to install PHPDraft ' . $ latestVersion . "\n" ;
188199 } else {
@@ -191,4 +202,14 @@ protected function handleVersionCheck()
191202
192203 exit (0 );
193204 }
205+
206+ /**
207+ * Print the version string
208+ *
209+ * @return void
210+ */
211+ private function printVersionString ()
212+ {
213+ print self ::version () . "\n\n" ;
214+ }
194215}
0 commit comments