PHP code example of neunerlei / event-bus

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

    

neunerlei / event-bus example snippets



use Neunerlei\EventBus\EventBus;
use Neunerlei\EventBus\Tests\Assets\FixtureEventA;

$bus = new EventBus();

$bus->addListener(FixtureEventA::class, function(FixtureEventA $e){
    // Do stuff :)
});

$bus->dispatch(new FixtureEventA());


use Neunerlei\EventBus\EventBus;
use Neunerlei\EventBus\Tests\Assets\FixtureEventA;

$bus = new EventBus();

$bus->addListener(FixtureEventA::class, function(FixtureEventA $e){
    echo "3";
}, ["priority" => -10]);

$bus->addListener(FixtureEventA::class, function(FixtureEventA $e){
    echo "2";
});

$bus->addListener(FixtureEventA::class, function(FixtureEventA $e){
    echo "1";
}, ["priority" => 10]);

$bus->dispatch(new FixtureEventA());


use Neunerlei\EventBus\EventBus;
use Neunerlei\EventBus\Tests\Assets\FixtureEventA;

$bus = new EventBus();

$bus->addListener(FixtureEventA::class, function(FixtureEventA $e){
    echo "2";
}, ["id" => "myId"]);

$bus->addListener(FixtureEventA::class, function(FixtureEventA $e){
    echo "1";
}, ["before" => "myId"]);

$bus->dispatch(new FixtureEventA());


use Neunerlei\EventBus\EventBus;
use Neunerlei\EventBus\Tests\Assets\FixtureEventA;

$bus = new EventBus();

$bus->addListener(FixtureEventA::class, static function(){
    echo 'I\'m a special kind of noodle!';
}, ['once']);

$bus->dispatch(new FixtureEventA());
$bus->dispatch(new FixtureEventA());

// The string will only show up once


use Neunerlei\EventBus\EventBus;
use Neunerlei\EventBus\Subscription\EventSubscriberInterface;
use Neunerlei\EventBus\Subscription\EventSubscriptionInterface;
use Neunerlei\EventBus\Tests\Assets\FixtureEventA;

class MyEventSubscriber implements EventSubscriberInterface {
    /**
     * @inheritDoc
     */
    public function subscribeToEvents(EventSubscriptionInterface $subscription){
        // You only have to define the method you want to trigger.
        $subscription->subscribe(FixtureEventA::class, "onEventHappens");
    }

    public function onEventHappens(FixtureEventA $event){
        // Do Stuff
    }

}

$bus = new EventBus();

$bus->addSubscriber(new MyEventSubscriber());

$bus->dispatch(new FixtureEventA());


use Neunerlei\EventBus\EventBus;
use Neunerlei\EventBus\Subscription\EventSubscriptionInterface;
use Neunerlei\EventBus\Subscription\LazyEventSubscriberInterface;
use Neunerlei\EventBus\Tests\Assets\FixtureEventA;

class MyEventSubscriber implements LazyEventSubscriberInterface {
    /**
     * @inheritDoc
     */
    public static function subscribeToEvents(EventSubscriptionInterface $subscription){
        // You only have to define the method you want to trigger.
        $subscription->subscribe(FixtureEventA::class, "onEventHappens");
    }

    public function onEventHappens(FixtureEventA $event){
        // Do Stuff
    }

}

$bus = new EventBus();

// The second argument (factory) is used to instantiate the event subscriber object when it is 


use Neunerlei\EventBus\EventBus;
$bus = new EventBus();

// Retrieve the current dispatcher implementation
$dispatcher = $bus->getConcreteDispatcher();

// Replace the dispatcher with another implementation
$bus->setConcreteDispatcher(new \Crell\Tukio\Dispatcher(new \Crell\Tukio\OrderedListenerProvider()));

// Retrieve the current implementation for the listener provider
$listenerProvider = $bus->getConcreteListenerProvider();

// Replace the listener provider with another implementation
$bus->setConcreteListenerProvider(new \Crell\Tukio\OrderedListenerProvider());

// Retrieve the current container instance
// This will be null, be cause there was no bus given to the constructor.
$container = $bus->getContainer();

// Replace the container with another implementation, or set one if the container
// did not exist at the time when the event bus was instantiated.
$bus->setContainer(new FancyContainer());


use Crell\Tukio\Dispatcher;
use Crell\Tukio\OrderedListenerProvider;
use Crell\Tukio\OrderedProviderInterface;
use Neunerlei\EventBus\EventBus;
use Neunerlei\EventBus\Dispatcher\EventListenerListItem;

$bus = new EventBus();

$orderedProvider = new OrderedListenerProvider();
$bus->setConcreteDispatcher(new Dispatcher($orderedProvider));
$bus->setConcreteListenerProvider($orderedProvider);
$bus->setProviderAdapter(OrderedProviderInterface::class,
    static function (
                    OrderedProviderInterface $provider,
                    string $event,
                    EventListenerListItem $item,
                    array $options
                ) {
                    if ($item->pivotId === null) {
                        return $provider->addListener($item->listener, $item->priority, $item->id, $event);
                    }

                    if ($item->beforePivot) {
                        return $provider->addListenerBefore($item->pivotId, $item->listener, $item->id, $event);
                    }

                    return $provider->addListenerAfter($item->pivotId, $item->listener, $item->id, $event);
                }
    );


use Neunerlei\EventBus\EventBus;
use Neunerlei\EventBus\Dispatcher\EventListenerListItem;

$bus = new EventBus();

$bus->setProviderAdapter(
    ExampleProvider::class,
    static function (
                    ExampleProvider $provider,
                    string $event,
                    EventListenerListItem $item,
                    array $options
                ) {
                    if($item->once){
                        $provider->addOneTimeListener($event, $item->listener, $item->priority, $item->id);
                    }

                    // [...] Your other configuration
                },
    true
);