2017 © Pedro Peláez
 

library events

Includes Event, EventDispatcher classes and IEventDispatcher interface for events support.

image

actualwave/events

Includes Event, EventDispatcher classes and IEventDispatcher interface for events support.

  • Wednesday, January 6, 2016
  • by a_[w]
  • Repository
  • 1 Watchers
  • 0 Stars
  • 11 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

PHP-Events

Build Status Coverage Status Dependency Status Latest Stable Version Total Downloads License, (*1)

Events allow easy setup and use for communication between components in application. Event can notify about state changes and pass data of any kind., (*2)

Installation

Via composer, (*3)

composer require actualwave/events

Usage

To use events you don't need to implement any interfaces, just create EventDispatcher and register some listeners for events., (*4)

class Broadcaster {
  const EVENT_FIRST = 'eventFirst';
  const EVENT_SECOND = 'eventSecond';
  const EVENT_THIRD = 'eventThird';
  /**
   * @var \aw\events\EventDispatcher
   */
  private $_dispatcher;

  //TODO add dispatcher target test
  public function __construct() {
    $this->_dispatcher = new \aw\events\EventDispatcher();
  }

  public function addHandler(string $eventType, callable $handler) {
    $this->_dispatcher->addEventListener($eventType, $handler);
  }

  public function doFirst() {
    echo 'do first and tell ';
    $this->_dispatcher->dispatchEvent(self::EVENT_FIRST);
  }

  public function doSecond() {
    echo 'do second and tell ';
    $this->_dispatcher->dispatchEvent(new \aw\events\ValueEvent(self::EVENT_SECOND, 'pass some data'));
  }

  public function doThird() {
    echo 'do third and tell ';
    $this->_dispatcher->dispatchEvent(self::EVENT_THIRD);
  }
}

After EventDispatcher is instantiated, you can add listeners and dispatch events., (*5)

$target = new Broadcaster();
// register event handlers
$target->addHandler(Broadcaster::EVENT_FIRST, function ($event) {
  echo $event->type . PHP_EOL;
});

function secondHandler($event) {
  echo 'handler was called with data: '.$event->value.PHP_EOL;
}

$target->addHandler(Broadcaster::EVENT_SECOND, 'secondHandler');
$target->addHandler(Broadcaster::EVENT_THIRD, [new class () {
  public function eventHandler($event) {
    echo 'event has target: '.json_encode(isset($event->target)).PHP_EOL;
  }
}, 'eventHandler']);
// broadcast events
$target->doThird(); // do third and tell event has target: true
$target->doSecond(); // do second and tell handler was called with data: pass some data
$target->doFirst(); // do first and tell eventFirst

The Versions

06/01 2016

dev-master

9999999-dev https://github.com/burdiuz/php-event-dispatcher

Includes Event, EventDispatcher classes and IEventDispatcher interface for events support.

  Sources   Download

MIT

The Requires

 

The Development Requires

component dispatcher event listener callback communication fire

05/01 2016

0.0.2

0.0.2.0 https://github.com/burdiuz/php-event-dispatcher

Includes Event, EventDispatcher classes and IEventDispatcher interface for events support.

  Sources   Download

MIT

The Requires

 

The Development Requires

component dispatcher event listener callback communication fire

11/12 2015

0.0.1

0.0.1.0

PHP Events

  Sources   Download

MIT

The Requires

  • aw/object >=0.0.3
  • aw/callbacks >=0.0.1

 

The Development Requires