mozart/event-dispatcher

Mozart Event Dispatcher Library

v0.1.5 2013-09-02 07:50 UTC

This package is not auto-updated.

Last update: 2024-12-21 18:09:19 UTC


README

Latest Stable Version - Build Status

Mozart Event Dispatcher Library

This event library from Mozart Small MVC Framework implementation from Observer Pattern

Composer

$ curl -s http://getcomposer.org/installer | php
$ php composer.phar install

//optional create alias composer
$ ln -s composer.phar /usr/bin/composer
$ composer install
$ composer install --dev 	// dev-master
$ composer require mozart/event-dispatcher	// next typing "dev-master"

Initialize Class Component

<?php
require 'vendor/autoload.php'   // if youre install this library with composer
use Mozart\Library\Event\Event;
use Mozart\Library\Event\EventDispatcher;
use Mozart\Library\Event\ObjectEvent;
use Mozart\Library\Event\Listener\CustomListener;

Custom Event Listener

Optional, custom listener to create default listener name on event

Usage

-. Event With Array Argument

<?php

/**
 * Example Class ListenerOne { Array Argument }
 */
class ListenerOne extends CustomListener
{
    public function emailSubscriber(Event $event)
    {
        $event->setEvent(CustomListener::EVENT_SUBSCRIBER);
        echo $event->getEvent();
    }
}

$dispatcher = new EventDispatcher();

/**
 * Dispatch the event with Array Event Argument
 */
$listenerOne = new ListenerOne();
$dispatcher->addListener('event.email.subscriber', array($listenerOne, 'emailSubscriber'));
$dispatcher->dispatch('event.email.subscriber');

-. Event With Closure

<?php
/**
 * Example Class ListenerTwo
 */
class ListenerTwo extends CustomListener
{
    public function newsEvent(Event $event)
    {
        $event->setEvent(CustomListener::EVENT_NEWS);
        echo $event->getEvent();
    }
}

/**
 * Dispatch the event with Closure Event Argument
 */
$dispatcher->addListener('news.event', function() {
        $listenerTwo = new ListenerTwo();
        echo $listenerTwo->newsEvent(new Event());
    });
$dispatcher->dispatch('news.event');

-.Object Event Create Custom Definition Event From Specific Object

<?php
/**
 * Email Subscriber Listener
 */
class EmailSubscriberListener
{
    public function sendDelivery(ObjectEvent $event)
    {
        if (isset($event['name']) && $event['name'] === 'email.subscriber') {
            return $event['name'];
        }

        $event['send'];
    }
}
$objectEvent = new ObjectEvent(
    $subject = null, array(
        'name'  => 'email.subsciber',
        'send'  => array(
            'list'  => array(
                'faizal_pribadi@aol.com',
                'nathan_wahyudi@arcacorp.com'
            )
        )
    )
);
$dispatcher->dispatch('email.subscriber', $objectEvent);
foreach ($objectEvent['send'] as $emails => $email) {
    foreach ($email as $listEmail) {
        echo $listEmail;
    }
}

For full examples @see examples folder

Inject The Dispatcher

For inject the constructor EventDispatcher

use Mozart\Library\Event\InjectDispatcher;

$inject = new InjectDispatcher($dispatcher)

PHPUnit Test Suite

$ phpunit