trash-panda / m2-callable-event-listeners
Listen to events with plain PHP callable minus any configuration
Installs: 1 461
Dependents: 0
Suggesters: 0
Security: 0
Stars: 6
Watchers: 3
Forks: 2
Open Issues: 0
Type:magento2-module
Requires
- php: >=7.0 <7.3
Requires (Dev)
- phpunit/phpunit: ^6.0
- squizlabs/php_codesniffer: ^2.8
This package is auto-updated.
Last update: 2024-12-20 22:27:43 UTC
README
Allow to register event listeners with a plain PHP callable - minus the config.
Installation
$ composer require trash-panda/m2-callable-event-listeners $ php bin/magento setup:upgrade
Usage
Grab an instance of \TrashPanda\CallableEventListeners\Model\Manager
either via DI or from the object manager and pass an event name and a callable to the listen
method.
use TrashPanda\CallableEventListeners\Model\Manager; use Magento\Framework\Event\Observer; class MyCommand { public function ___construct(Manager $manager) { $manager->listen('some-event', function (Observer $observer) { echo "Hey!\n\n"; var_dump($observer->getData()); }); } }
Now when the event some-event
is dispatched from \Magento\Framework\Event\Manager
your callable will be invoked
along with any other listeners attached via config.
Use Cases
- Scoping events to one particular code path - eg a command or a cron
- Prototyping
- Improved feedback from IDE - regarding classes and method existence