spiral-packages / league-event
The League Event bridge for Spiral Framework
Installs: 29 920
Dependents: 2
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 1
Open Issues: 2
Requires
- php: ^8.1
- league/event: ^3.0
- spiral/events: ^3.0
Requires (Dev)
- mockery/mockery: ^1.5
- phpunit/phpunit: ^9.5
- roave/security-advisories: dev-latest
- spiral/testing: ^2.0
- vimeo/psalm: ^4.22
This package is auto-updated.
Last update: 2024-12-09 15:55:35 UTC
README
Requirements
Make sure that your server is configured with following PHP version and extensions:
- PHP 8.1+
- Spiral framework 3.0+
Installation
You can install the package via composer:
composer require spiral-packages/league-event
After package install you need to register bootloader from the package.
protected const LOAD = [ // ... \Spiral\League\Event\Bootloader\EventBootloader::class, ];
Note If you are using
spiral-packages/discoverer
, you don't need to register bootloader by yourself.
Usage
Event
An event can be represented by a simple class:
namespace Spiral\Router\Event; use Spiral\Router\RouteInterface; final class RouteFound { public function __construct( public readonly RouteInterface $route ) { } }
Dispatching an event:
$this->container->get(EventDispatcherInterface::class)->dispatch(new RouteNotFound($request));
Listener
A listener can be represented by a simple class with a method that will be called to handle the event.
The method name can be configured in the Listener attribute parameter or in the configuration file ( __invoke
by default):
namespace App\Listener; use Spiral\Events\Attribute\Listener; use Spiral\Router\Event\RouteFound; class RouteListener { public function __invoke(RouteFound $event): void { // ... } }
Registering a listener via a config file:
// file app/config/events.php use App\Listener\RouteListener; use Spiral\Events\Config\EventListener; use Spiral\Router\Event\RouteFound; return [ 'listeners' => [ // without any options RouteFound::class => [ RouteListener::class, ], // OR // with additional options RouteFound::class => [ new EventListener( listener: RouteListener::class, method: 'onRouteFound', priority: 1 ), ], ] ];
Registering a listener via an attribute:
The attribute can be used without additional parameters. Then the method name __invoke
and the event from the type
of the method parameter will be used:
namespace App\Listener; use Spiral\Events\Attribute\Listener; use Spiral\Router\Event\RouteFound; #[Listener] class RouteListener { public function __invoke(RouteFound $event): void { // ... } }
All available options:
namespace App\Listener; use Spiral\Events\Attribute\Listener; use Spiral\Router\Event\RouteFound; #[Listener(event: RouteFound::class, method: 'onRouteFound', priority: 1)] class RouteListener { public function onRouteFound(RouteFound $event): void { // ... } }
The attribute can be used directly on the method, then the method name can be omitted:
namespace App\Listener; use Spiral\Events\Attribute\Listener; use Spiral\Router\Event\RouteFound; class RouteListener { #[Listener(priority: 1)] public function onRouteFound(RouteFound $event): void { // ... } }
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
License
The MIT License (MIT). Please see License File for more information.