12
12
class Factory
13
13
{
14
14
private $ loop ;
15
-
15
+ private $ bin = PHP_BINARY ;
16
16
private $ useSocket ;
17
17
18
18
/**
@@ -32,6 +32,18 @@ public function __construct(LoopInterface $loop)
32
32
33
33
// use socket I/O for Windows only, use faster process pipes everywhere else
34
34
$ this ->useSocket = DIRECTORY_SEPARATOR === '\\' ;
35
+
36
+ // if this is the php-cgi binary, check if we can execute the php binary instead
37
+ $ candidate = \str_replace ('-cgi ' , '' , $ this ->bin );
38
+ if ($ candidate !== $ this ->bin && \is_executable ($ candidate )) {
39
+ $ this ->bin = $ candidate ; // @codeCoverageIgnore
40
+ }
41
+
42
+ // if `php` is a symlink to the php binary, use the shorter `php` name
43
+ // this is purely cosmetic feature for the process list
44
+ if (\realpath ($ this ->which ('php ' )) === $ this ->bin ) {
45
+ $ this ->bin = 'php ' ; // @codeCoverageIgnore
46
+ }
35
47
}
36
48
37
49
/**
@@ -78,7 +90,7 @@ public function open($filename, $flags = null)
78
90
79
91
private function openProcessIo ($ filename , $ flags = null )
80
92
{
81
- $ command = 'exec ' . \escapeshellarg (\ PHP_BINARY ) . ' ' . \escapeshellarg ( __DIR__ . ' /../res/ sqlite-worker.php ') ;
93
+ $ command = 'exec ' . \escapeshellarg ($ this -> bin ) . ' sqlite-worker.php ' ;
82
94
83
95
// Try to get list of all open FDs (Linux/Mac and others)
84
96
$ fds = @\scandir ('/dev/fd ' );
@@ -120,7 +132,7 @@ private function openProcessIo($filename, $flags = null)
120
132
$ command = 'exec bash -c ' . \escapeshellarg ($ command );
121
133
}
122
134
123
- $ process = new Process ($ command , null , null , $ pipes );
135
+ $ process = new Process ($ command , __DIR__ . ' /../res ' , null , $ pipes );
124
136
$ process ->start ($ this ->loop );
125
137
126
138
$ db = new ProcessIoDatabase ($ process );
@@ -139,14 +151,14 @@ private function openProcessIo($filename, $flags = null)
139
151
140
152
private function openSocketIo ($ filename , $ flags = null )
141
153
{
142
- $ command = \escapeshellarg (\ PHP_BINARY ) . ' ' . \escapeshellarg ( __DIR__ . ' /../res/ sqlite-worker.php ') ;
154
+ $ command = \escapeshellarg ($ this -> bin ) . ' sqlite-worker.php ' ;
143
155
144
156
// launch process without default STDIO pipes
145
157
$ null = \DIRECTORY_SEPARATOR === '\\' ? 'nul ' : '/dev/null ' ;
146
158
$ pipes = array (
147
159
array ('file ' , $ null , 'r ' ),
148
160
array ('file ' , $ null , 'w ' ),
149
- STDERR // array('file ', $null, 'w'),
161
+ \defined ( ' STDERR ' ) ? \ STDERR : \fopen ( ' php://stderr ' , 'w ' )
150
162
);
151
163
152
164
// start temporary socket on random address
@@ -161,7 +173,7 @@ private function openSocketIo($filename, $flags = null)
161
173
stream_set_blocking ($ server , false );
162
174
$ command .= ' ' . stream_socket_get_name ($ server , false );
163
175
164
- $ process = new Process ($ command , null , null , $ pipes );
176
+ $ process = new Process ($ command , __DIR__ . ' /../res ' , null , $ pipes );
165
177
$ process ->start ($ this ->loop );
166
178
167
179
$ deferred = new Deferred (function () use ($ process , $ server ) {
@@ -214,4 +226,19 @@ private function openSocketIo($filename, $flags = null)
214
226
215
227
return $ deferred ->promise ();
216
228
}
229
+
230
+ /**
231
+ * @param string $bin
232
+ * @return string|null
233
+ * @codeCoverageIgnore
234
+ */
235
+ private function which ($ bin )
236
+ {
237
+ foreach (\explode (\PATH_SEPARATOR , \getenv ('PATH ' )) as $ path ) {
238
+ if (\is_executable ($ path . \DIRECTORY_SEPARATOR . $ bin )) {
239
+ return $ path . \DIRECTORY_SEPARATOR . $ bin ;
240
+ }
241
+ }
242
+ return null ;
243
+ }
217
244
}
0 commit comments