{"id":14966360,"url":"https://github.com/yiisoft/event-dispatcher","last_synced_at":"2025-05-16T05:06:00.493Z","repository":{"id":34436511,"uuid":"178614696","full_name":"yiisoft/event-dispatcher","owner":"yiisoft","description":"PSR-14 event dispatcher","archived":false,"fork":false,"pushed_at":"2025-03-10T09:41:44.000Z","size":188,"stargazers_count":72,"open_issues_count":2,"forks_count":21,"subscribers_count":19,"default_branch":"master","last_synced_at":"2025-05-07T06:35:21.660Z","etag":null,"topics":["event-dispatcher","events","hacktoberfest","psr-14","yii3"],"latest_commit_sha":null,"homepage":"https://www.yiiframework.com/","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yiisoft.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"open_collective":"yiisoft","github":["yiisoft"]}},"created_at":"2019-03-30T21:46:48.000Z","updated_at":"2025-03-10T09:41:46.000Z","dependencies_parsed_at":"2023-02-13T14:00:30.110Z","dependency_job_id":"6e4df91d-ae72-4dc3-bbdf-cc097f0a930b","html_url":"https://github.com/yiisoft/event-dispatcher","commit_stats":{"total_commits":134,"total_committers":20,"mean_commits":6.7,"dds":0.5149253731343284,"last_synced_commit":"5afae724a56d69a05333a767987526da05dfbf77"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yiisoft%2Fevent-dispatcher","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yiisoft%2Fevent-dispatcher/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yiisoft%2Fevent-dispatcher/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yiisoft%2Fevent-dispatcher/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yiisoft","download_url":"https://codeload.github.com/yiisoft/event-dispatcher/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254453945,"owners_count":22073682,"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","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":["event-dispatcher","events","hacktoberfest","psr-14","yii3"],"created_at":"2024-09-24T13:36:16.268Z","updated_at":"2025-05-16T05:06:00.459Z","avatar_url":"https://github.com/yiisoft.png","language":"PHP","funding_links":["https://opencollective.com/yiisoft","https://github.com/sponsors/yiisoft"],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://github.com/yiisoft\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://yiisoft.github.io/docs/images/yii_logo.svg\" height=\"100px\" alt=\"Yii\"\u003e\n    \u003c/a\u003e\n    \u003ch1 align=\"center\"\u003eYii Event Dispatcher\u003c/h1\u003e\n    \u003cbr\u003e\n\u003c/p\u003e\n\n[![Latest Stable Version](https://poser.pugx.org/yiisoft/event-dispatcher/v/stable.png)](https://packagist.org/packages/yiisoft/event-dispatcher)\n[![Total Downloads](https://poser.pugx.org/yiisoft/event-dispatcher/downloads.png)](https://packagist.org/packages/yiisoft/event-dispatcher)\n[![Build Status](https://github.com/yiisoft/event-dispatcher/workflows/build/badge.svg)](https://github.com/yiisoft/event-dispatcher/actions?query=workflow%3Abuild)\n[![Code Coverage](https://codecov.io/gh/yiisoft/event-dispatcher/branch/master/graph/badge.svg)](https://codecov.io/gh/yiisoft/event-dispatcher)\n[![Mutation testing badge](https://img.shields.io/endpoint?style=flat\u0026url=https://badge-api.stryker-mutator.io/github.com/yiisoft/event-dispatcher/master)](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/event-dispatcher/master)\n[![static analysis](https://github.com/yiisoft/event-dispatcher/workflows/static%20analysis/badge.svg)](https://github.com/yiisoft/event-dispatcher/actions?query=workflow%3A%22static+analysis%22)\n[![type-coverage](https://shepherd.dev/github/yiisoft/event-dispatcher/coverage.svg)](https://shepherd.dev/github/yiisoft/event-dispatcher)\n\n[PSR-14](https://www.php-fig.org/psr/psr-14/) compatible event dispatcher provides an ability to dispatch events and listen\nto events dispatched.\n\n## Features\n\n- [PSR-14](https://www.php-fig.org/psr/psr-14/) compatible.\n- Simple and lightweight.\n- Encourages designing event hierarchy.\n- Can combine multiple event listener providers.\n\n## Requirements\n\n- PHP 8.0 or higher.\n\n## Installation\n\nThe package could be installed with [Composer](https://getcomposer.org):\n\n```shell\ncomposer require yiisoft/event-dispatcher\n```\n\n## General usage\n\nThe library consists of two parts: event dispatcher and event listener provider. Provider's job is to register listeners\nfor a certain event type. Dispatcher's job is to take an event, get listeners for it from a provider and call them sequentially.\n\n```php\n// Add some listeners.\n$listeners = (new \\Yiisoft\\EventDispatcher\\Provider\\ListenerCollection())\n    -\u003eadd(function (AfterDocumentProcessed $event) {\n        $document = $event-\u003egetDocument();\n        // Do something with document.\n    });\n\n$provider = new Yiisoft\\EventDispatcher\\Provider\\Provider($listeners);\n$dispatcher = new Yiisoft\\EventDispatcher\\Dispatcher\\Dispatcher($provider);\n```\n\nThe event dispatching may look like:\n\n```php\nuse Psr\\EventDispatcher\\EventDispatcherInterface;\n\nfinal class DocumentProcessor\n{\n    private EventDispatcherInterface $eventDispatcher;\n    \n    public function __construct(EventDispatcherInterface $eventDispatcher) {\n        $this-\u003eeventDispatcher = $eventDispatcher;\n    }\n\n    public function process(Document $document): void\n    {\n        // Process the document, then dispatch completion event.\n        $this-\u003eeventDispatcher-\u003edispatch(new AfterDocumentProcessed($document));\n    }\n}\n```\n\n### Stoppable events\n\nEvent could be made stoppable by implementing `Psr\\EventDispatcher\\StoppableEventInterface`:\n\n```php\nfinal class BusyEvent implements Psr\\EventDispatcher\\StoppableEventInterface\n{\n    // ...\n\n    public function isPropagationStopped(): bool\n    {\n        return true;\n    }\n}\n```\n\nThis way we can ensure that only first event listener will be able to handle the event. Another option is\nto allow stopping propagation in one of the listeners by providing corresponding event method.\n\n### Events hierarchy\n\nEvents do not have any name or wildcard matching on purpose. Event class names and class/interface hierarchy\nand composition could be used to achieve great flexibility:\n\n```php\ninterface DocumentEvent\n{\n}\n\nfinal class BeforeDocumentProcessed implements DocumentEvent\n{\n}\n\nfinal class AfterDocumentProcessed implements DocumentEvent\n{\n}\n```\n\nWith the interface above listening to all document-related events could be done as:\n\n```php\n$listeners-\u003eadd(static function (DocumentEvent $event) {\n    // log events here\n});\n```\n\n### Combining multiple listener providers\n\nIn case you want to combine multiple listener providers, you can use `CompositeProvider`:\n\n```php\n$compositeProvider = new Yiisoft\\EventDispatcher\\Provider\\CompositeProvider();\n$provider = new Yiisoft\\EventDispatcher\\Provider\\Provider($listeners);\n$compositeProvider-\u003eadd($provider);\n$compositeProvider-\u003eadd(new class implements ListenerProviderInterface {\n    public function getListenersForEvent(object $event): iterable\n    {\n        yield static function ($event) {\n            // handle \n        };\n    }\n});\n\n$dispatcher = new Yiisoft\\EventDispatcher\\Dispatcher\\Dispatcher($compositeProvider);\n```\n\n### Register listeners with concrete event names\n\nYou may use a more simple listener provider, which allows you to specify which event they can provide.\n\nIt can be useful in some specific cases, for instance if one of your listeners does not need the event\nobject passed as a parameter (can happen if the listener only needs to run at a specific stage during\nruntime, but does not need event data).\n\nIn that case, it is advised to use the aggregate (see above) if you need features from both providers included\nin this library.\n\n```php\n$listeners = (new \\Yiisoft\\EventDispatcher\\Provider\\ListenerCollection())\n    -\u003eadd(static function () {\n    // this function does not need an event object as argument\n}, SomeEvent::class);\n```\n\n### Dispatching to multiple dispatchers\n\nThere may be a need to dispatch an event via multiple dispatchers at once. It could be achieved like the following:\n\n```php\nuse Yiisoft\\EventDispatcher\\Dispatcher\\CompositeDispatcher;\nuse Yiisoft\\EventDispatcher\\Dispatcher\\Dispatcher;\nuse Yiisoft\\EventDispatcher\\Provider\\ListenerCollection;\nuse Yiisoft\\EventDispatcher\\Provider\\Provider;\n\n// Add some listeners.\n$listeners1 = (new ListenerCollection())\n    -\u003eadd(function (AfterDocumentProcessed $event) {\n        $document = $event-\u003egetDocument();\n        // Do something with document.\n    });\n\n$provider1 = new Provider($listeners1);\n\n// Add some listeners.\n$listeners2 = (new ListenerCollection())\n    -\u003eadd(function (AfterDocumentProcessed $event) {\n        $document = $event-\u003egetDocument();\n        // Do something with document.\n    });\n\n$provider2 = new Provider($listeners2);\n\n\n$dispatcher = new CompositeDispatcher();\n$dispatcher-\u003eattach(new Dispatcher($provider1));\n$dispatcher-\u003eattach(new Dispatcher($provider2));\n\n$dispatcher-\u003edispatch(new MyEvent());\n```\n\n## Documentation\n\n- [Internals](docs/internals.md)\n\nIf you need help or have a question, the [Yii Forum](https://forum.yiiframework.com/c/yii-3-0/63) is a good place for that.\nYou may also check out other [Yii Community Resources](https://www.yiiframework.com/community).\n\n## License\n\nThe Yii Event Dispatcher is free software. It is released under the terms of the BSD License.\nPlease see [`LICENSE`](./LICENSE.md) for more information.\n\nMaintained by [Yii Software](https://www.yiiframework.com/).\n\n## Credits\n\n- Larry Garfield (@crell) for initial implementation of deriving callable parameter type.\n\n## Support the project\n\n[![Open Collective](https://img.shields.io/badge/Open%20Collective-sponsor-7eadf1?logo=open%20collective\u0026logoColor=7eadf1\u0026labelColor=555555)](https://opencollective.com/yiisoft)\n\n## Follow updates\n\n[![Official website](https://img.shields.io/badge/Powered_by-Yii_Framework-green.svg?style=flat)](https://www.yiiframework.com/)\n[![Twitter](https://img.shields.io/badge/twitter-follow-1DA1F2?logo=twitter\u0026logoColor=1DA1F2\u0026labelColor=555555?style=flat)](https://twitter.com/yiiframework)\n[![Telegram](https://img.shields.io/badge/telegram-join-1DA1F2?style=flat\u0026logo=telegram)](https://t.me/yii3en)\n[![Facebook](https://img.shields.io/badge/facebook-join-1DA1F2?style=flat\u0026logo=facebook\u0026logoColor=ffffff)](https://www.facebook.com/groups/yiitalk)\n[![Slack](https://img.shields.io/badge/slack-join-1DA1F2?style=flat\u0026logo=slack)](https://yiiframework.com/go/slack)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyiisoft%2Fevent-dispatcher","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyiisoft%2Fevent-dispatcher","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyiisoft%2Fevent-dispatcher/lists"}