PHP code example of phossa / phossa-event

1. Go to this page and download the library: Download phossa/phossa-event library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

phossa / phossa-event example snippets


  $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 ]);
  

  // 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 ]);
  

  // bind event for once
  $dispatcher->one('user.login', function(Event $evt) {
      // ...
  });

  // allow 3 times
  $dispatcher->many('user.tag', 3, function(Event $evt) {
      // ...
  });
  

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

  // detach
  $dispatcher->off('user.*');
  

  // callable returns bool
  $dispatcher->error(function($errno, $errstr, $errfile, $errline) {
      // ...
      return true;
  });
  

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

  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();
  

  // 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);
  

  use Phossa\Event\Interfaces;

  class MyEventAware implements Interfaces\EventAwareInterface
  {
      // add setEventManager() and triggerEvent()
      use Interfaces\EventAwareTrait;

      ...
  }

  $subject = new MyEventAware();
  

  // set manager to event-aware subject
  $subject->setEventManager($evtManager);

  // trigger event
  $subject->triggerEvent('eventName2');
  

  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',
          ];
      }
  }
  

  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 ]
              )
          );
      }
  }
  

  use Phossa\Event\Interfaces;

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

      ...
  }
  

  // 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');
  

  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');
  

  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( ... );
  

  // 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 {
      ...
  }