forked from clue/reactphp-zenity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseZen.php
More file actions
86 lines (72 loc) · 2.18 KB
/
BaseZen.php
File metadata and controls
86 lines (72 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
namespace Clue\React\Zenity\Zen;
use React\Promise\PromisorInterface;
use React\ChildProcess\Process;
use React\Promise\Deferred;
class BaseZen implements PromisorInterface
{
protected $promise;
protected $deferred;
protected $process;
/** @internal */
public function go(Process $process)
{
$this->deferred = $deferred = new Deferred();
$this->process = $process;
$buffered = null;
$process->stdout->on('data', function ($data) use (&$buffered) {
if ($data !== '') {
$buffered .= $data;
}
});
$process->on('exit', function($code) use ($deferred) {
if ($code !== 0) {
$deferred->reject($code);
} else {
$deferred->resolve();
}
});
$that = $this;
$this->promise = $deferred->promise()->then(function () use (&$buffered, $that) {
if ($buffered === null) {
$buffered = true;
} else {
$buffered = $that->parseValue(trim($buffered));
}
return $buffered;
});
}
public function promise()
{
return $this->promise;
}
public function close()
{
// resolve with whatever is currently buffered
$this->deferred->resolve();
if ($this->process !== null && $this->process->isRunning()) {
$this->process->terminate(defined('SIGKILL') ? SIGKILL : null);
// explicitly close all streams immediately
$this->process->stdin->close();
$this->process->stdout->close();
$this->process->stderr->close();
}
return $this;
}
protected function writeln($line)
{
if ($this->process === null) return;
$this->process->stdin->write($line . PHP_EOL);
}
/**
* Parse the given value from the dialog to the appropriate return value for this dialog
*
* @param string $value The raw value received from the dialog window
* @return mixed The logical value presented to the user
* @internal
*/
public function parseValue($value)
{
return $value;
}
}