GithubHelp home page GithubHelp logo

event's Introduction

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(
                '[email protected]',
                '[email protected]'
            )
        )
    )
);
$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

event's People

Contributors

fujimaru92 avatar

Stargazers

 avatar All Unser Miranda avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.