Event Handler For Ice Cream
PHP
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
docs
src
tests
.gitignore
.travis.yml
LICENSE
README.md
composer.json
composer.lock
phpunit.xml

README.md

Ice Cream Events

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.