@@ -15,7 +15,10 @@ class Factory
1515 /** @var LoopInterface */
1616 private $ loop ;
1717
18- private $ bin = PHP_BINARY ;
18+ /** @var string */
19+ private $ bin ;
20+
21+ /** @var bool */
1922 private $ useSocket ;
2023
2124 /**
@@ -31,26 +34,28 @@ class Factory
3134 * This value SHOULD NOT be given unless you're sure you want to explicitly use a
3235 * given event loop instance.
3336 *
37+ * This class takes an optional `?string $binary` parameter that can be used to
38+ * pass a custom PHP binary to use when spawning a child process. You can use a
39+ * `null` value here in order to automatically detect the current PHP binary. You
40+ * may want to pass a custom executable path if this automatic detection fails or
41+ * if you explicitly want to run the child process with a different PHP version or
42+ * environment than your parent process.
43+ *
44+ * ```php
45+ * // advanced usage: pass custom PHP binary to use when spawning child process
46+ * $factory = new Clue\React\SQLite\Factory(null, '/usr/bin/php6.0');
47+ * ```
48+ *
3449 * @param ?LoopInterface $loop
50+ * @param ?string $binary
3551 */
36- public function __construct (LoopInterface $ loop = null )
52+ public function __construct (LoopInterface $ loop = null , $ binary = null )
3753 {
3854 $ this ->loop = $ loop ?: Loop::get ();
55+ $ this ->bin = $ binary === null ? $ this ->php () : $ binary ;
3956
4057 // use socket I/O for Windows only, use faster process pipes everywhere else
41- $ this ->useSocket = DIRECTORY_SEPARATOR === '\\' ;
42-
43- // if this is the php-cgi binary, check if we can execute the php binary instead
44- $ candidate = \str_replace ('-cgi ' , '' , $ this ->bin );
45- if ($ candidate !== $ this ->bin && \is_executable ($ candidate )) {
46- $ this ->bin = $ candidate ; // @codeCoverageIgnore
47- }
48-
49- // if `php` is a symlink to the php binary, use the shorter `php` name
50- // this is purely cosmetic feature for the process list
51- if (\realpath ($ this ->which ('php ' )) === $ this ->bin ) {
52- $ this ->bin = 'php ' ; // @codeCoverageIgnore
53- }
58+ $ this ->useSocket = \DIRECTORY_SEPARATOR === '\\' ;
5459 }
5560
5661 /**
@@ -360,4 +365,25 @@ private function resolve($filename)
360365 }
361366 return $ filename ;
362367 }
368+
369+ /**
370+ * @return string
371+ */
372+ private function php ()
373+ {
374+ // if this is the php-cgi binary, check if we can execute the php binary instead
375+ $ binary = \PHP_BINARY ;
376+ $ candidate = \str_replace ('-cgi ' , '' , $ binary );
377+ if ($ candidate !== $ binary && \is_executable ($ candidate )) {
378+ $ binary = $ candidate ; // @codeCoverageIgnore
379+ }
380+
381+ // if `php` is a symlink to the php binary, use the shorter `php` name
382+ // this is purely cosmetic feature for the process list
383+ if (\realpath ($ this ->which ('php ' )) === $ binary ) {
384+ $ binary = 'php ' ; // @codeCoverageIgnore
385+ }
386+
387+ return $ binary ;
388+ }
363389}
0 commit comments