pollen-solutions / event
Pollen Solutions - Event Component - PSR-14 Event dispatcher.
v1.0.1
2021-07-07 00:00 UTC
Requires
- php: ^7.4 || ^8.0
- league/event: ^3.0
- pollen-solutions/support: ^1.0
Requires (Dev)
- phpunit/phpunit: ^9.0
- roave/security-advisories: dev-latest
Suggests
- pollen-solutions/container: Pollen Container Component - PSR-11 ready Dependencies Injection Container
This package is auto-updated.
Last update: 2024-10-30 01:42:56 UTC
README
Pollen Solutions Event Component provide a PSR-14 implementation of Event Dispatching.
Installation
composer require pollen-solutions/event
Basic Usage
Simple way of events dispatching
use Pollen\Event\EventDispatcher; // Create Dispatcher $dispatcher = new EventDispatcher(); // Subscribe events $dispatcher->on('event.demo', function () { var_dump('one'); }); $dispatcher->on('event.demo', function () { var_dump('two'); }); // Dispatch events $dispatcher->trigger('event.demo'); // Output // >> (string) 'one' // >> (string) 'two'
Prioritize the event dispatching
use Pollen\Event\EventDispatcher; // Create Dispatcher $dispatcher = new EventDispatcher(); // Subscribe events $dispatcher->on('event.demo', function () { var_dump('one'); }, 10); $dispatcher->on('event.demo', function () { var_dump('two'); }, 20); // Dispatch events $dispatcher->trigger('event.demo'); // Output // >> (string) 'two' // >> (string) 'one'
Stoppable event propagation
use Pollen\Event\EventDispatcher; use Pollen\Event\StoppableEvent; // Create Dispatcher $dispatcher = new EventDispatcher(); // Subscribe events $dispatcher->on('event.demo', function (StoppableEvent $e) { $e->stopPropagation(); var_dump('one'); }); $dispatcher->on('event.demo', function () { var_dump('two'); }); // Dispatch events $dispatcher->trigger('event.demo'); // Output // >> (string) 'one'
Passed arguments to event listeners
use Pollen\Event\EventDispatcher; use Pollen\Event\TriggeredEvent; // Create Dispatcher $dispatcher = new EventDispatcher(); // Subscribe events $dispatcher->on('event.demo', function (TriggeredEvent $e, $arg1, $arg2) { var_dump('one', $arg1, $arg2); $e->setEventArgs(['newValue1', 'newValue2']); }); $dispatcher->on('event.demo', function (TriggeredEvent $e, $arg1, $arg2) { var_dump('two', $arg1, $arg2); }); // Dispatch events $dispatcher->trigger('event.demo', ['value1', 'value2']); // Output // >> (string) 'one' // >> (string) 'value1' // >> (string) 'value2' // >> (string) 'two' // >> (string) 'newValue1' // >> (string) 'newValue2'
Once dispatch listener
use Pollen\Event\EventDispatcher; // Create Dispatcher $dispatcher = new EventDispatcher(); // Subscribe events $dispatcher->one('event.demo', function () { var_dump('one'); }); $dispatcher->on('event.demo', function () { var_dump('two'); }); // Dispatch events // First $dispatcher->trigger('event.demo'); // Second $dispatcher->trigger('event.demo'); // Output // First dispatch // >> (string) 'one' // >> (string) 'two' // Second dispatch // >> (string) 'two'
Dependencies injection container lazy loading and invokable class
namespace { use Pollen\Container\Container; use Pollen\Event\EventDispatcher; // Container Declaration $container = new Container(); class ServiceDemoNamedClass { public function __invoke($e, $v) { var_dump('one >> '. $v); } } $container->add( 'container.service1', ServiceDemoNamedClass::class ); class ServiceDemoClosuredClass { public function __invoke($e, $v) { var_dump('two >> '. $v); } } $container->add( 'container.service2', function () { return new ServiceDemoClosuredClass(); } ); // Class Declaration class InvokableDemoClass { public function __invoke($e, $v) { var_dump('three >> '. $v); } } class InstantiableDemoClass { public function __invoke($e, $v) { var_dump('four >> '. $v); } } // Create Dispatcher $dispatcher = new EventDispatcher([], $container); // Subscribe events // Good practice $dispatcher->on('event.demo', 'container.service1'); $dispatcher->on('event.demo', 'container.service2'); $dispatcher->on('event.demo', InvokableDemoClass::class); // Increased memory usage practice $dispatcher->on('event.demo', new InstantiableDemoClass()); // Dispatch events $dispatcher->trigger('event.demo', ['value']); }
Advanced usage
@todo