{"id":20441729,"url":"https://github.com/mix-php/event","last_synced_at":"2025-04-12T23:32:46.153Z","repository":{"id":57017770,"uuid":"203337341","full_name":"mix-php/event","owner":"mix-php","description":"Event dispatcher based on PSR-14 standard / 基于 PSR-14 标准的事件调度库","archived":false,"fork":false,"pushed_at":"2021-07-21T07:37:17.000Z","size":7,"stargazers_count":1,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-11T03:39:53.784Z","etag":null,"topics":["event","event-dispatcher","mix"],"latest_commit_sha":null,"homepage":"https://openmix.org/mix-php","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mix-php.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-20T08:51:30.000Z","updated_at":"2024-12-16T14:57:08.000Z","dependencies_parsed_at":"2022-08-22T11:31:42.972Z","dependency_job_id":null,"html_url":"https://github.com/mix-php/event","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fevent","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fevent/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fevent/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fevent/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mix-php","download_url":"https://codeload.github.com/mix-php/event/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248647257,"owners_count":21139081,"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","event-dispatcher","mix"],"created_at":"2024-11-15T09:34:28.122Z","updated_at":"2025-04-12T23:32:46.133Z","avatar_url":"https://github.com/mix-php.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003e OpenMix 出品：[https://openmix.org](https://openmix.org/mix-php)\n\n# Mix Event\n\nEvent dispatcher based on PSR-14 standard\n\n基于 PSR-14 标准的事件调度库\n\n## Installation\n\n```\ncomposer require mix/event\n```\n\n## 定义一个事件\n\n事件可以为任意类，我们以 SQL 执行事件调度来举例\n\n```php\nclass DatabaseEvent\n{\n    public $time = 0;\n    public $sql = '';\n    public $bindings = [];\n}\n```\n\n## 定义一个监听器\n\n监听器是用户编写处理事件逻辑代码的地方，`events` 方法返回一个要监听的事件类的数组，当这些事件触发时，会调用 `process` 方法\n\n```php\nclass DatabaseListener implements Mix\\Event\\ListenerInterface\n{\n\n    public function events(): array\n    {\n        // 要监听的事件数组，可监听多个事件\n        return [\n            DatabaseEvent::class,\n        ];\n    }\n\n    public function process(object $event): void\n    {\n        // 事件触发后，会执行该方法\n    }\n\n}\n```\n\n## 创建调度器\n\n创建调度器，并传入监听器，可传入多个\n\n```php\n$dispatcher = new Mix\\Event\\EventDispatcher(new DatabaseListener());\n```\n\n## 触发事件\n\n在事件产生的位置触发事件，当后面需要对该事件扩展业务逻辑时，只需在监听器中增加代码即可，达到不污染正常业务流程的目的\n\n```php\n$event = new DatabaseEvent();\n$event-\u003etime = 10;\n$event-\u003esql = 'select * from users';\n$dispatcher-\u003edispatch($event);\n```\n\n## License\n\nApache License Version 2.0, http://www.apache.org/licenses/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmix-php%2Fevent","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmix-php%2Fevent","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmix-php%2Fevent/lists"}