Commit e1c677fe authored by Avris's avatar Avris

Init

parent c6181688
## Avris Event Dispatcher ##
TODO
### Instalation
composer require avris/dispatcher
### Event Definition
An event is an object that extends `Avris\Dispatcher\Event`.
It has to provide its name (used as an identifier for triggering it)
and a setter and getter for its value (for instance if `RequestEvent` is supposed to produce a `Response`, that response is the value).
For example:
final class RequestEvent extends Event
{
/** @var RequestInterface */
private $request;
/** @var ResponseInterface */
private $response;
public function __construct(RequestInterface $request)
{
$this->request = $request;
}
public function getName(): string
{
return 'request';
}
public function getRequest(): RequestInterface
{
return $this->request;
}
public function getResponse(): ?ResponseInterface
{
return $this->response;
}
public function setResponse(ResponseInterface $response): self
{
$this->response = $response;
return $this;
}
public function setValue($value): Event
{
$this->response = $value;
return $this;
}
public function getValue()
{
return $this->response;
}
}
### Attaching listeners
$dispatcher = new EventDispatcher();
$dispatcher->attachListener('request', function (RequestEvent $event) {
if (substr($event->getRequest()->getUrl(), 0, 6) === '/admin' && !is_admin()) {
return new Response('Forbidden', 403);
}
});
Returning anything other than `null` will automatically set event's value
(you can also do it explicitly with `$event->setResponse($response)`).
As a last argument of `attachListener` you can specify the priority of the listener, for example:
$dispatcher->attachListener('request', [$this, 'one']);
$dispatcher->attachListener('request', [$this, 'two']);
$dispatcher->attachListener('request', [$this, 'three'], -9);
$dispatcher->attachListener('request', [$this, 'four'], 9);
The listeners will be triggered in order: `four`, `one`, `two`, `three`.
To stop propagation of an event, use `$event->stopPropagation()`.
### Triggering events
To trigger an event:
$response = $dispatcher->trigger(new RequestEvent($request));
As you can see, the value of the event after executing the listeners will be returned for convenience
(again, you can explicitly use `$event->getResponse()`).
### Event subscribers
A subscriber is an object that defines event listeners in an elegant manner,
encapsulating possibly multiple listeners related to one service.
It needs to implement `Avris\Dispatcher\EventSubscriberInterface`:
final class FooService implements EventSubscriberInterface
{
public function getSubscribedEvents(): iterable
{
yield 'request' => [$this, 'onRequest'];
yield 'response:9' => [$this, 'onResponse']; // priority 9
}
}
To register them in the dispatcher:
$fooService = new FooService();
$this->dispatcher->registerSubscriber($fooService);
### Copyright ###
......
......@@ -10,6 +10,9 @@
"email": "andre@avris.it",
"homepage": "https://avris.it"
}],
"require": {
"php": "^7.1"
},
"require-dev": {
"phpunit/phpunit": "^6.5",
"symfony/var-dumper": "^4.0"
......
......@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"content-hash": "12a26ef6c2434e45b605989a9f66f620",
"content-hash": "aff98730139d135465ce44a96f10bf93",
"packages": [],
"packages-dev": [
{
......@@ -862,16 +862,16 @@
},
{
"name": "sebastian/comparator",
"version": "2.1.0",
"version": "2.1.1",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/comparator.git",
"reference": "1174d9018191e93cb9d719edec01257fc05f8158"
"reference": "b11c729f95109b56a0fe9650c6a63a0fcd8c439f"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/1174d9018191e93cb9d719edec01257fc05f8158",
"reference": "1174d9018191e93cb9d719edec01257fc05f8158",
"url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/b11c729f95109b56a0fe9650c6a63a0fcd8c439f",
"reference": "b11c729f95109b56a0fe9650c6a63a0fcd8c439f",
"shasum": ""
},
"require": {
......@@ -922,7 +922,7 @@
"compare",
"equality"
],
"time": "2017-11-03T07:16:52+00:00"
"time": "2017-12-22T14:50:35+00:00"
},
{
"name": "sebastian/diff",
......@@ -1653,6 +1653,8 @@
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform": {
"php": "^7.1"
},
"platform-dev": []
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment