66use EProcess \MessengerFactory ;
77
88use React \ChildProcess \Process ;
9- use React \EventLoop \LoopInterface ;
109use Symfony \Component \Process \PhpExecutableFinder ;
1110
12- class ChildProcess
11+ class ChildProcess extends BaseAdapter
1312{
1413 use UniversalSerializer;
1514
@@ -49,26 +48,19 @@ class ChildProcess
4948
5049 private $ loop ;
5150 private $ process ;
52- private $ executableFinder ;
53-
54- public function __construct (LoopInterface $ loop )
55- {
56- $ this ->loop = $ loop ;
57- $ this ->executableFinder = new PhpExecutableFinder ();
58- }
5951
6052 public function create ($ class , array $ data = [])
6153 {
62- if (false === $ php = $ this ->executableFinder ->find ()) {
54+ $ executableFinder = new PhpExecutableFinder ();
55+
56+ if (false === $ php = $ executableFinder ->find ()) {
6357 throw new \RuntimeException ('Unable to find the PHP executable. ' );
6458 }
6559
66- $ node = uniqid ('thread_ ' );
67- $ unix = sprintf ('unix://tmp/%s.sock ' , $ node );
68-
60+ $ unix = $ this ->createUnixSocket ();
6961 $ messenger = MessengerFactory::server ($ unix , $ this ->loop );
7062
71- $ file = sprintf (__DIR__ . '/../../tmp/%s.php ' , $ node );
63+ $ file = sprintf (__DIR__ . '/../../tmp/%s.php ' , $ this -> node );
7264
7365 file_put_contents ($ file , sprintf (
7466 $ this ->script ,
@@ -81,7 +73,7 @@ public function create($class, array $data = [])
8173 $ this ->process = new Process (sprintf ('exec %s %s ' , $ php , realpath ($ file )));
8274 $ this ->process ->start ($ this ->loop );
8375
84- $ this ->loop ->addTimer (5 , function () use ($ file ) {
76+ $ this ->loop ->addTimer (3 , function () use ($ file ) {
8577 unlink ($ file );
8678 });
8779
@@ -93,10 +85,6 @@ public function create($class, array $data = [])
9385 echo $ data ;
9486 });
9587
96- register_shutdown_function (function () use ($ unix ) {
97- unlink ($ unix );
98- });
99-
10088 return $ messenger ;
10189 }
10290
0 commit comments