-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHandler.php
More file actions
141 lines (110 loc) · 4.04 KB
/
Handler.php
File metadata and controls
141 lines (110 loc) · 4.04 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
<?php
declare(strict_types=1);
namespace Ardillo\Examples\Dns;
use Ardillo\{
TableModel,
TableModelHandler,
TableValue,
TableValueString,
TableValueType,
};
use React\Dns\Config\Config;
use React\Dns\Resolver\Factory;
use React\Promise\Deferred;
use Throwable;
use function React\Promise\all;
class Handler extends TableModelHandler
{
/** @var array<int<0, max>, array<string>> */
protected array $values = [];
public function resolve(TableModel $model, string $host): void
{
assert($this->app instanceof App);
$this->app->win->statusBar->setText("Resolving {$host} ...");
$config = Config::loadSystemConfigBlocking();
if (!$config->nameservers) {
$config->nameservers = [$this->app->nameserver];
}
$factory = new Factory();
$dns = $factory->create($config);
$promises = [];
foreach ($this->app->querySelection as $type => $enabled) {
if (!$enabled) {
continue;
}
$deferred = new Deferred();
$dns->resolveAll($host, $type)
->then(function (array $results) use ($deferred): void {
$deferred->resolve($results);
})
->catch(function (Throwable $t) use ($deferred): void {
$deferred->resolve([]);
});
$promises[$this->app::QUERY_TYPES[$type]] = $deferred->promise();
}
all($promises)
->then(function (array $results) use ($model, $host): void {
$rows = $this->numRowsHandler($model);
for ($i = $rows - 1; $i >= 0; $i--) {
array_pop($this->values);
$model->rowDeleted($i);
}
$i = 0;
foreach ($results as $type => $answers) {
foreach ($answers as $answer) {
if (is_array($answer)) {
foreach ($answer as $k => $v) {
if (is_string($k) && is_string($v)) {
$answer[$k] = "{$k}={$v}";
}
}
$answer = implode('; ', $answer);
}
$this->values[$i] = [(string)$type, (string)$answer];
$model->rowInserted($i++);
}
}
assert($this->app instanceof App);
$this->app->win->statusBar->setText("Done resolving {$host}");
})
->catch(function (Throwable $t): void {
echo 'Error: ' . $t->getMessage() . PHP_EOL;
});
}
public function export(string $path): void
{
assert($this->app instanceof App);
/* Please note the I/O operations against this file are blocking/syncrhonous which is
* contrary to the non-blocking/asynchronous nature of ReactPHP. This is done for
* simplicity and to keep the example code short and concise. */
$fp = fopen($path, 'w');
if (!$fp) {
$this->app->win->statusBar->setText('Cannot open ' . basename($path));
return;
}
foreach ($this->values as $row) {
fputcsv($fp, $row);
}
fclose($fp);
$this->app->win->statusBar->setText('Records exported to ' . basename($path));
}
public function cellValueHandler(TableModel $model, int $row, int $column): TableValue
{
return new TableValueString($this->values[$row][$column] ?? '-');
}
public function columnTypeHandler(TableModel $model, int $column): int
{
return TableValueType::Str;
}
public function numColumnsHandler(TableModel $model): int
{
return 2;
}
public function numRowsHandler(TableModel $model): int
{
return count($this->values);
}
public function setCellValueHandler(TableModel $model, int $row, int $column, ?TableValue $value): void
{
}
}