Skip to content

Commit a36e607

Browse files
Anton Shaboutazloyuser
Anton Shabouta
authored andcommitted
Handler wrappers
1 parent 3925564 commit a36e607

25 files changed

+516
-706
lines changed

composer.json

+3-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
],
1818
"require": {
1919
"php": "^7.1",
20-
"amphp/amp": "^2.0"
20+
"amphp/amp": "^2.0",
21+
"psr/container": "^1.0"
2122
},
2223
"require-dev": {
2324
"amphp/parallel-functions": "^0.1.3",
@@ -29,10 +30,7 @@
2930
"autoload": {
3031
"psr-4": {
3132
"PHPinnacle\\Ensign\\": "src"
32-
},
33-
"files": [
34-
"src/functions.php"
35-
]
33+
}
3634
},
3735
"autoload-dev": {
3836
"psr-4": {

composer.lock

+50-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/concurent.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?php
22

33
use Amp\Delayed;
4-
use PHPinnacle\Ensign\Dispatcher;
4+
use PHPinnacle\Ensign\DispatcherBuilder;
55

66
require __DIR__ . '/../vendor/autoload.php';
77

88
Amp\Loop::run(function () {
9-
$dispatcher = new Dispatcher();
10-
$dispatcher
9+
$builder = new DispatcherBuilder;
10+
$builder
1111
->register('emit', function (string $string, int $num, int $delay = 100) {
1212
for ($i = 0; $i < $num; $i++) {
1313
echo $string;
@@ -19,6 +19,8 @@
1919
})
2020
;
2121

22+
$dispatcher = $builder->build();
23+
2224
$times = \rand(5, 10);
2325
$actionOne = $dispatcher->dispatch('emit', '-', $times, 100);
2426
$actionTwo = $dispatcher->dispatch('emit', '+', $times + \rand(5, 10), 100);

examples/emit.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Amp\Delayed;
44
use PHPinnacle\Ensign\Dispatcher;
5+
use PHPinnacle\Ensign\DispatcherBuilder;
56

67
require __DIR__ . '/../vendor/autoload.php';
78

@@ -28,8 +29,8 @@ public function __construct(int $num, int $delay = 100)
2829
}
2930

3031
Amp\Loop::run(function () {
31-
$dispatcher = new Dispatcher();
32-
$dispatcher
32+
$builder = new DispatcherBuilder;
33+
$builder
3334
->register(SimpleCommand::class, function (SimpleCommand $cmd) {
3435
yield new Delayed($cmd->delay); // Just do some heavy calculations
3536
yield SimpleEvent::class => new SimpleEvent($cmd->num + $cmd->num);
@@ -44,6 +45,8 @@ public function __construct(int $num, int $delay = 100)
4445
})
4546
;
4647

48+
$dispatcher = $builder->build();
49+
4750
$data = yield $dispatcher->dispatch(new SimpleCommand(10));
4851

4952
echo \sprintf('Action resolved with value: %d at %s' . \PHP_EOL, $data, \microtime(true));

examples/events.php

+8-52
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,28 @@
11
<?php
22

33
use Amp\Delayed;
4-
use PHPinnacle\Ensign\Dispatcher;
5-
use PHPinnacle\Ensign\Processor;
4+
use PHPinnacle\Ensign\DispatcherBuilder;
65

76
require __DIR__ . '/../vendor/autoload.php';
87

9-
class Publish
10-
{
11-
public $signal;
12-
public $arguments;
13-
14-
public function __construct(string $signal, ...$arguments)
15-
{
16-
$this->signal = $signal;
17-
$this->arguments = $arguments;
18-
}
19-
}
20-
21-
class Publisher
22-
{
23-
private $processor;
24-
private $listeners = [];
25-
26-
public function __construct(Processor $processor)
27-
{
28-
$this->processor = $processor;
29-
}
30-
31-
public function listen(string $signal, callable $listener): self
32-
{
33-
$this->listeners[$signal][] = $listener;
34-
35-
return $this;
36-
}
37-
38-
public function __invoke(Publish $message)
39-
{
40-
$actions = \array_map(function ($listener) use ($message) {
41-
return $this->processor->execute($listener, $message->arguments);
42-
}, $this->listeners[$message->signal] ?? []);
43-
44-
yield $actions;
45-
}
46-
}
47-
488
Amp\Loop::run(function () {
49-
$processor = new Processor();
50-
$publisher = new Publisher($processor);
51-
$publisher
52-
->listen('print', function ($num) {
9+
$builder = new DispatcherBuilder;
10+
$builder
11+
->register('print', function ($num) {
5312
for ($i = 0; $i < $num; $i++) {
5413
echo '-';
5514

5615
yield new Delayed(100);
5716
}
5817
})
59-
->listen('print', function ($num) {
18+
->register('print', function ($num) {
6019
for ($i = 0; $i < $num; $i++) {
6120
echo '+';
6221

6322
yield new Delayed(100);
6423
}
6524
})
66-
->listen('print', function ($num) {
25+
->register('print', function ($num) {
6726
for ($i = 0; $i < $num; $i++) {
6827
echo '*';
6928

@@ -72,10 +31,7 @@ public function __invoke(Publish $message)
7231
})
7332
;
7433

75-
$dispatcher = new Dispatcher($processor);
76-
$dispatcher
77-
->register(Publish::class, $publisher)
78-
;
34+
$dispatcher = $builder->build();
7935

80-
yield $dispatcher->dispatch(new Publish('print', 10));
36+
yield $dispatcher->dispatch('print', 10);
8137
});

examples/parallel.php

-31
This file was deleted.

examples/pcntl.php

-21
This file was deleted.

examples/ping-pong.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
use PHPinnacle\Ensign\Dispatcher;
3+
use PHPinnacle\Ensign\DispatcherBuilder;
44

55
require __DIR__ . '/../vendor/autoload.php';
66

@@ -23,8 +23,8 @@ class Stop
2323
}
2424

2525
Amp\Loop::run(function () {
26-
$dispatcher = new Dispatcher();
27-
$dispatcher
26+
$builder = new DispatcherBuilder;
27+
$builder
2828
->register(Ping::class, function (Ping $cmd) {
2929
if ($cmd->times > 0) {
3030
$cmd->times--;
@@ -46,5 +46,7 @@ class Stop
4646
})
4747
;
4848

49+
$dispatcher = $builder->build();
50+
4951
yield $dispatcher->dispatch(new Ping(\rand(2, 10)));
5052
});

examples/process.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
use Amp\Delayed;
44
use PHPinnacle\Ensign\Dispatcher;
5+
use PHPinnacle\Ensign\HandlerRegistry;
56

67
require __DIR__ . '/../vendor/autoload.php';
78

89
Amp\Loop::run(function () {
9-
$dispatcher = new Dispatcher();
10-
$dispatcher
11-
->register('spawn', function (callable $process) {
10+
$handlers = new HandlerRegistry;
11+
$handlers
12+
->add('spawn', function (callable $process) {
1213
static $pid = 1;
1314

1415
$gen = $process($pid);
@@ -17,18 +18,20 @@
1718

1819
return $pid++;
1920
})
20-
->register('send', function (int $pid, string $message, ...$arguments) {
21+
->add('send', function (int $pid, string $message, ...$arguments) {
2122
$signal = sprintf('%s.%d', $message, $pid);
2223

2324
return yield $signal => $arguments;
2425
})
25-
->register('receive', function (int $pid, string $message, callable $handler) use ($dispatcher) {
26+
->add('receive', function (int $pid, string $message, callable $handler) use ($handlers) {
2627
$signal = sprintf('%s.%d', $message, $pid);
2728

28-
$dispatcher->register($signal, $handler);
29+
$handlers->add($signal, $handler);
2930
})
3031
;
3132

33+
$dispatcher = new Dispatcher($handlers);
34+
3235
$receiver = yield $dispatcher->dispatch('spawn', function (int $pid) {
3336
yield 'receive' => [$pid, 'ping', function () {
3437
echo 'Ping!' . \PHP_EOL;

0 commit comments

Comments
 (0)