- Notifications
You must be signed in to change notification settings - Fork1
Lightweight library that discovers available PSR-14 Event Dispatcher implementations by searching for a list of well-known classes that implement the relevant interface, and returns an instance of the first one that is found.
License
psr-discovery/event-dispatcher-implementations
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Lightweight library that discovers availablePSR-14 Event Dispatcher implementations by searching for a list of well-known classes that implement the relevant interface, and returns an instance of the first one that is found.
This package is part of thePSR Discovery utility suite, which also supportsPSR-18 HTTP Clients,PSR-17 HTTP Factories,PSR-11 Containers,PSR-6 Caches andPSR-3 Logs.
This is largely intended for inclusion in libraries like SDKs that wish to support PSR-14 Event Dispatchers without requiring hard dependencies on specific implementations or demanding extra configuration by users.
- Requirements
- Implementations
- Installation
- Usage
- Handling Failures
- Exceptions
- Singletons
- Mocking Priority
- Preferring an Implementation
- Using a Specific Implementation
- PHP 8.2+
- Composer 2.0+
Successful discovery requires the presence of a compatible implementation in the host application. This library does not install any implementations for you.
The followingpsr/event-dispatcher-implementation implementations are discovered and instantiated automatically:
- carlosas/simple-event-dispatcher ^0.1.0
- league/event ^3.0
- symfony/event-dispatcher ^4.3 | ^5.0 | ^6.0 | ^7.0
- yiisoft/event-dispatcher ^1.0
The following mock implementations are also available:
Ifa particular implementation is missing that you'd like to see, please open a pull request adding support.
composer require psr-discovery/event-dispatcher-implementations
usePsrDiscovery\Discover;// Return an instance of the first discovered PSR-14 Event Dispatcher implementation.$eventDispatcher = Discover::eventDispatcher();// Send a request using the discovered Event Dispatcher.eventDispatcher->dispatch(...);
If the library is unable to discover a suitable PSR-14 implementation, theDiscover::eventDispatcher() discovery method will simply returnnull. This allows you to handle the failure gracefully, for example by falling back to a default implementation.
Example:
usePsrDiscovery\Discover;$eventDispatcher = Discover::eventDispatcher();if ($eventDispatcher ===null) {// No suitable Event Dispatcher implementation was discovered.// Fall back to a default implementation.$eventDispatcher =newDefaultEventDispatcher();}
By default, theDiscover::eventDispatcher() method will always return a new instance of the discovered implementation. If you wish to use a singleton instance instead, simply passtrue to the$singleton parameter of the discovery method.
Example:
usePsrDiscovery\Discover;// $eventDispatcher1 !== $eventDispatcher2 (default)$eventDispatcher1 = Discover::eventDispatcher();$eventDispatcher2 = Discover::eventDispatcher();// $eventDispatcher1 === $eventDispatcher2$eventDispatcher1 = Discover::eventDispatcher(singleton:true);$eventDispatcher2 = Discover::eventDispatcher(singleton:true);
This library will give priority to searching for a known, available mocking library before searching for a real implementation. This is to allow for easier testing of code that uses this library.
The expectation is that these mocking libraries will always be installed as development dependencies, and therefore if they are available, they are intended to be used.
If you wish to prefer a specific implementation over others, you canprefer() it by package name:
usePsrDiscovery\Discover;usePsrDiscovery\Implementations\Psr14\EventDispatchers;// Prefer the a specific implementation of PSR-14 over others.EventDispatchers::prefer('league/event');// Return an instance of League\Event\Dispatcher,// or the next available from the list of candidates,// Returns null if none are discovered.$dispatcher = Discover::eventDispatcher();
This will cause theeventDispatcher() method to return the preferred implementation if it is available, otherwise, it will fall back to the default behavior.
Note that assigning a preferred implementation will give it priority over the default preference of mocking libraries.
If you wish to force a specific implementation and ignore the rest of the discovery candidates, you canuse() its package name:
usePsrDiscovery\Discover;usePsrDiscovery\Implementations\Psr14\EventDispatchers;// Only discover a specific implementation of PSR-14.EventDispatchers::use('league/event');// Return an instance of League\Event\Dispatcher,// or null if it is not available.$dispatcher = Discover::eventDispatcher();
This will cause theeventDispatcher() method to return the preferred implementation if it is available, otherwise, it will returnnull.
This library is not produced or endorsed by, or otherwise affiliated with, the PHP-FIG.
About
Lightweight library that discovers available PSR-14 Event Dispatcher implementations by searching for a list of well-known classes that implement the relevant interface, and returns an instance of the first one that is found.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.