-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.php
106 lines (96 loc) · 2.95 KB
/
test.php
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
<?php
use Rx\Observable;
use Rx\React\FromFileObservable;
use Rx\Thruway\Client;
require __DIR__ . '/vendor/autoload.php';
$historyFile = ".history";
$client = new Client($argv[1], $argv[2]);
$router = new Clue\Commander\Router();
$disposable = new \Rx\Disposable\SerialDisposable();
$loop = \EventLoop\getLoop();
//$router->add('exit [<code>]', function (array $args) {
// exit(isset($args['code']) ? (int)$args['code'] : 0);
//});
//
//$router->add('help', function () use ($router) {
// echo 'Usage:' . PHP_EOL;
// foreach ($router->getRoutes() as $route) {
// echo ' ' . $route . PHP_EOL;
// }
//});
//
//$router->add('publish [<uri>] [<value>]', function (array $args) use ($client) {
// $client->publish($args['uri'], $args['value']);
//});
//
//$router->add('call [<uri>] [<value>...]', function (array $args) use ($client) {
//
// //temp until Tokenizer bug fixed
// if (!isset($args['uri']) || $args['value']) {
// echo "Invalid Arguments for command 'call'", PHP_EOL;
// return;
// }
//
// return $client->call($args['uri'], $args['value'])->pluck(0)->pluck(0);
//
//});
//
//$router->add('topic <uri>', function (array $args) use ($client) {
// return $client->topic($args['uri'])->pluck(0)->pluck(0);
//});
//
//$router->add('cancel', function () use ($disposable) {
// $disposable->dispose();
//});
//
//echo 'Thruway CLI: type help to see a list commands.' . PHP_EOL;
//
$readline = new Clue\React\Readline\Readline($loop, 'thruway> ');
//$readline->setAutocompleteWords(array_map(function ($route) {
// return explode(' ', $route)[0];
//}, $router->getRoutes()));
//
$history = new React\Stream\Stream(fopen($historyFile, 'a'), $loop);
$readline->on('line', function ($line) use ($readline, $router, $disposable, $history) {
if ($line === 'quit' || $line === 'exit') {
$readline->pause();
return;
}
if ($line === '') {
return;
}
try {
$readline->addHistory($line);
$history->write($line . "\n");
$args = Clue\Arguments\split($line);
$result = $router->handleArgs($args);
if ($result instanceof Observable) {
$subscription = $result->subscribeCallback(
function ($r) {
echo json_encode($r);
},
function (Exception $e) {
echo $e->getMessage(), PHP_EOL;
});
$disposable->setDisposable($subscription);
}
} catch (Exception $e) {
echo $e->getMessage(), PHP_EOL;
}
});
//
////Reload history from disk
//(new FromFileObservable($historyFile))
// ->cut()
// ->filter(function ($line) {
// //don't include blank lines
// return $line !== '';
// })
// ->subscribeCallback(
// function ($line) use ($readline) {
// $readline->addHistory($line);
// },
// function (Exception $e) {
// echo $e->getMessage();
// }
// );