2017 © Pedro Peláez
 

library mediator

Mediator component

image

gobline/mediator

Mediator component

  • Wednesday, February 1, 2017
  • by mdecaffmeyer
  • Repository
  • 1 Watchers
  • 2 Stars
  • 75 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 1 % Grown

The README.md

Mediator component

The Mediator component allows your application components to communicate with each other by dispatching events and listening to them. It implements the mediator behavioral design pattern., (*1)

Creating the Dispatcher

use Gobline\Mediator\EventDispatcher;

$dispatcher = new EventDispatcher();

Adding Event Subscribers

use Gobline\Mediator\EventSubscriberInterface;

class FooSubscriber implements EventSubscriberInterface
{
    public function onFooEvent()
    {
        // ... do something
    }

    public function getSubscribedEvents()
    {
        return ['fooEvent' => 'onFooEvent'];
    }
}

$subscriber = new FooSubscriber();
$dispatcher->addSubscriber($subscriber);

Adding Event Listeners

Event listeners are similar to event subscribers. The only difference is that an event listener doesn't need to implement the EventSubscriberInterface interface and provide the events it listens to. Instead, you specify the events it listens to outside the class. The equivalent of the sample above using an event listener would be:, (*2)

class FooListener
{
    public function onFooEvent()
    {
        // ... do something
    }
}

$listener = new FooListener();
$dispatcher->addListener($listener, ['fooEvent' => 'onFooEvent']);

Lazy Listeners

You can add a closure which will lazy load the listener instance. This allows the listener to be instantiated only when an event it listens to is dispatched., (*3)

$dispatcher->addListener(
    function() { return new FooListener(); },
    ['fooEvent' => 'onFooEvent']);

Dispatching Events

$dispatcher->dispatch('fooEvent');

Passing event data

$dispatcher->dispatch('fooEvent', ['foo' => 'bar']);

class FooListener
{
    public function onFooEvent(array $data)
    {
        // ... do something with $data['foo']
    }
}

Installation

You can install the Mediator component using the dependency management tool Composer. Run the require command to resolve and download the dependencies:, (*4)

composer require gobline/mediator

The Versions

01/02 2017

dev-master

9999999-dev https://github.com/gobline

Mediator component

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.6.0

 

by Mathieu Decaffmeyer

event dispatcher mediator gobline

03/12 2015

v3.0.0

3.0.0.0 https://github.com/gobline

Mediator component

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.6.0

 

by Mathieu Decaffmeyer

event dispatcher mediator gobline

17/01 2015

v2.0.1

2.0.1.0 https://github.com/mendoframework

Mendo Mediator Component

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5.0

 

The Development Requires

by Mathieu Decaffmeyer

event dispatcher mediator mf mendoframework mendo framework

15/12 2014

v2.0.0

2.0.0.0 https://github.com/mendoframework

Mendo Mediator Component

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5.0

 

The Development Requires

by Mathieu Decaffmeyer

event dispatcher mediator mf mendoframework mendo framework

15/12 2014

v1.1.0

1.1.0.0 https://github.com/mendoframework

Mendo Mediator Component

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5.0

 

The Development Requires

by Mathieu Decaffmeyer

event dispatcher mediator mf mendoframework mendo framework

15/12 2014

v1.0.1

1.0.1.0 https://github.com/mendoframework

Mendo Mediator Component

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5.0

 

The Development Requires

by Mathieu Decaffmeyer

event dispatcher mediator mf mendoframework mendo framework

13/11 2014

v1.0.0

1.0.0.0 https://github.com/mendoframework

Mendo Mediator Component

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.5.0

 

The Development Requires

by Mathieu Decaffmeyer

event dispatcher mediator mf mendoframework mendo framework