2017 © Pedro Peláez
 

library phossa-event

A event management library for PHP

image

phossa/phossa-event

A event management library for PHP

  • Sunday, June 12, 2016
  • by phossa
  • Repository
  • 1 Watchers
  • 1 Stars
  • 17 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 0 % Grown

The README.md

phossa-event [ABANDONED]

Build Status Latest Stable Version License, (*1)

See new lib at phoole/event, (*2)

Introduction

Phossa-event is an event management library for PHP. It decoupled from any packages other than the phossa/phossa-shared. It requires PHP5.4 only., (*3)

Installation

Install via the composer utility., (*4)

composer require "phossa/phossa-event=1.*"

or add the following lines to your composer.json, (*5)

{
    "require": {
       "phossa/phossa-event": "1.*"
    }
}

Simple EventDispatcher

Simple event dispatching using Phossa\Event\EventDispatcher, (*6)

  • Simple usage, (*7)

    $dispatcher = new EventDispatcher();
    
    // bind event 'user.login' to a callable
    $dispatcher->on('user.login', function(Event $evt) {
      // ...
    });
    
    // trigger the 'user.login' event, passing data
    $dispatcher->trigger('user.login', [ 'user' => $user ]);
    

    or use statically (global dispatching), (*8)

    // bind event with priority 60 (0 - 100, high # higher priority)
    EventDispatcher::on('user.login', function(Event $evt) {
      // ...
    }, 60);
    
    // trigger the 'user.login' event
    EventDispatcher::trigger('user.login', [ 'user' => $user ]);
    
  • Trigger for limited times, (*9)

    // bind event for once
    $dispatcher->one('user.login', function(Event $evt) {
      // ...
    });
    
    // allow 3 times
    $dispatcher->many('user.tag', 3, function(Event $evt) {
      // ...
    });
    
  • Event globbing, (*10)

    // globbing
    $dispatcher->on('user.*', function(Event $evt) {
      //...
    });
    
  • Detach events, (*11)

    // detach
    $dispatcher->off('user.*');
    
  • Monitoring PHP errors, (*12)

    Execute a callable when PHP error happens., (*13)

    // callable returns bool
    $dispatcher->error(function($errno, $errstr, $errfile, $errline) {
      // ...
      return true;
    });
    
  • Execute a callable when script finishes, (*14)

    // run this after script ends
    $dispatcher->ready(function() {
      // ...
    });
    

Full-fledged EventManager

Complex event management using Phossa\Event\EventManager with full fledge support for event listener, event subject, local event manager and global event manageretc., (*15)

  • Event listener, (*16)

    use Phossa\Event\Interfaces;
    
    class MyListener implements Interfaces\EventListenerInteface
    {
      /*
       * Get events and callables MyListener listens to
       */
      public function getEventsListening()
      {
          return array(
              'eventName1' => 'method1', // method1 of $this
              'eventName2' => array('method2', 20), // priority 20
              'eventName3' => array( // multiple callables
                  [ 'method3', 70 ],
                  [ 'method4', 50 ]
              )
          );
      }
    }
    
    $listener = new MyListener();
    
  • Event manager, (*17)

    // create an event manager
    $evtManager = new Event\EventManager();
    
    // attach a listener object
    $evtManager->attachListener($listener);
    
    // attach a callable directly to 'oneSpecialEvent'
    $callable = function(Event\Event $evt) {
      ...
    };
    $evtManager->attachListener($callable, 'oneSpecialEvent');
    
    // detach a callable
    $evtManager->detachListener($callable);
    
  • The event-aware subject, (*18)

    use Phossa\Event\Interfaces;
    
    class MyEventAware implements Interfaces\EventAwareInterface
    {
      // add setEventManager() and triggerEvent()
      use Interfaces\EventAwareTrait;
    
      ...
    }
    
    $subject = new MyEventAware();
    
  • Combine together, (*19)

    // set manager to event-aware subject
    $subject->setEventManager($evtManager);
    
    // trigger event
    $subject->triggerEvent('eventName2');
    
  • Event globbing, (*20)

    Able to listen to all events by using '' or 'event'., (*21)

    use Phossa\Event\Interfaces\EventListenerInterface;
    
    class Listener implements EventListenerInterface
    {
      public function getEventsListening()
      {
          return [
              'evtTest1' => 'testC',
              'evtTest2' => [ 'testD', 20 ],
              'evtTest3' => [
                  [ 'testA', 70 ],
                  [ 'testB', 50 ]
              ],
              // globbing
              'evt*' => 'bingo',
              'evtTest*' => 'bingo2',
              '*' => 'wow',
          ];
      }
    }
    
  • Event management for static classes., (*22)

    The static listener class,, (*23)

    use Phossa\Event\Interfaces;
    
    class StaticListener implements Interfaces\EventListenerStaticInteface
    {
      /*
       * Get events and callables StaticListener listens to
       */
      public static function getEventsListening()
      {
          return array(
              'eventName1' => 'method1', // method1 of $this
              'eventName2' => array('method2', 20), // priority 20
              'eventName3' => array( // multiple callables
                  [ 'method3', 70 ],
                  [ 'method4', 50 ]
              )
          );
      }
    }
    

    The static subject class to use events,, (*24)

    use Phossa\Event\Interfaces;
    
    class StaticEventAware implements Interfaces\EventAwareStaticInterface
    {
      // add setEventManager() and triggerEvent()
      use Interfaces\EventAwareStaticTrait;
    
      ...
    }
    

    The static subject class trigger events as follows,, (*25)

    // create an event manager/dispatcher
    $evtManager = new Event\EventManager();
    
    // attach a static listener class
    $evtManager->attachListener(StaticListener::CLASS);
    
    // set manager/dispatcher to event-aware static subject class
    StaticEventAware::setEventManagerStatically($evtManager);
    
    // trigger event by the static subject class
    StaticEventAware::triggerEventStatically('eventName2');
    
  • Composite event manager, (*26)

    Able to use composite event manager as follows,, (*27)

    use Phossa\Event;
    
    // global event manager
    $global_manager = new Event\EventManager();
    $global_manager->attachListener($some_global_event_listener);
    
    // local event manager
    $local_manager  = new Event\Variation\EventManagerComposite();
    $local_manager->attachListener($local_listener);
    
    // allow local event manager dispatch events to global event manager
    $local_manager->setOtherManager('global', $global_manager);
    
    // the event aware subject
    $subject = new MyEventAware();
    
    // set event manager
    $subject->setEventManager($local_manager);
    
    // fire up an event, will look into event handling queue from both
    // $local_manager and $global_manager
    $subject->triggerEvent('some_event');
    
  • Immutable event manager, (*28)

    use Phossa\Event;
    
    // low-level manager to hide
    $_evtManager = new EventManager();
    $_evtManager->attachListener(...);
    ...
    
    // expose an immutable event managet to user
    $evtManager = new Variation\ImmutableEventManager($_evtManager);
    
    // cause an exception
    $evtManager->detachListener( ... );
    
  • Shareable event manager, a single copy of global manager and lots of local managers., (*29)

    // get global copy by using static method `getInstance()`
    $globalEventManager = ShareableEventManager::getInstance();
    
    // normal event managers
    $localEventManager  = new ShareableEventManager();
    
    // is this the global copy?
    if ($evtManager->isShareable()) {
      ...
    } else {
      ...
    }
    

Features

  • Supports PHP 5.4+, PHP 7.0+, HHVM., (*30)

  • PHP7 ready for return type declarations and argument type declarations., (*31)

  • PSR-1, PSR-2, PSR-4 compliant., (*32)

  • Decoupled packages can be used seperately without the framework., (*33)

Dependencies

  • PHP >= 5.4.0, (*34)

  • phossa/phossa-shared >= 1.0.8, (*35)

License

MIT License, (*36)

The Versions

12/06 2016

dev-master

9999999-dev https://github.com/phossa/phossa-event

A event management library for PHP

  Sources   Download

MIT

The Requires

 

framework event phossa

27/05 2016

1.0.7

1.0.7.0 https://github.com/phossa/phossa-event

A event management library for PHP

  Sources   Download

MIT

The Requires

 

framework event phossa

27/05 2016

1.0.6

1.0.6.0 https://github.com/phossa/phossa-event

A event management library for PHP

  Sources   Download

MIT

The Requires

 

framework event phossa

27/05 2016

1.0.5

1.0.5.0 https://github.com/phossa/phossa-event

A event management library for PHP

  Sources   Download

MIT

The Requires

 

framework event phossa

07/02 2016

1.0.4

1.0.4.0 https://github.com/phossa/phossa-event

The event management package of Phossa framework

  Sources   Download

MIT

The Requires

 

framework event phossa

01/02 2016

1.0.2

1.0.2.0 https://github.com/phossa/phossa-event

The event management package of Phossa framework

  Sources   Download

MIT

The Requires

 

framework event phossa

02/11 2015

1.0.0

1.0.0.0 https://github.com/phossa/phossa-event

The event package of Phossa framework

  Sources   Download

MIT

The Requires

  • php >=5.4.0
  • phossa-shared >=1.0.0

 

framework event phossa