1. Go to this page and download the library: Download solventt/event-dispatcher library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
public function __construct(private EventDispatcherInterface $dispatcher) {}
...
$this->dispatcher->on(FirstEvent::class, new InvokableClass());
$this->dispatcher->on(SecondEvent::class, [ArrayCallable::class, 'test']);
$this->dispatcher->on(ThirdEvent::class, function (ThirdEvent $event): void {});
$this->dispatcher->on(FirstEvent::class, new InvokableClass(), 4);
$this->dispatcher->off(FirstEvent::class, new InvokableClass());
$this->dispatcher->off(SecondEvent::class, [ArrayCallable::class, 'test']);
$provider = new ReflectionListenerProvider([
new InvokableClass(),
[ArrayCallable::class, 'test'],
[new ArrayCallable(), 'test2'],
'usualFunc',
$someClosure
]);
$dispatcher = new EventDispatcher($provider);
$dispatcher->dispatch(new FirstEvent());
...
[
[new InvokableClass(), 0],
[[ArrayCallable::class, 'test'], 2],
[new ArrayCallable(), 'test2'],
['usualFunc', 3], // will be executed the first
$someClosure // 0 - a default priority, if it isn't specified explicitly
]
...
public function __construct(private EventDispatcherInterface $dispatcher) {}
...
$this->dispatcher->add(new InvokableClass());
$this->dispatcher->add([ArrayCallable::class, 'test']);
$this->dispatcher->add(function (ThirdEvent $event): void {});
public function __construct(private EventDispatcherInterface $dispatcher) {}
...
$this->dispatcher->defer(new FirstEvent()); // no listeners are called;
$this->dispatcher->defer(new SecondEvent()); // no listeners are called;
$this->dispatcher->dispatchDeferredEvents(); // FirstEvent and SecondEvent are dispatched
// Listeners callbacks
public function noParameters(): void {}
public function moreThanOneParameter(FirstEvent $event, string $name): void {}
public function undefinedParameterType($event): void {}
public function noReturnType(object $event) {}
public function wrongReturnType(FirstEvent $event): string {}
$provider = new ContainerListenerProvider($container, 'someDefinitionName', false);
...
$provider = new ClassicListenerProvider([...definition...], false);
return [
'eventsToListeners' => [
FirstEvent::class => [InvokableListenerOne::class], // supports switching off the listener constraints
InvokableListenerTwo::class, // doesn't support switching off the listener constraints
]
];
class FirstEvent implements StoppableEventInterface
{
public string $result = '';
public function isPropagationStopped(): bool
{
return (bool) preg_match('/stop/', $this->result);
}
}
public function __invoke(FirstEvent $event): void
{
$event->result = 'First';
}
...
public function __invoke(FirstEvent $event): void
{
$event->result .= '-stop';
}
...
public function __invoke(FirstEvent $event): void
{
$event->result .= '-Test';
}
return [
'eventsToListeners' => [
FirstEvent::class => [
InvokableListenerOne::class,
InvokableListenerTwo::class, // adds the '-stop' string in the $result property of FirstEvent
InvokableListenerThree::class, // will not be executed
],
],
EventDispatcherInterface::class => function (ContainerInterface $container) {
$provider = new ContainerListenerProvider($container);
return new EventDispatcher($provider);
}
];
$definition = [
FirstEvent::class => [
new InvokableClass(),
[ArrayCallable::class, 'test'],
]
];
$provider = new ClassicListenerProvider(new ArrayIterator($definition));
class ListenerIterator extends ArrayIterator
{
public function __construct()
{
parent::__construct($this->getListeners());
}
private function getListeners(): array
{
return [
FirstEvent::class => [
new InvokableClass(),
[[ArrayCallable::class, 'test'], 2],
],
SecondEvent::class => [
'usualFunc',
[new ArrayCallable(), 'test2']
]
];
}
}
$provider = new ClassicListenerProvider(new ListenerIterator());
// php 7.4+
composer r
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.