1. Go to this page and download the library: Download onoi/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/ */
onoi / event-dispatcher example snippets
class BarListener implements EventListener {
public function execute( DispatchContext $dispatchContext = null ) {
// Do something
}
public function isPropagationStopped() {
return false;
}
}
class ListenerCollectionRegistry implements EventListenerCollection {
private $eventListenerCollection;
public function __construct( EventListenerCollection $eventListenerCollection ) {
$this->eventListenerCollection = $eventListenerCollection;
}
public function getCollection() {
return $this->addToListenerCollection()->getCollection();
}
private function addToListenerCollection() {
$this->eventListenerCollection->registerCallback( 'do.something', function() {
// Do something
} );
$this->eventListenerCollection->registerListener( 'notify.bar', new BarListener() );
return $this->eventListenerCollection;
}
}
$eventDispatcherFactory = new EventDispatcherFactory();
$listenerCollectionRegistry = new ListenerCollectionRegistry(
$eventDispatcherFactory->newGenericEventListenerCollection()
);
$eventDispatcher = $eventDispatcherFactory->newGenericEventDispatcher();
$eventDispatcher->addListenerCollection( $listenerCollectionRegistry );
class Foo {
use EventDispatcherAwareTrait;
public function doSomething() {
// No context
$this->eventDispatcher->dispatch( 'do.something' );
$dispatchContext = new DispatchContext();
$dispatchContext->set( 'dosomethingelse', new \stdClass );
// Using `DispatchContext`
$this->eventDispatcher->dispatch( 'notify.bar', $dispatchContext );
// Using an array as context which is later converted into
// a `DispatchContext`
$this->eventDispatcher->dispatch( 'notify.foo', [ 'Bar' => 123 ] );
}
}
$instance = new Foo();
$instance->setEventDispatcher( $eventDispatcher );
$instance->doSomething();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.