2017 © Pedro Peláez
 

library event-dispatcher

Observer for events

image

systream/event-dispatcher

Observer for events

  • Tuesday, November 1, 2016
  • by systream
  • Repository
  • 1 Watchers
  • 0 Stars
  • 622 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Event dispatcher

Observer for events, (*1)

Usage Examples

Installation

You can install this package via packagist.org with composer., (*2)

composer require systream/event-dispatcher, (*3)

composer.json:, (*4)

"require": {
    "systream/event-dispatcher": "1.*"
}

This library requires php 5.6 or higher, but also works on php 5.4., (*5)

Subscribe to event

$eventDispatcher = new EventDispatcher();

$subscription = new EventDispatcher\Subscription('nameOfEvent', function (EventInterface $event) {
    // do stuff here
});
$eventDispatcher->subscribe($subscription);

Fire an event

$eventDispatcher = new EventDispatcher();
$eventDispatcher->emit(new EventDispatcher\Event('nameOfEvent'));

Stop Propagation

$eventDispatcher = new EventDispatcher();

$subscription = new EventDispatcher\Subscription('fooBar', function (EventDispatcher\EventInterface $event) {
    $event->stopPropagation();
});
$eventDispatcher->subscribe($subscription);

$subscription2 = new EventDispatcher\Subscription('fooBar', function (EventDispatcher\EventInterface $event) {
    // do stuff here
});
$eventDispatcher->subscribe($subscription2);

$eventDispatcher->emit(new EventDispatcher\Event('fooBar'));

In this case the second subscription won't be invoked., (*6)

Custom events

The only thing you need to do is that implementing the EventInterface, (*7)

class MyCustomEvent extends AbstractEvent implements EventInterface
{

    /**
     * @var string
     */
    protected $key = 'order.save';

    /**
     * @var Order
     */
    protected $order;

    /**
     * @param string $key
     */
    public function __construct(Order $order)
    {
        $this->order = $order;
    }

    /**
     * @return Order
     */
    public function getOrder()
    {
        return $this->order;
    }
}

...

$eventDispatcher = new EventDispatcher();
$subscription = new EventDispatcher\Subscription('nameOfEvent', function (OrderEvent $event) {
    $order = $orderEvent->getOrder();
    // do stuff
});
$eventDispatcher->subscribe($subscription);

Custom Subscriptions

The only thing you need to do is that implementing the SubscriptionInterface, (*8)

Test

Build Status, (*9)

The Versions

01/11 2016

dev-master

9999999-dev

Observer for events

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Peter Tihanyi

dispatcher event observer

11/06 2016

1.0.3

1.0.3.0

Observer for events

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Peter Tihanyi

dispatcher event observer

30/05 2016

1.0.2

1.0.2.0

Observer for events

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Peter Tihanyi

dispatcher event observer

16/05 2016

1.0.1

1.0.1.0

Observer for events

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Peter Tihanyi

dispatcher event observer