PHP code example of psr-discovery / event-dispatcher-implementations

1. Go to this page and download the library: Download psr-discovery/event-dispatcher-implementations 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/ */

    

psr-discovery / event-dispatcher-implementations example snippets


use PsrDiscovery\Discover;

// Return an instance of the first discovered PSR-14 Event Dispatcher implementation.
$eventDispatcher = Discover::eventDispatcher();

// Send a request using the discovered Event Dispatcher.
eventDispatcher->dispatch(...);

use PsrDiscovery\Discover;

$eventDispatcher = Discover::eventDispatcher();

if ($eventDispatcher === null) {
    // No suitable Event Dispatcher implementation was discovered.
    // Fall back to a default implementation.
    $eventDispatcher = new DefaultEventDispatcher();
}

use PsrDiscovery\Discover;

// $eventDispatcher1 !== $eventDispatcher2 (default)
$eventDispatcher1 = Discover::eventDispatcher();
$eventDispatcher2 = Discover::eventDispatcher();

// $eventDispatcher1 === $eventDispatcher2
$eventDispatcher1 = Discover::eventDispatcher(singleton: true);
$eventDispatcher2 = Discover::eventDispatcher(singleton: true);

use PsrDiscovery\Discover;
use PsrDiscovery\Implementations\Psr14\EventDispatchers;

// Prefer the a specific implementation of PSR-14 over others.
EventDispatchers::prefer('league/event');

// Return an instance of League\Event\Dispatcher,
// or the next available from the list of candidates,
// Returns null if none are discovered.
$dispatcher = Discover::eventDispatcher();

use PsrDiscovery\Discover;
use PsrDiscovery\Implementations\Psr14\EventDispatchers;

// Only discover a specific implementation of PSR-14.
EventDispatchers::use('league/event');

// Return an instance of League\Event\Dispatcher,
// or null if it is not available.
$dispatcher = Discover::eventDispatcher();