Skip to content

Commit

Permalink
Handler wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Shabouta authored and zloyuser committed Dec 27, 2018
1 parent 3925564 commit a36e607
Show file tree
Hide file tree
Showing 25 changed files with 516 additions and 706 deletions.
8 changes: 3 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
],
"require": {
"php": "^7.1",
"amphp/amp": "^2.0"
"amphp/amp": "^2.0",
"psr/container": "^1.0"
},
"require-dev": {
"amphp/parallel-functions": "^0.1.3",
Expand All @@ -29,10 +30,7 @@
"autoload": {
"psr-4": {
"PHPinnacle\\Ensign\\": "src"
},
"files": [
"src/functions.php"
]
}
},
"autoload-dev": {
"psr-4": {
Expand Down
51 changes: 50 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions examples/concurent.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

use Amp\Delayed;
use PHPinnacle\Ensign\Dispatcher;
use PHPinnacle\Ensign\DispatcherBuilder;

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

Amp\Loop::run(function () {
$dispatcher = new Dispatcher();
$dispatcher
$builder = new DispatcherBuilder;
$builder
->register('emit', function (string $string, int $num, int $delay = 100) {
for ($i = 0; $i < $num; $i++) {
echo $string;
Expand All @@ -19,6 +19,8 @@
})
;

$dispatcher = $builder->build();

$times = \rand(5, 10);
$actionOne = $dispatcher->dispatch('emit', '-', $times, 100);
$actionTwo = $dispatcher->dispatch('emit', '+', $times + \rand(5, 10), 100);
Expand Down
7 changes: 5 additions & 2 deletions examples/emit.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Amp\Delayed;
use PHPinnacle\Ensign\Dispatcher;
use PHPinnacle\Ensign\DispatcherBuilder;

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

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

Amp\Loop::run(function () {
$dispatcher = new Dispatcher();
$dispatcher
$builder = new DispatcherBuilder;
$builder
->register(SimpleCommand::class, function (SimpleCommand $cmd) {
yield new Delayed($cmd->delay); // Just do some heavy calculations
yield SimpleEvent::class => new SimpleEvent($cmd->num + $cmd->num);
Expand All @@ -44,6 +45,8 @@ public function __construct(int $num, int $delay = 100)
})
;

$dispatcher = $builder->build();

$data = yield $dispatcher->dispatch(new SimpleCommand(10));

echo \sprintf('Action resolved with value: %d at %s' . \PHP_EOL, $data, \microtime(true));
Expand Down
60 changes: 8 additions & 52 deletions examples/events.php
Original file line number Diff line number Diff line change
@@ -1,69 +1,28 @@
<?php

use Amp\Delayed;
use PHPinnacle\Ensign\Dispatcher;
use PHPinnacle\Ensign\Processor;
use PHPinnacle\Ensign\DispatcherBuilder;

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

class Publish
{
public $signal;
public $arguments;

public function __construct(string $signal, ...$arguments)
{
$this->signal = $signal;
$this->arguments = $arguments;
}
}

class Publisher
{
private $processor;
private $listeners = [];

public function __construct(Processor $processor)
{
$this->processor = $processor;
}

public function listen(string $signal, callable $listener): self
{
$this->listeners[$signal][] = $listener;

return $this;
}

public function __invoke(Publish $message)
{
$actions = \array_map(function ($listener) use ($message) {
return $this->processor->execute($listener, $message->arguments);
}, $this->listeners[$message->signal] ?? []);

yield $actions;
}
}

Amp\Loop::run(function () {
$processor = new Processor();
$publisher = new Publisher($processor);
$publisher
->listen('print', function ($num) {
$builder = new DispatcherBuilder;
$builder
->register('print', function ($num) {
for ($i = 0; $i < $num; $i++) {
echo '-';

yield new Delayed(100);
}
})
->listen('print', function ($num) {
->register('print', function ($num) {
for ($i = 0; $i < $num; $i++) {
echo '+';

yield new Delayed(100);
}
})
->listen('print', function ($num) {
->register('print', function ($num) {
for ($i = 0; $i < $num; $i++) {
echo '*';

Expand All @@ -72,10 +31,7 @@ public function __invoke(Publish $message)
})
;

$dispatcher = new Dispatcher($processor);
$dispatcher
->register(Publish::class, $publisher)
;
$dispatcher = $builder->build();

yield $dispatcher->dispatch(new Publish('print', 10));
yield $dispatcher->dispatch('print', 10);
});
31 changes: 0 additions & 31 deletions examples/parallel.php

This file was deleted.

21 changes: 0 additions & 21 deletions examples/pcntl.php

This file was deleted.

8 changes: 5 additions & 3 deletions examples/ping-pong.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use PHPinnacle\Ensign\Dispatcher;
use PHPinnacle\Ensign\DispatcherBuilder;

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

Expand All @@ -23,8 +23,8 @@ class Stop
}

Amp\Loop::run(function () {
$dispatcher = new Dispatcher();
$dispatcher
$builder = new DispatcherBuilder;
$builder
->register(Ping::class, function (Ping $cmd) {
if ($cmd->times > 0) {
$cmd->times--;
Expand All @@ -46,5 +46,7 @@ class Stop
})
;

$dispatcher = $builder->build();

yield $dispatcher->dispatch(new Ping(\rand(2, 10)));
});
15 changes: 9 additions & 6 deletions examples/process.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

use Amp\Delayed;
use PHPinnacle\Ensign\Dispatcher;
use PHPinnacle\Ensign\HandlerRegistry;

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

Amp\Loop::run(function () {
$dispatcher = new Dispatcher();
$dispatcher
->register('spawn', function (callable $process) {
$handlers = new HandlerRegistry;
$handlers
->add('spawn', function (callable $process) {
static $pid = 1;

$gen = $process($pid);
Expand All @@ -17,18 +18,20 @@

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

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

$dispatcher->register($signal, $handler);
$handlers->add($signal, $handler);
})
;

$dispatcher = new Dispatcher($handlers);

$receiver = yield $dispatcher->dispatch('spawn', function (int $pid) {
yield 'receive' => [$pid, 'ping', function () {
echo 'Ping!' . \PHP_EOL;
Expand Down
Loading

0 comments on commit a36e607

Please sign in to comment.