PHP code example of dakwamine / container-aware-event-dispatcher

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

    

dakwamine / container-aware-event-dispatcher example snippets




class SomeEvent implements \Dakwamine\Event\EventInterface
{
  const EVENT_NAME = 'Any string';

  public function getName(){
    return static::EVENT_NAME;
  }
}

class SomeEventListener implements \Dakwamine\Event\EventListenerInterface
{
  public function handleEvent(\Dakwamine\Event\EventInterface $event) {
    // Do stuff related to $event, like dispatching to other methods.
    if ($event->getName() === SomeEvent::EVENT_NAME) {
      // ...
    }
  }
}

// The container.
$container = new \League\Container\Container();

// Add the listener to the container.
$container->share(SomeEventListener::class);

// This object holds the lists of class names per event.
$listenerProvider = new \Dakwamine\Event\ContainerAwareListenerProvider($container);

// Register the listener. Optionally set the priority.
$listenerProvider->addListener(SomeEvent::EVENT_NAME, SomeEventListener::class, 42);

// Dispatch the event. Listeners will be instantiated using the container by the listener provider.
$eventDispatcher = new \Dakwamine\Event\EventDispatcher($listenerProvider);
$eventDispatcher->dispatch(new SomeEvent());