13
13
*/
14
14
class UI
15
15
{
16
- public static $ PHPD_SORT_ALL = 3 ;
16
+ public static $ PHPD_SORT_ALL = 3 ;
17
17
public static $ PHPD_SORT_WEBSERVICES = 2 ;
18
- public static $ PHPD_SORT_STRUCTURES = 1 ;
18
+ public static $ PHPD_SORT_STRUCTURES = 1 ;
19
19
protected $ versionStringPrinted ;
20
20
21
21
/**
@@ -27,41 +27,34 @@ class UI
27
27
*/
28
28
static function main ($ argv = [])
29
29
{
30
- $ options = getopt ('f:t:i:c:j:s:hvu ' );
30
+ $ options = getopt ('f:t:i:c:j:s:hvuyo ' );
31
31
32
- if (!isset ($ argv [1 ]))
33
- {
32
+ if (!isset ($ argv [1 ])) {
34
33
file_put_contents ('php://stderr ' , 'Not enough arguments ' . PHP_EOL );
35
34
self ::help ();
36
35
exit (1 );
37
36
}
38
37
39
38
$ sorting = -1 ;
40
- if (isset ($ options ['s ' ]))
41
- {
39
+ if (isset ($ options ['s ' ])) {
42
40
$ value = strtoupper ($ options ['s ' ]);
43
- if (isset (UI ::$ {'PHPD_SORT_ ' . $ value }))
44
- {
41
+ if (isset (UI ::$ {'PHPD_SORT_ ' . $ value })) {
45
42
$ sorting = UI ::$ {'PHPD_SORT_ ' . $ value };
46
43
}
47
44
}
48
45
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 ' ])) {
53
48
self ::help ();
54
49
exit (0 );
55
50
}
56
51
57
- if (isset ($ options ['v ' ]))
58
- {
52
+ if (isset ($ options ['v ' ])) {
59
53
self ::version ();
60
54
exit (0 );
61
55
}
62
56
63
- if (isset ($ options ['f ' ]))
64
- {
57
+ if (isset ($ options ['f ' ])) {
65
58
$ file = $ options ['f ' ];
66
59
} else {
67
60
file_put_contents ('php://stderr ' , 'No file to parse ' . PHP_EOL );
@@ -70,11 +63,14 @@ static function main($argv = [])
70
63
} else {
71
64
$ file = $ argv [1 ];
72
65
}
66
+ if (isset ($ options ['y ' ]) || isset ($ options ['o ' ])) {
67
+ define ('DRAFTER_ONLINE_MODE ' , 1 );
68
+ }
73
69
74
70
$ 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 ;
78
74
79
75
return [
80
76
'file ' => $ file ,
@@ -125,16 +121,6 @@ static function release_id()
125
121
return (VERSION === '0 ' ) ? @exec ('git describe --tags 2>&1 ' ) : VERSION ;
126
122
}
127
123
128
- /**
129
- * Print the version string
130
- *
131
- * @return void
132
- */
133
- private function printVersionString ()
134
- {
135
- print self ::version () . "\n\n" ;
136
- }
137
-
138
124
/**
139
125
* Print the series of the update
140
126
*
@@ -144,8 +130,7 @@ private function printVersionString()
144
130
*/
145
131
public static function series ()
146
132
{
147
- if (strpos (self ::release_id (), '- ' ))
148
- {
133
+ if (strpos (self ::release_id (), '- ' )) {
149
134
$ version = explode ('- ' , self ::release_id ())[0 ];
150
135
} else {
151
136
$ version = self ::release_id ();
@@ -161,14 +146,41 @@ public static function series()
161
146
*/
162
147
public static function getReleaseChannel ()
163
148
{
164
- if (strpos (self ::release_id (), '- ' ) !== FALSE )
165
- {
149
+ if (strpos (self ::release_id (), '- ' ) !== false ) {
166
150
return '-nightly ' ;
167
151
}
168
152
169
153
return '' ;
170
154
}
171
155
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
+
172
184
/**
173
185
* Handle the check for a version
174
186
*
@@ -181,8 +193,7 @@ protected function handleVersionCheck()
181
193
$ this ->printVersionString ();
182
194
$ latestVersion = file_get_contents ('https://phar.phpdraft.de/latest-version-of/phpdraft ' );
183
195
$ isOutdated = version_compare ($ latestVersion , self ::release_id (), '> ' );
184
- if ($ isOutdated )
185
- {
196
+ if ($ isOutdated ) {
186
197
print "You are not using the latest version of PHPDraft. \n" ;
187
198
print 'Use "phpdraft --self-upgrade" to install PHPDraft ' . $ latestVersion . "\n" ;
188
199
} else {
@@ -191,4 +202,14 @@ protected function handleVersionCheck()
191
202
192
203
exit (0 );
193
204
}
205
+
206
+ /**
207
+ * Print the version string
208
+ *
209
+ * @return void
210
+ */
211
+ private function printVersionString ()
212
+ {
213
+ print self ::version () . "\n\n" ;
214
+ }
194
215
}
0 commit comments