forked from clue/reactphp-zenity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLauncher.php
More file actions
147 lines (120 loc) · 4.1 KB
/
Launcher.php
File metadata and controls
147 lines (120 loc) · 4.1 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
namespace Clue\React\Zenity;
use React\EventLoop\LoopInterface;
use Icecave\Mephisto\Factory\ProcessFactory;
use Icecave\Mephisto\Launcher\CommandLineLauncher;
use Icecave\Mephisto\Process\ProcessInterface;
use Clue\React\Zenity\Dialog\AbstractDialog;
use React\Promise\Deferred;
/**
*
* @link https://github.com/clue/zenity-react
* @link https://help.gnome.org/users/zenity/stable/index.html.en
*/
class Launcher
{
private $loop;
private $processLauncher;
private $bin = 'zenity';
public function __construct(LoopInterface $loop, CommandLineLauncher $processLauncher = null)
{
if ($processLauncher === null) {
$processFactory = new ProcessFactory($loop);
$processLauncher = new CommandLineLauncher($processFactory);
}
$this->processLauncher = $processLauncher;
$this->loop = $loop;
}
public function setBin($bin)
{
$this->bin = $bin;
return $this;
}
public function launch(AbstractDialog $dialog)
{
$process = $this->createProcess($dialog);
$inbuffer = $dialog->getInBuffer();
if ($inbuffer !== null) {
$process->inputStream()->write($inbuffer);
}
$deferred = new Deferred();
$result = null;
$process->outputStream()->on('data', function ($data) use (&$result) {
if ($data !== '') {
$result .= $data;
}
});
$zen = $dialog->createZen($deferred, $process);
$process->outputStream()->on('end', function() use ($process, $zen, &$result, $dialog, $deferred) {
$code = $process->status()->exitCode();
if ($code !== 0) {
$deferred->reject($code);
} else {
if ($result === null) {
$result = true;
} else {
$result = $dialog->parseValue(trim($result));
}
$deferred->resolve($result);
}
$zen->close();
});
return $zen;
}
/**
* Block while waiting for the given dialog to return
*
* If the dialog is already closed, this returns immediately, without doing
* much at all. If the dialog is not yet opened, it will be opened and this
* method will wait for the dialog to be handled (i.e. either completed or
* closed). Clicking "ok" will result in a boolean true value, clicking
* "cancel" or hitten escape key will or running into a timeout will result
* in a boolean false. For all other input fields, their respective (parsed)
* value will be returned.
*
* For this to work, this method will temporarily start the event loop and
* stop it afterwards. Thus, it is *NOT* a good idea to mix this if anything
* else is listening on the event loop. The recommended way in this case is
* to avoid using this blocking method call and go for a fully async
* `self::then()` instead.
*
* @param AbstractDialog $dialog
* @return boolean|string dialog return value
* @uses Launcher::waitFor()
*/
public function waitFor(AbstractDialog $dialog)
{
$done = false;
$ret = null;
$loop = $this->loop;
$process = $this->launch($dialog);
$process->then(function ($result) use (&$ret, &$done, $loop) {
$ret = $result;
$done = true;
$loop->stop();
}, function () use (&$ret, &$done, $loop) {
$ret = false;
$done = true;
$loop->stop();
});
if (!$done) {
$loop->run();
}
return $ret;
}
private function createProcess(AbstractDialog $dialog)
{
return $this->run($dialog->getArgs());
}
private function run(array $args = array())
{
$command = $this->bin;
foreach ($args as $value) {
$command .= ' ' . escapeshellarg($value);
}
// var_dump($command);
$process = $this->processLauncher->runCommandLine($command);
/* @var $process ProcessInterface */
return $process;
}
}