Mozart Event Dispatcher PHP
PHP
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
Exception
Listener
Subscriber
Tests
examples
.gitignore
.scrutnizer.yml
.sparkleshare
.travis.yml
CHANGELOG.md
Event.php
EventDispatcher.php
EventDispatcherInterface.php
EventInterface.php
InjectDispatcher.php
LICENSE
ObjectEvent.php
README.md
SparkleShare.txt
composer.json
phpunit.xml.dist

README.md

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

Constant Result
EVENT_SUBSCRIBER event.subsriber
EVENT_NEWS event.news
EVENT_AUTHOR event.author
EVENT_DATE event.date

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

Base Class Definition
Sendmail Event Create one event listener
Store Subscriber Event Allow subscriber to event

Inject The Dispatcher

For inject the constructor EventDispatcher

use Mozart\Library\Event\InjectDispatcher;

$inject = new InjectDispatcher($dispatcher)

PHPUnit Test Suite

$ phpunit