|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace EProcess\Adapter; |
| 4 | + |
| 5 | +use EProcess\Behaviour\UniversalSerializer; |
| 6 | +use EProcess\MessengerFactory; |
| 7 | + |
| 8 | +use React\ChildProcess\Process; |
| 9 | +use React\EventLoop\LoopInterface; |
| 10 | +use Symfony\Component\Process\PhpExecutableFinder; |
| 11 | + |
| 12 | +class ChildProcess |
| 13 | +{ |
| 14 | + use UniversalSerializer; |
| 15 | + |
| 16 | + private $script = <<<PHP |
| 17 | +<?php |
| 18 | +
|
| 19 | +require_once '%s'; |
| 20 | +
|
| 21 | +set_time_limit(0); |
| 22 | +
|
| 23 | +use EProcess\MessengerFactory; |
| 24 | +use EProcess\Application\ApplicationFactory; |
| 25 | +use React\EventLoop\Factory; |
| 26 | +
|
| 27 | +\$loop = Factory::create(); |
| 28 | +
|
| 29 | +\$messenger = MessengerFactory::client( |
| 30 | + '%s', |
| 31 | + \$loop |
| 32 | +); |
| 33 | +
|
| 34 | +\$application = ApplicationFactory::create('%s'); |
| 35 | +
|
| 36 | +\$application->messenger(\$messenger); |
| 37 | +\$application->loop(\$loop); |
| 38 | +\$application->data(\$application->unserialize(base64_decode('%s'))); |
| 39 | +
|
| 40 | +\$messenger->emit('initialized', true); |
| 41 | +
|
| 42 | +try { |
| 43 | + \$application->run(); |
| 44 | + \$loop->run(); |
| 45 | +} catch (\Exception \$e) { |
| 46 | + echo \$e; |
| 47 | +} |
| 48 | +PHP; |
| 49 | + |
| 50 | + private $loop; |
| 51 | + private $process; |
| 52 | + private $executableFinder; |
| 53 | + |
| 54 | + public function __construct(LoopInterface $loop) |
| 55 | + { |
| 56 | + $this->loop = $loop; |
| 57 | + $this->executableFinder = new PhpExecutableFinder(); |
| 58 | + } |
| 59 | + |
| 60 | + public function create($class, array $data = []) |
| 61 | + { |
| 62 | + if (false === $php = $this->executableFinder->find()) { |
| 63 | + throw new \RuntimeException('Unable to find the PHP executable.'); |
| 64 | + } |
| 65 | + |
| 66 | + $node = uniqid('thread_'); |
| 67 | + $unix = sprintf('unix://tmp/%s.sock', $node); |
| 68 | + |
| 69 | + $messenger = MessengerFactory::server($unix, $this->loop); |
| 70 | + |
| 71 | + $file = sprintf(__DIR__ . '/../../tmp/%s.php', $node); |
| 72 | + |
| 73 | + file_put_contents($file, sprintf( |
| 74 | + $this->script, |
| 75 | + EPROCESS_AUTOLOAD, |
| 76 | + $unix, |
| 77 | + $class, |
| 78 | + base64_encode($this->serialize($data)) |
| 79 | + )); |
| 80 | + |
| 81 | + $this->process = new Process(sprintf('exec %s %s', $php, realpath($file))); |
| 82 | + $this->process->start($this->loop); |
| 83 | + |
| 84 | + $this->loop->addTimer(5, function() use ($file) { |
| 85 | + unlink($file); |
| 86 | + }); |
| 87 | + |
| 88 | + $this->process->stdout->on('data', function($data) { |
| 89 | + echo $data; |
| 90 | + }); |
| 91 | + |
| 92 | + $this->process->stderr->on('data', function($data) { |
| 93 | + echo $data; |
| 94 | + }); |
| 95 | + |
| 96 | + register_shutdown_function(function() use ($unix) { |
| 97 | + unlink($unix); |
| 98 | + }); |
| 99 | + |
| 100 | + return $messenger; |
| 101 | + } |
| 102 | + |
| 103 | + public function kill() |
| 104 | + { |
| 105 | + $this->process->close(); |
| 106 | + } |
| 107 | +} |
0 commit comments