99namespace PHPDraft \Out ;
1010
1111
12+ use Exception ;
13+ use Phar ;
14+ use Throwable ;
15+
1216class UI
1317{
18+ protected $ versionStringPrinted ;
19+
1420 static function main ($ argv = [])
1521 {
16- $ options = getopt ("f:t::i::hv " );
22+ $ options = getopt ("f:t::i::hvu " );
1723 if (!isset ($ argv [1 ]))
1824 {
1925 file_put_contents ('php://stderr ' , 'Not enough arguments ' .PHP_EOL );
@@ -35,6 +41,12 @@ static function main($argv = [])
3541 exit (0 );
3642 }
3743
44+ if (isset ($ options ['u ' ]))
45+ {
46+ self ::handleSelfUpdate ();
47+ exit (0 );
48+ }
49+
3850 elseif (isset ($ options ['f ' ]))
3951 {
4052 $ file = $ options ['f ' ];
@@ -60,6 +72,98 @@ static function main($argv = [])
6072 ];
6173 }
6274
75+ private function printVersionString ()
76+ {
77+ print self ::version () . "\n\n" ;
78+ }
79+
80+ /**
81+ * @since Method available since Release 1.4
82+ */
83+ protected function handleSelfUpdate ()
84+ {
85+ self ::printVersionString ();
86+ $ localFilename = realpath ($ _SERVER ['argv ' ][0 ]);
87+ if (!is_writable ($ localFilename )) {
88+ print 'No write permission to update ' . $ localFilename . "\n" ;
89+ exit (3 );
90+ }
91+ if (!extension_loaded ('openssl ' )) {
92+ print "The OpenSSL extension is not loaded. \n" ;
93+ exit (3 );
94+ }
95+ //set POST variables
96+ //https://github.com/SMillerDev/phpdraft/releases/download/1.3.2/phpdraft.phar
97+ $ url = 'https://github.com/SMillerDev/phpdraft/releases/latest ' ;
98+ $ ch = curl_init ();
99+ curl_setopt ($ ch ,CURLOPT_URL , $ url );
100+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
101+ $ result = curl_exec ($ ch );
102+ $ matches = [];
103+ preg_match ("/href= \"https:\/\/github.com\/SMillerDev\/phpdraft\/releases\/tag\/([0-9.]*) \"/ " , $ result , $ matches );
104+ curl_close ($ ch );
105+ $ remoteFilename = sprintf (
106+ 'https://github.com/SMillerDev/phpdraft/releases/download/%s/phpdraft.phar ' ,
107+ $ matches [0 ]
108+ );
109+
110+ $ tempFilename = tempnam (sys_get_temp_dir (), 'phpdraft ' ) . '.phar ' ;
111+ // Workaround for https://bugs.php.net/bug.php?id=65538
112+ $ caFile = dirname ($ tempFilename ) . '/ca.pem ' ;
113+ copy (__PHPDRAFT_PHAR_ROOT__ . '/ca.pem ' , $ caFile );
114+ print 'Updating the PHPDraft PHAR ... ' ;
115+ $ options = [
116+ 'ssl ' => [
117+ 'allow_self_signed ' => false ,
118+ 'cafile ' => $ caFile ,
119+ 'verify_peer ' => true
120+ ]
121+ ];
122+ file_put_contents (
123+ $ tempFilename ,
124+ file_get_contents (
125+ $ remoteFilename ,
126+ false ,
127+ stream_context_create ($ options )
128+ )
129+ );
130+ chmod ($ tempFilename , 0777 & ~umask ());
131+ try {
132+ $ phar = new Phar ($ tempFilename );
133+ unset($ phar );
134+ rename ($ tempFilename , $ localFilename );
135+ unlink ($ caFile );
136+ } catch (Throwable $ _e ) {
137+ $ e = $ _e ;
138+ } catch (Exception $ _e ) {
139+ $ e = $ _e ;
140+ }
141+ if (isset ($ e )) {
142+ unlink ($ caFile );
143+ unlink ($ tempFilename );
144+ print " done \n\n" . $ e ->getMessage () . "\n" ;
145+ exit (2 );
146+ }
147+ print " done \n" ;
148+ exit (0 );
149+ }
150+ /**
151+ * @since Method available since Release 1.4
152+ */
153+ protected function handleVersionCheck ()
154+ {
155+ $ this ->printVersionString ();
156+ $ latestVersion = file_get_contents ('https://phar.phpdraft.de/latest-version-of/phpdraft ' );
157+ $ isOutdated = version_compare ($ latestVersion , self ::release_id (), '> ' );
158+ if ($ isOutdated ) {
159+ print "You are not using the latest version of PHPDraft. \n" ;
160+ print 'Use "phpdraft --self-upgrade" to install PHPDraft ' . $ latestVersion . "\n" ;
161+ } else {
162+ print "You are using the latest version of PHPDraft. \n" ;
163+ }
164+ exit (0 );
165+ }
166+
63167 static function help ()
64168 {
65169 echo 'This is a parser for API Blueprint files in PHP. ' .PHP_EOL .PHP_EOL ;
@@ -71,8 +175,46 @@ static function help()
71175
72176 static function version ()
73177 {
74- $ version = ( VERSION === ' 0 ' ) ? @ exec ( ' git describe --tags 2>&1 ' ) : VERSION ;
178+ $ version = self :: release_id () ;
75179 echo 'PHPDraft: ' .$ version ;
76180 }
77181
182+ /**
183+ * Get the version number
184+ *
185+ * @return string
186+ */
187+ static function release_id ()
188+ {
189+ return (VERSION === '0 ' ) ? @exec ('git describe --tags 2>&1 ' ) : VERSION ;
190+ }
191+
192+
193+ /**
194+ * @return string
195+ *
196+ * @since Method available since Release 4.8.13
197+ */
198+ public static function series ()
199+ {
200+ if (strpos (self ::release_id (), '- ' )) {
201+ $ version = explode ('- ' , self ::release_id ())[0 ];
202+ } else {
203+ $ version = self ::release_id ();
204+ }
205+ return implode ('. ' , array_slice (explode ('. ' , $ version ), 0 , 2 ));
206+ }
207+
208+ /**
209+ * @return string
210+ *
211+ * @since Method available since Release 4.0.0
212+ */
213+ public static function getReleaseChannel ()
214+ {
215+ if (strpos (self ::release_id (), '- ' ) !== false ) {
216+ return '-nightly ' ;
217+ }
218+ return '' ;
219+ }
78220}
0 commit comments