@@ -11,18 +11,10 @@ class Process
1111{
1212 private const MAX_INT = 2147483647 ;
1313
14- private array $ processTemplate = [PHP_BINARY , __DIR__ . DIRECTORY_SEPARATOR . 'background_processor.php ' , '{key} ' ,
15- '{length} ' , '& ' ];
16- private int $ key ;
17-
18- public function __construct ()
14+ public function __construct (private int $ key = 0 )
1915 {
20- $ this ->key = mt_rand (0 , self ::MAX_INT ); // communication key
21- $ this ->processTemplate [2 ] = $ this ->key ;
22-
23- if (PHP_OS_FAMILY === 'Windows ' ) {
24- $ this ->processTemplate = ['start ' , '"" ' , '/B ' , PHP_BINARY , __DIR__ . DIRECTORY_SEPARATOR .
25- 'background_processor.php ' , $ this ->key , '{length} ' ];
16+ if (!$ this ->key ) {
17+ $ this ->key = mt_rand (0 , self ::MAX_INT ); // communication key
2618 }
2719 }
2820
@@ -31,6 +23,7 @@ public function __construct()
3123 */
3224 public function send (Closure $ asyncFunction ): void
3325 {
26+ $ separator = DIRECTORY_SEPARATOR ;
3427 $ serialized = serialize (new SerializableClosure ($ asyncFunction ));
3528 $ serializedLength = strlen ($ serialized );
3629 $ shmopInstance = shmop_open ($ this ->key , 'c ' , 0660 , $ serializedLength );
@@ -46,9 +39,14 @@ public function send(Closure $asyncFunction): void
4639 $ serializedLength . '. Bytes written: ' . $ bytesWritten );
4740 }
4841
49- $ key = array_search ('{length} ' , $ this ->processTemplate );
50- $ this ->processTemplate [$ key ] = $ serializedLength ;
51- $ process = new SymfonyProcess ($ this ->processTemplate );
52- $ process ->start ();
42+ if (PHP_OS_FAMILY === 'Windows ' ) {
43+ $ arg = ['start ' , '"" ' , '/B ' , PHP_BINARY , __DIR__ . $ separator . 'background_processor.php ' , $ this ->key ];
44+ $ process = new SymfonyProcess ($ arg );
45+ $ process ->start ();
46+ return ;
47+ }
48+
49+ exec (PHP_BINARY . __DIR__ . $ separator . 'background_processor.php ' . $ this ->key .
50+ ' > /dev/null 2>&1 & ' );
5351 }
5452}
0 commit comments