zippovich2/event-dispatcher

Simple event dispatcher

v1.0.0 2020-06-03 11:40 UTC

This package is auto-updated.

Last update: 2024-11-11 21:53:40 UTC


README

Simple event dispatcher.

Installation

$ composer require zippovich2/event-dispatcher

Usage

  1. Default usage:

    use Zippovich2\EventDispatcher\EventDispatcher;
    
    $dispatcher = new EventDispatcher();
    
    // Add subscriber to events
    $dispatcher->subscribe('event1', 'Subscriber::callback');
    $dispatcher->subscribe('event2', 'Subscriber::callback2');
    
    // Getting event subscribers
    $event1Subscribers = $dispatcher->getSubscribers('event1');
    
    // Or get subscribers from all events
    $allSubscribers = $dispatcher->getSubscribers();
    
    // Dispatch event
    $dispatcher->dispatch('eventName');
  2. Using TraceableEventDispatcher:

    use Zippovich2\EventDispatcher\TraceableEventDispatcher;
    
    $dispatcher = new TraceableEventDispatcher();
    
    // Add subscriber to events
    $dispatcher->subscribe('event1', 'Subscriber::callback');
    $dispatcher->subscribe('event2', 'Subscriber::callback2');
    
    // Dispatch event return callstack tree
    $callstackTree = $dispatcher->dispatch('eventName');
       
    // Getting raw callstack
    $callStack = $dispatcher->getCallStack();