Skip to content

Commit ecdd633

Browse files
committed
Remove hacks, pass script directly to stdin
1 parent 4baa105 commit ecdd633

File tree

5 files changed

+60
-16
lines changed

5 files changed

+60
-16
lines changed

EProcess/Adapter/BaseAdapter.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace EProcess\Adapter;
44

5-
use EProcess\Terminator;
65
use React\EventLoop\LoopInterface;
76

87
abstract class BaseAdapter

EProcess/Adapter/ChildProcess.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,22 @@ public function create($class, array $data = [])
5959
$unix = $this->createUnixSocket();
6060
$messenger = MessengerFactory::server($unix, $this->loop);
6161

62-
$file = sprintf(__DIR__ . '/../../tmp/%s.php', $this->node);
63-
64-
file_put_contents($file, sprintf(
62+
$script = sprintf(
6563
$this->script,
6664
EPROCESS_AUTOLOAD,
6765
$unix,
6866
$class,
6967
base64_encode($this->serialize($data))
70-
));
68+
);
7169

72-
$this->process = new Process(sprintf('exec %s %s', $php, realpath($file)));
73-
$this->process->start($this->loop);
70+
$this->process = new Process($php);
71+
$this->process->start($this->loop, 0.1);
72+
73+
$this->process->stdin->resume();
74+
$this->process->stdin->write($script);
7475

75-
$this->loop->addTimer(1, function() use ($file) {
76-
unlink($file);
76+
$this->process->stdin->on('full-drain', function() {
77+
$this->process->stdin->close();
7778
});
7879

7980
$this->process->stdout->on('data', function($data) {

EProcess/Adapter/SymfonyProcess.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ class SymfonyProcess extends BaseAdapter
4444
}
4545
PHP;
4646

47-
private $loop;
4847
private $process;
4948

5049
public function create($class, array $data = [])

EProcess/Stream/FullDrainSteam.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace EProcess\Stream;
4+
5+
use React\Stream\Stream;
6+
use React\EventLoop\LoopInterface;
7+
use React\Stream\Buffer;
8+
9+
class FullDrainStream extends Stream
10+
{
11+
12+
public function __construct($stream, LoopInterface $loop)
13+
{
14+
$this->stream = $stream;
15+
if (!is_resource($this->stream) || get_resource_type($this->stream) !== "stream") {
16+
throw new \InvalidArgumentException('First parameter must be a valid stream resource');
17+
}
18+
19+
stream_set_blocking($this->stream, 0);
20+
21+
// Use unbuffered read operations on the underlying stream resource.
22+
// Reading chunks from the stream may otherwise leave unread bytes in
23+
// PHP's stream buffers which some event loop implementations do not
24+
// trigger events on (edge triggered).
25+
// This does not affect the default event loop implementation (level
26+
// triggered), so we can ignore platforms not supporting this (HHVM).
27+
if (function_exists('stream_set_read_buffer')) {
28+
stream_set_read_buffer($this->stream, 0);
29+
}
30+
31+
$this->loop = $loop;
32+
$this->buffer = new Buffer($this->stream, $this->loop);
33+
34+
$that = $this;
35+
36+
$this->buffer->on('error', function ($error) use ($that) {
37+
$that->emit('error', array($error, $that));
38+
$that->close();
39+
});
40+
41+
$this->buffer->on('drain', function () use ($that) {
42+
$that->emit('drain', array($that));
43+
});
44+
45+
$this->buffer->on('full-drain', function () use ($that) {
46+
$that->emit('full-drain', array($that));
47+
});
48+
49+
$this->resume();
50+
}
51+
}

README.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,3 @@ ApplicationFactory::launch(Main::class);
7171
```
7272

7373
You need to have proper autoloading established in order to use this example.
74-
75-
ToDo
76-
=======
77-
78-
* Refactor adapters, remove hacks, pass php code directly with stdin instead of file.
79-
* Can IPC be done without unix sockets or using only single socket?

0 commit comments

Comments
 (0)