ice-cream/events

Event Handler For Ice Cream Framework

1.1.1 2018-03-16 21:25 UTC

This package is not auto-updated.

Last update: 2024-11-10 03:15:39 UTC


README

Build Status Packagist Maintenance Made With Love

Simple event handler for managing events in Ice Cream.

Requirements

  • PHP 7.2
  • Is Stand Alone

Documentation

You can view this packages documentation here

Usage

Usage is rather simple, first we have to create a listener and register it. A listener is going to listen for new events to be dispatched and then do something when that event is dispatched.

This is very similar to Symfony's event Dispatcher:

// Create the handler:

$eventHandler = new EventHandler();

// Create a listener to listen to events being fired:

$listener = new Listener();

Lets define the listener class:

use IceCreamEvents\Listener;

class PageViewEventListener extends Listener{

  // Read on to see the event definition.
  public function onAction(PageViewEvent $pageViewedEvent) {
     // ... Do something.
  }
}

Now lets create an event:

use IceCreamEvents\Event;

class PageViewEvent extends Event {

  protected $pageViewed;

  public function __construct(PageViewed $pageViewed) {
    $this->pageViewed = $pageViewed;
  }

  public function getPageViewed() {
    return $this->pageViewed;
  }
}

Finally lets register the event with the appropriate listener:

$eventHandler->register('page.viewed', PageViewEvent::class, PageViewEventListener::class, 'onAction');

Next we dispatch the event:

$eventHandler->dispatch('page.viewed');

What this does is register the page viewed event with the listener class and when the event is fired we call the onAction method and pass it the event class. This is similar to how Laravel registers events.