PHP code example of mmdm / sim-event-dispatcher

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

    

mmdm / sim-event-dispatcher example snippets

 
composer 



// we need event provider to add events to that
$event_provider = new EventProvider();

// also we need closure provider to add closures to that
$closure_provider = new ClosureProvider();

// then use their methods
$event_provider->addEvent(new Event('boot'))
    ->addEvent(new Event('close'));
//-----
$closure_provider->addClosure($nameOfClosureOrKey, function () {
    // define closure functionality
});

// to instanciate an emitter object,
// use event provider and closure provider,
// from above
$emitter = new Emitter($event_provider, $closure_provider);

// now work with listeners
$emitter->addListener($nameOfEvent, $nameOfClosureOrKey);
$emitter->removeListener($nameOfEvent, $nameOfClosureOrKey);

// finally dispatch event where you want
$emitter->dispatch($nameOfEvent);

// to add an event
$event_provider->addEvent(new Event('cartOnBoot'));

// to remove an event
$event_provider->removeEvent('cartOnBoot');

// to get an event
$event_provider->getEvent('cartOnBoot');

// check if an event exists
$event_provider->hasEvent('cartOnBoot');

// to add a closure
$closure_provider->addClosure($closureKeyOrName, function () {
    // define closure functionality
});

// to remove a closure
$closure_provider->removeClosure($closureKeyOrName);

// to get a closure
$closure_provider->getClosure($closureKeyOrName);

// check if a closure exists
$closure_provider->hasClosure('cartOnBoot');

// to add a listener
$emitter->addListener('cartOnBoot', 'cart_boot');
// or with a higher priority
$emitter->addListener('cartOnBoot', 'cart_boot', 5);

// to remove a listener
$emitter->removeListener('cartOnBoot', 'cart_boot');

// to remove a listener
$emitter->removeAllListeners('cartOnBoot');

// to remove a listener with wild card
//
// code below will find all events like
// [cart:boot], [cartOnBoot], etc.
// and remove them all
$emitter->removeAllListeners('cart:?.*');

// to get all listeners of an event
$emitter->getListener('cartOnBoot');

// to get all listeners
$emitter->getAllListeners();

// to get all events with [cart:boot] and [cart:orders]
$emitter->getAllListeners('cart:(boot|orders)');