Coder Social home page Coder Social logo

event-dispatcher's Introduction

Event Dispatcher

Minimum PHP Version Build status SensioLabsInsight

This is a simple library for managing event (e.g, http-kernel event, exception event) which utilizes Symfony event dispatcher class interface as event dispatcher object and Symfony event subscriber class interface as event subscriber (or listener collection) for current event dispatcher object.

This event dispatcher library uses several design patterns for extendibility and maintainability:

Table Of Content

Quick Start

EventDispatcher object instantiation

use Gandung\EventDispatcher\EventDispatcher;
use Gandung\EventDispatcher\EventContainer;

$dispatcher = new EventDispatcher(new EventContainer);

or, you can do it with a factory.

use Gandung\EventDispatcher\EventDispatcherFactory;

$factory = new EventDispatcherFactory;
$dispatcher = $factory->getDispatcher();

Resolving events using simple closure based listener

use Gandung\EventDispatcher\EventDispatcherFactory;

$factory = new EventDispatcherFactory;
$dispatcher = $factory->getDispatcher();
$listener = function() {
	echo "i'am a closure based listener.\n";
};

$dispatcher->attachListener('event.simple.closure', $listener, 20);
$dispatcher->dispatch('event.simple.closure');

Resolving events using simple object based listener

use Gandung\EventDispatcher\EventDispatcherFactory;

class Foo
{
	public function dummyResolver()
	{
		echo sprintf("Inside {%s}@{%s}", \spl_object_hash($this), __METHOD__);
	}
}

$foo = new Foo();
$factory = new EventDispatcherFactory;
$dispatcher = $factory->getDispatcher();
$dispatcher->attachListener('event.simple.object', [$foo, 'dummyResolver'], 20);
$dispatcher->dispatch('event.simple.object');

Resolving subscribed events

use Gandung\EventDispatcher\EventDispatcherFactory;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class FooSubscriber implements EventSubscriberInterface
{
	public static function getSubscribedEvents()
	{
		return [
			'event.simple.prioritized' => [
				['dummyResolver1', 20],
				['dummyResolver2', 10],
				['dummyResolver3', -90]
			],
			'event.simple.unprioritized' => [
				'unprioritizedResolver'
			],
			'event.simple.single' => 'singleResolver'
		];
	}

	public function dummyResolver1()
	{
		echo sprintf("{%s@%s}\n", \spl_object_hash($this), __METHOD__);
	}

	public function dummyResolver2()
	{
		echo sprintf("{%s@%s}\n", \spl_object_hash($this), __METHOD__);
	}

	public function dummyResolver3()
	{
		echo sprintf("{%s@%s}\n", \spl_object_hash($this), __METHOD__);
	}

	public function unprioritizedResolver()
	{
		echo sprintf("{%s@%s}\n", \spl_object_hash($this), __METHOD__);
	}

	public function singleResolver()
	{
		echo sprintf("{%s@%s}\n", \spl_object_hash($this), __METHOD__);
	}
}

$subscriber = new FooSubscriber;
$factory = new EventDispatcherFactory();
$dispatcher->attachSubscriber($subscriber);
$dispatcher->dispatch('event.simple.prioritized');
$dispatcher->dispatch('event.simple.unprioritized');
$dispatcher->dispatch('event.simple.single');

API

EventDispatcherFactory

getDispatcher()

Return the EventDispatcher object instance.

EventDispatcher

attachListener($event, $listener, $priority = 0)

Append listener handler to specified event.

detachListener($event, $listener)

Remove listener handler to specified event.

getListeners($event = null)

Get listener that bind on specified event.

hasListeners($event = null)

Determine if specified event name has listeners.

setListenerPriority($event, $listener, $priority)

Set event listener priority.

getListenerPriority($event, $listener)

Get event listener priority.

attachSubscriber(EventSubscriberInterface $subscriber)

Register event subscriber.

detachSubscriber(EventSubscriberInterface $subscriber)

Remove event subscriber.

dispatch($event, Event $eventHandler = null)

Dispatch the specified event.

event-dispatcher's People

Contributors

plvhx 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.