@@ -11,18 +11,10 @@ class Process
11
11
{
12
12
private const MAX_INT = 2147483647 ;
13
13
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 )
19
15
{
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
26
18
}
27
19
}
28
20
@@ -31,6 +23,7 @@ public function __construct()
31
23
*/
32
24
public function send (Closure $ asyncFunction ): void
33
25
{
26
+ $ separator = DIRECTORY_SEPARATOR ;
34
27
$ serialized = serialize (new SerializableClosure ($ asyncFunction ));
35
28
$ serializedLength = strlen ($ serialized );
36
29
$ shmopInstance = shmop_open ($ this ->key , 'c ' , 0660 , $ serializedLength );
@@ -46,9 +39,14 @@ public function send(Closure $asyncFunction): void
46
39
$ serializedLength . '. Bytes written: ' . $ bytesWritten );
47
40
}
48
41
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 & ' );
53
51
}
54
52
}
0 commit comments