{"id":15068592,"url":"https://github.com/spiral-packages/event-bus","last_synced_at":"2025-09-11T18:46:04.956Z","repository":{"id":37979939,"uuid":"453168271","full_name":"spiral-packages/event-bus","owner":"spiral-packages","description":"A simple observer pattern implementation based on symfony event handler for Spiral Framework 2.x, allowing you to subscribe and listen for various events that occur within your application.","archived":false,"fork":false,"pushed_at":"2023-03-13T07:58:48.000Z","size":97,"stargazers_count":6,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"1.x","last_synced_at":"2025-07-13T21:05:01.696Z","etag":null,"topics":["eventbus","package","php","php8","psr-14","spiral-framework"],"latest_commit_sha":null,"homepage":"https://spiral.dev/docs/packages-event-bus","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/spiral-packages.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-01-28T18:05:00.000Z","updated_at":"2024-06-27T17:28:23.000Z","dependencies_parsed_at":"2024-09-29T14:00:33.373Z","dependency_job_id":null,"html_url":"https://github.com/spiral-packages/event-bus","commit_stats":{"total_commits":39,"total_committers":3,"mean_commits":13.0,"dds":0.4871794871794872,"last_synced_commit":"b7f0b48a9268972bb332d71b8a52011cbf5cb7ad"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":"spiral-packages/package-skeleton","purl":"pkg:github/spiral-packages/event-bus","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spiral-packages%2Fevent-bus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spiral-packages%2Fevent-bus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spiral-packages%2Fevent-bus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spiral-packages%2Fevent-bus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spiral-packages","download_url":"https://codeload.github.com/spiral-packages/event-bus/tar.gz/refs/heads/1.x","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spiral-packages%2Fevent-bus/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270466805,"owners_count":24588793,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-14T02:00:10.309Z","response_time":75,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["eventbus","package","php","php8","psr-14","spiral-framework"],"created_at":"2024-09-25T01:38:26.725Z","updated_at":"2025-08-14T18:45:03.259Z","avatar_url":"https://github.com/spiral-packages.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# A simple observer pattern implementation based on symfony event handler (PSR-14 compatible)\n\n[![PHP](https://img.shields.io/packagist/php-v/spiral-packages/event-bus.svg?style=flat-square)](https://packagist.org/packages/spiral-packages/event-bus)\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/spiral-packages/event-bus.svg?style=flat-square)](https://packagist.org/packages/spiral-packages/event-bus)\n[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/spiral-packages/event-bus/run-tests?label=tests)](https://github.com/spiral-packages/event-bus/actions?query=workflow%3Arun-tests+branch%3Amain)\n[![Total Downloads](https://img.shields.io/packagist/dt/spiral-packages/event-bus.svg?style=flat-square)](https://packagist.org/packages/spiral-packages/event-bus)\n\nSubscribe and listen for various events that occur within your application.\n\n## Requirements\n\nMake sure that your server is configured with following PHP version and extensions:\n\n- PHP 8.0+\n- Spiral framework 2.9+\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require spiral-packages/event-bus\n```\n\nAfter package install you need to register bootloader from the package.\n\n```php\nprotected const LOAD = [\n    // ...\n    \\Spiral\\EventBus\\Bootloader\\EventBusBootloader::class,\n];\n```\n\nor\n\n```php\nnamespace App\\Bootloader;\n\nuse Spiral\\EventBus\\Bootloader\\EventBusBootloader as BaseBootloader\n\nclass EventBusBootloader extends BaseBootloader\n{\n    protected const LISTENS = [\n        \\App\\Event\\UserCreated::class =\u003e [\n            \\App\\Listener\\SendWelcomeMessageListener::class\n        ],\n        //...\n    ];\n}\n```\n\n## Usage\n\nAt first need create config file `app/config/event-bus.php`, where you can specify listeners.\n\n```php\n\u003c?php\n\ndeclare(strict_types=1);\n\nreturn [\n    'queueConnection' =\u003e env('EVENT_BUS_QUEUE_CONNECTION'), // default queue connection for Listeners with \\Spiral\\EventBus\\QueueableInterface\n    'discoverListeners' =\u003e env('EVENT_BUS_DISCOVER_LISTENERS', true), // Discover listeners with \\Spiral\\EventBus\\Attribute\\Listener attribute\n    'listeners' =\u003e [\n        UserDeleted::class =\u003e [\n            DeleteUserComments::class,\n        ]\n    ],\n    'interceptors' =\u003e [\n        BroadcastEventInterceptor::class\n    ]\n];\n```\n\nYou can also register listeners via `Spiral\\EventBus\\ListenerRegistryInterface`\n\n```php\nclass MyPackageBootloader extends Spiral\\Boot\\Bootloader\\Bootloader\n{\n    public function start(Spiral\\EventBus\\ListenerRegistryInterface $registry) \n    {\n        $registry-\u003eaddListener(UserDeleted::class, DeleteUserComments::class);\n    }\n}\n```\n\n#### Event example\n\n```php\nclass UserDeleted \n{\n    public function __construct(public string $name) {}\n}\n```\n\n#### Listener example\n\n\u003e Make sure to use variable `$event` for event handler method. It's required.\n\n```php\nclass DeleteUserComments \n{\n    public function __construct(private CommentService $service) {}\n    \n    public function __invoke(UserDeleted $event)\n    {\n        $this-\u003eservice-\u003edeleteCommentsForUser($event-\u003ename);\n    }\n}\n```\n\n#### Listener example with attributes\n\nIf you are using listeners with attributes `'discoverListeners' = true`, you don't need to register them, they will be\nregistered automatically.\n\n```php\nuse Spiral\\EventBus\\Attribute\\Listener;\n\nclass DeleteUserComments \n{\n    public function __construct(private CommentService $service) {}\n    \n    #[Listener]\n    public function handleDeletedUser(UserDeleted $event)\n    {\n        $this-\u003eservice-\u003edeleteCommentsForUser($event-\u003eusernname);\n    }\n    \n    #[Listener]\n    public function handleCreatedUser(UserCreated $event)\n    {\n        $this-\u003eservice-\u003ecreaateUserProfile($event-\u003eusernname);\n    }\n    \n    #[Listener]\n    public function notifyAdmins(UserCreated|UserDeleted $event)\n    {\n        $this-\u003eservice-\u003enotifyAdmins($event-\u003eusernname);\n    }\n}\n```\n\n#### Listener example that should be handled in a queue\n\nIf you want to push listener to a queue, you can add `Spiral\\EventBus\\QueueableInterface`\n\n```php\nclass DeleteUserComments implements \\Spiral\\EventBus\\QueueableInterface\n{\n    // ...\n}\n```\n\n#### Event dispatching\n\n```php\nuse Symfony\\Component\\EventDispatcher\\EventDispatcherInterface;\n\nclass UserService \n{\n    public function __construct(private EventDispatcherInterface $events) {}\n    \n    public function deleteUserById(string $id): void\n    {\n        $user = User::findById($id);\n        //.. \n        \n        $this-\u003eevents-\u003edispatch(\n            new UserDeleted($user-\u003eusername)\n        );\n    }\n}\n```\n\n#### Interceptors\n\nThe package provides convenient Bootloader to configure core\ninterceptors `Spiral\\EventBus\\Bootloader\\EventBusBootloader` automatically:\n\n```php\nnamespace App\\Bootloader;\n\nuse Spiral\\EventBus\\Bootloader\\EventBusBootloader as BaseBootloader\n\nclass EventBusBootloader extends BaseBootloader\n{\n    protected const INTERCEPTORS = [\n        \\App\\Event\\Interceptor\\BroadcastEventInterceptor::class,\n        //...\n    ];\n}\n```\n\nor via config `app/config/event-bus.php`\n\n```php\n\u003c?php\n\ndeclare(strict_types=1);\n\nreturn [\n    // ...\n    'interceptors' =\u003e [\n        BroadcastEventInterceptor::class\n    ]\n];\n```\n\n```php\nnamespace App\\Event\\Interceptor;\n\nuse Spiral\\Broadcasting\\BroadcastInterface;\n\nclass BroadcastEventInterceptor implements \\Spiral\\Core\\CoreInterceptorInterface\n{\n    public function __construct(\n        private BroadcastInterface $broadcast\n    ) {}\n    \n    public function process(string $eventName, string $action, array , CoreInterface $core): mixed\n    {\n        $event = $parameters['event']; // Event object\n        $listeners = $parameters['listeners']; // array of invokable listeners\n        \n        $result = $core-\u003ecallAction($eventName, $action, $parameters);     \n        \n        if ($event instanceof ShouldBroadcastInterface) {\n            $this-\u003ebroadcast-\u003epublish(\n                $event-\u003egetBroadcasTopics(), \n                \\json_encode($event-\u003etoBroadcast())\n            );\n        }\n        \n        return $result;\n    }\n}\n```\n\n## Testing\n\n```bash\ncomposer test\n```\n\nIf you are using [`spiral/testing`](https://github.com/spiral/testing) package in your application, you can additionally\nuse trait `Spiral\\EventBus\\Testing\\InteractsWithEvents` in your tests cases.\n\n```php\nclass EventDispatcherTest extends TestCase\n{\n    use \\Spiral\\EventBus\\Testing\\InteractsWithEvents;\n\n    public function testDispatchEvent(): void\n    {\n        $events = $this-\u003efakeEventDispatcher();\n    \n        $this-\u003egetDispatcher()-\u003edispatch(new SimpleEvent());\n    \n        $events-\u003eassertListening(SimpleEvent::class, SimpleListener::class);\n        $events-\u003eassertListening(SimpleEvent::class, ListenerWithAttributes::class, 'methodA');\n        \n        $events-\u003eassertDispatched(SimpleEvent::class)\n        \n        $events-\u003eassertDispatched(SimpleEvent::class, function(SimpleEvent $event) {\n            return $event-\u003esomeProperty === 'foo';\n        });\n\n        $events-\u003eassertDispatchedTimes(SimpleEvent::class, 10);\n        \n        $events-\u003eassertNotDispatched(AnotherSimpleEvent::class);\n        \n        $events-\u003eassertNotDispatched(AnotherSimpleEvent::class);\n        \n        $events-\u003eassertNothingDispatched();\n    }\n}\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n- [butschster](https://github.com/spiral-packages)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspiral-packages%2Fevent-bus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspiral-packages%2Fevent-bus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspiral-packages%2Fevent-bus/lists"}