settermjd / mezzio-eventmanager-integration
This is a small package that simplifies integrating laminas-eventmanager with Mezzio projects built with the Mezzio Skeleton.
Fund package maintenance!
Community Bridge
Requires
- php: ^8.3 || ^8.4
- laminas/laminas-eventmanager: ^3.14
- psr/container: ^2.0
Requires (Dev)
- laminas/laminas-coding-standard: ^3.0
- laminas/laminas-component-installer: ^3.5
- phpstan/phpdoc-parser: ^2.1
- phpstan/phpstan: ^2.1
- phpstan/phpstan-doctrine: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- phpunit/phpunit: ^12.1
- squizlabs/php_codesniffer: ^3.12
README
Mezzio / Laminas EventManager Integration
This project provides a simplistic way of integrating laminas-eventmanager with Mezzio projects that are (originally) scaffolded with the Mezzio Skeleton, making it almost trivial to subscribe listeners to events.
Tip
If you're new to Mezzio, check out Mezzio Essentials a practical, hands-on guide, which steps you through how to build an application, from start to finish.
Prerequisites
To use this project, you need the following:
- Composer installed globally
- PHP 8.3 or 8.4
Usage
To use the package with an existing Mezzio application, use Composer to add the package as a dependency to your project, as in the following example:
composer require mezzio-eventmanager-integration
How to subscribe listeners to events
Then, to subscribe listeners to events, you need to do two things:
- Add a
listeners
element to the application's configuration, listing the events and the listeners that are subscribed to those events, along with the priority to subscribe them at (which is optional). There are three things to be aware of here:- If you don't assign a priority, a listener will be subscribed with a default priority of 1
- Higher priority values execute earlier
- Lower (negative) priority values execute later
- The listeners must be registered as services in the DI container.
There are many ways to add a listeners
element to the application's configuration, but likely the simplest is to create a new file in the config/autoload directory named listeners.global.php, and in that file add a configuration similar to the example below.
<?php return [ 'listeners' => [ 'add-item' => [ [ 'listener' => FakeLoggerListener::class, 'priority' => 10, ], [ 'listener' => FakeNotificationListener::class, 'priority' => 20, ] ], 'update-item' => [ [ 'listener' => FakeLoggerListener::class, ] ], 'delete-item' => [ [ 'listener' => FakeNotificationListener::class, 'priority' => 10, ], ], ], ];
Assuming the configuration above was used:
FakeLoggerListener
andFakeNotificationListener
are subscribed to the "add-item" eventFakeLoggerListener
is subscribed to the "update-item" with a priority of "1" as a priority was not specifiedFakeNotificationListener
is subscribed to the "delete-item" event
When the "add-item" event is triggered, FakeLoggerListener
will execute first as it has a lower priority, then FakeNotificationListener
will execute (assuming that execution isn't short-circuited).
Contributing
If you want to contribute to the project, whether you have found issues with it or just want to improve it, here's how:
- Issues: ask questions and submit your feature requests, bug reports, etc
- Pull requests: send your improvements
Did You Find The Project Useful?
If the project was useful, and you want to say thank you and/or support its active development, here's how:
- Add a GitHub Star to the project
- Write an interesting article about the project wherever you blog
Disclaimer
No warranty expressed or implied. Software is as is.
Made with 🖤 in Bundaberg by Matthew Setter.