@@ -31,9 +31,9 @@ class ProcWrapper
31
31
private $ command ;
32
32
33
33
/**
34
- * @var string
34
+ * @var string|null
35
35
*/
36
- private $ workDir = '' ;
36
+ private $ workDir ;
37
37
38
38
/**
39
39
* @var array
@@ -100,13 +100,18 @@ public static function new(string $command = '', array $descriptors = []): self
100
100
*/
101
101
public static function runCmd (string $ command , string $ workDir = '' ): array
102
102
{
103
+ $ isWindowsOS = '\\' === DIRECTORY_SEPARATOR ;
103
104
$ descriptors = [
104
105
0 => ['pipe ' , 'r ' ], // stdin - read channel
105
106
1 => ['pipe ' , 'w ' ], // stdout - write channel
106
107
2 => ['pipe ' , 'w ' ], // stdout - error channel
107
108
3 => ['pipe ' , 'r ' ], // stdin - This is the pipe we can feed the password into
108
109
];
109
110
111
+ if ($ isWindowsOS ) {
112
+ unset($ descriptors [3 ]);
113
+ }
114
+
110
115
$ proc = new ProcWrapper ($ command , $ descriptors );
111
116
$ proc ->run ($ workDir );
112
117
@@ -121,12 +126,13 @@ public static function runCmd(string $command, string $workDir = ''): array
121
126
$ error = stream_get_contents ($ pipes [2 ]);
122
127
fclose ($ pipes [2 ]);
123
128
124
- // TODO: Write passphrase in pipes[3].
125
- fclose ($ pipes [3 ]);
129
+ if (!$ isWindowsOS ) {
130
+ // TODO: Write passphrase in pipes[3].
131
+ fclose ($ pipes [3 ]);
132
+ }
126
133
127
134
// Close all pipes before proc_close! $code === 0 is success.
128
135
$ code = $ proc ->close ();
129
-
130
136
return [$ code , $ output , $ error ];
131
137
}
132
138
@@ -220,14 +226,22 @@ public function open(): self
220
226
throw new InvalidArgumentException ('The want execute command is cannot be empty ' );
221
227
}
222
228
223
- $ workDir = $ this ->workDir ;
229
+ $ workDir = $ this ->workDir ?: null ;
224
230
$ options = $ this ->options ;
225
231
226
232
$ options ['suppress_errors ' ] = true ;
227
233
if ('\\' === DIRECTORY_SEPARATOR ) { // windows
228
234
$ options ['bypass_shell ' ] = true ;
229
235
}
230
236
237
+ // ERROR on windows
238
+ // proc_open(): CreateProcess failed, error code - 123
239
+ //
240
+ // https://docs.microsoft.com/zh-cn/windows/win32/debug/system-error-codes--0-499-
241
+ // The filename, directory name, or volume label syntax is incorrect.
242
+ // FIX:
243
+ // 1. runCmd() not set $descriptors[3] on windows
244
+ // 2. $workDir set as null when is empty.
231
245
$ process = proc_open ($ command , $ this ->descriptors , $ this ->pipes , $ workDir , $ this ->runENV , $ options );
232
246
233
247
if (!is_resource ($ process )) {
@@ -357,9 +371,9 @@ public function getPipes(): array
357
371
}
358
372
359
373
/**
360
- * @return string
374
+ * @return string|null
361
375
*/
362
- public function getWorkDir (): string
376
+ public function getWorkDir (): ? string
363
377
{
364
378
return $ this ->workDir ;
365
379
}
0 commit comments