@@ -72,15 +72,16 @@ class PHPFmt {
72
72
73
73
format ( context , text ) {
74
74
return new Promise ( ( resolve , reject ) => {
75
- cp . exec ( `${ this . phpBin } -r "echo PHP_VERSION_ID;"` , ( err , stdout ) => {
76
- if ( err ) {
77
- window . showErrorMessage ( 'cannot find php bin' ) ;
78
- reject ( ) ;
79
- } else if ( Number ( stdout . toString ( ) ) < 70000 ) {
75
+ try {
76
+ const stdout = cp . execSync ( `${ this . phpBin } -r "echo PHP_VERSION_ID;"` ) ;
77
+ if ( Number ( stdout . toString ( ) ) < 70000 ) {
80
78
window . showErrorMessage ( 'php version < 7.0' ) ;
81
- reject ( ) ;
79
+ return reject ( ) ;
82
80
}
83
- } ) ;
81
+ } catch ( e ) {
82
+ window . showErrorMessage ( 'cannot find php bin' ) ;
83
+ return reject ( ) ;
84
+ }
84
85
85
86
const fileName =
86
87
tmpDir +
@@ -93,12 +94,12 @@ class PHPFmt {
93
94
fs . writeFileSync ( fileName , text ) ;
94
95
95
96
// test whether the php file has syntax error
96
- cp . exec ( ` ${ this . phpBin } -l ${ fileName } ` , err => {
97
- if ( err && err . code && err . code !== 0 ) {
98
- window . showErrorMessage ( 'syntax error in your php file' ) ;
99
- reject ( ) ;
100
- }
101
- } ) ;
97
+ try {
98
+ cp . execSync ( ` ${ this . phpBin } -l ${ fileName } ` ) ;
99
+ } catch ( e ) {
100
+ window . showErrorMessage ( 'syntax error in your php file' ) ;
101
+ return reject ( ) ;
102
+ }
102
103
103
104
const args = this . getArgs ( fileName ) ;
104
105
args . unshift ( `${ context . extensionPath } /fmt.phar` ) ;
@@ -142,8 +143,6 @@ class PHPFmt {
142
143
}
143
144
144
145
exports . activate = context => {
145
- const phpfmt = new PHPFmt ( ) ;
146
-
147
146
context . subscriptions . push (
148
147
commands . registerTextEditorCommand ( 'phpfmt.format' , textEditor => {
149
148
if ( textEditor . document . languageId === 'php' ) {
@@ -160,6 +159,8 @@ exports.activate = context => {
160
159
const lastLine = document . lineAt ( document . lineCount - 1 ) ;
161
160
const range = new Range ( new Position ( 0 , 0 ) , lastLine . range . end ) ;
162
161
162
+ const phpfmt = new PHPFmt ( ) ;
163
+
163
164
phpfmt
164
165
. format ( context , originalText )
165
166
. then ( text => {
0 commit comments