PHP code example of yiisoft / event-dispatcher

1. Go to this page and download the library: Download yiisoft/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/ */

    

yiisoft / event-dispatcher example snippets


// Add some listeners.
$listeners = (new \Yiisoft\EventDispatcher\Provider\ListenerCollection())
    ->add(function (AfterDocumentProcessed $event) {
        $document = $event->getDocument();
        // Do something with document.
    });

$provider = new Yiisoft\EventDispatcher\Provider\Provider($listeners);
$dispatcher = new Yiisoft\EventDispatcher\Dispatcher\Dispatcher($provider);

use Psr\EventDispatcher\EventDispatcherInterface;

final class DocumentProcessor
{
    private EventDispatcherInterface $eventDispatcher;
    
    public function __construct(EventDispatcherInterface $eventDispatcher) {
        $this->eventDispatcher = $eventDispatcher;
    }

    public function process(Document $document): void
    {
        // Process the document, then dispatch completion event.
        $this->eventDispatcher->dispatch(new AfterDocumentProcessed($document));
    }
}

final class BusyEvent implements Psr\EventDispatcher\StoppableEventInterface
{
    // ...

    public function isPropagationStopped(): bool
    {
        return true;
    }
}

interface DocumentEvent
{
}

final class BeforeDocumentProcessed implements DocumentEvent
{
}

final class AfterDocumentProcessed implements DocumentEvent
{
}

$listeners->add(static function (DocumentEvent $event) {
    // log events here
});

$compositeProvider = new Yiisoft\EventDispatcher\Provider\CompositeProvider();
$provider = new Yiisoft\EventDispatcher\Provider\Provider($listeners);
$compositeProvider->add($provider);
$compositeProvider->add(new class implements ListenerProviderInterface {
    public function getListenersForEvent(object $event): iterable
    {
        yield static function ($event) {
            // handle 
        };
    }
});

$dispatcher = new Yiisoft\EventDispatcher\Dispatcher\Dispatcher($compositeProvider);

$listeners = (new \Yiisoft\EventDispatcher\Provider\ListenerCollection())
    ->add(static function () {
    // this function does not need an event object as argument
}, SomeEvent::class);

use Yiisoft\EventDispatcher\Dispatcher\CompositeDispatcher;
use Yiisoft\EventDispatcher\Dispatcher\Dispatcher;
use Yiisoft\EventDispatcher\Provider\ListenerCollection;
use Yiisoft\EventDispatcher\Provider\Provider;

// Add some listeners.
$listeners1 = (new ListenerCollection())
    ->add(function (AfterDocumentProcessed $event) {
        $document = $event->getDocument();
        // Do something with document.
    });

$provider1 = new Provider($listeners1);

// Add some listeners.
$listeners2 = (new ListenerCollection())
    ->add(function (AfterDocumentProcessed $event) {
        $document = $event->getDocument();
        // Do something with document.
    });

$provider2 = new Provider($listeners2);


$dispatcher = new CompositeDispatcher();
$dispatcher->attach(new Dispatcher($provider1));
$dispatcher->attach(new Dispatcher($provider2));

$dispatcher->dispatch(new MyEvent());