Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

PSR-14 Event Processing and Notification package

License

NotificationsYou must be signed in to change notification settings

WyriHaximus/php-broadcast

Repository files navigation

✨ Statically on composer install/update compiled event dispatcher

Continuous IntegrationLatest Stable VersionTotal DownloadsType CoverageLicense

Installation

To install viaComposer, use the command below, it will automatically detect the latest version and bind it with^.

composer require wyrihaximus/broadcast

Usage

Any 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)

Listener

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.    }}

Async & Await

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.

License

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

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors6


[8]ページ先頭

©2009-2025 Movatter.jp