Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork0
PSR-14 Event Processing and Notification package
License
WyriHaximus/php-broadcast
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
✨ Statically on composer install/update compiled event dispatcher
To install viaComposer, use the command below, it will automatically detect the latest version and bind it with^.
composer require wyrihaximus/broadcastAny package setting the following in theircomposer.json will have its autoloading directories and files scannedfor any classes that implementsWyriHaximus\Broadcast\Contracts\Listener. Each public method with a concrete objecttype hint on classes implementingWyriHaximus\Broadcast\Contracts\Listener will be registered as an event listenerfor that object in the type hint.
{"extra": {"wyrihaximus": {"broadcast": {"has-listeners":true } } }}To use those automatically picked up event listeners, use theWyriHaximus\Broadcast\ContainerListenerProvider, whichneeds aPSR-11 container to work.
The following example uses the dummy event and listener coming with this package:
useWyriHaximus\Broadcast\ContainerListenerProvider;useWyriHaximus\Broadcast\Dispatcher;useWyriHaximus\Broadcast\Dummy\Event;$event =newEvent();(newDispatcher(newContainerListenerProvider($container),$logger))->dispatch($event)
The following listener is from one of my apps and listeners both on initialize and shutdown events. The logic has beentaken out, but logging is left intact to demonstrate a simple listener example.
<?phpdeclare(strict_types=1);namespaceWyriHaximus\Apps\WyriHaximusNet\GitHub\Ingest;useMammatus\LifeCycleEvents\Initialize;useMammatus\LifeCycleEvents\Shutdown;usePsr\Log\LoggerInterface;useWyriHaximus\Broadcast\Contracts\DoNotHandle;useWyriHaximus\Broadcast\Contracts\Listener;finalclass Consumerimplements Listener{privateLoggerInterface$logger;publicfunction__construct(ConsumerContract$consumer,Producer$producer,LoggerInterface$logger) {$this->logger =$logger; }publicfunctionstart(Initialize$event):void {$this->logger->debug('Starting to consume ingested GitHub WebHook events'); }publicfunctionhandle(GenericEvent|StandardEvent$even):void {// This handler handles both the GenericEvent and StandardEvent events. }publicfunctionstop(Shutdown$event):void {$this->logger->debug('Stopping to consume ingested GitHub WebHook events'); } #[DoNotHandle]publicfunctiondoNotHandle(Event$event):void {// Even through this method accepts a single object we mark not to handle it with the DoNotHandle attribute. }}
Besides synchronous PHP event dispatching this package also supports async & await throughReactPHP's async package.
To mark a class's listeners to run inside a fiber and have the dispatcher wait for each one to complete beforecontinuing, use theWyriHaximus\Broadcast\Contracts\AsyncListener marker interface instead ofListener.
The MIT License (MIT)
Copyright (c) 2023 Cees-Jan Kiewiet
Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.
About
PSR-14 Event Processing and Notification package
Topics
Resources
License
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors6
Uh oh!
There was an error while loading.Please reload this page.