1. Go to this page and download the library: Download symbiotic/event-contracts 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/ */
symbiotic / event-contracts example snippets
use \Psr\EventDispatcher\ListenerProviderInterface;
interface ListenersInterface extends ListenerProviderInterface
{
/**
* @param string $event the class name or an arbitrary event name
* (with an arbitrary name, you need a custom dispatcher not for PSR)
*
* @param \Closure|string $handler function or class name of the handler
* The event handler class must implement the handle method (...$params) or __invoke(...$params)
* Important! When adding listeners as class names, you will need to adapt them to \Closure
* when you return them in the getListenersForEvent() method!!!
*
* @return void
*/
public function add(string $event, $handler): void;
}
$listenerResolver = function($listener) {
return function(object $event) use ($listener) {
// if classname
if(is_string($listener) && class_exists($listener)) {
$listener = new $listener();
return $listener->handle($event);
} elseif(is_callable($listener)) {
return $listener($event);
}
};
};
// You can implement your wrapper directly in the getListenersForEvent() method or throw a resolver with a PSR container
class ListenerProvider implements Symbiotic\Event\ListenersInterface
{
protected $listenerResolver;
protected $listeners = [];
public function __construct(\Closure $listenerResolver = null)
{
$this->listenerResolver = $listenerResolver;
}
public function add(string $event, $handler): void
{
$this->listeners[$event][] = $handler;
}
public function getListenersForEvent(object $event): iterable
{
$parents = \class_parents($event);
$implements = \class_implements($event);
$classes = array_merge([\get_class($event)], $parents ?: [], $implements ?: []);
$listeners = [];
foreach ($classes as $v) {
$listeners = array_merge($listeners, isset($this->listeners[$v]) ? $this->listeners[$v] : []);
}
$wrapper = $this->listenerResolver;
return $wrapper ? array_map(function ($v) use ($wrapper) {
return $wrapper($v);
}, $listeners) : $listeners;
}
}
/**
* use resolver
**/
$listenersProvider = new \Symbiotic\Event\ListenerProvider($listenerResolver);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.