{"id":21823530,"url":"https://github.com/jbzoo/event","last_synced_at":"2025-08-08T05:35:18.590Z","repository":{"id":3683676,"uuid":"50533635","full_name":"JBZoo/Event","owner":"JBZoo","description":"The EventEmitter is a simple pattern that allows you to create an object that emits events, and allow you to listen to those events.","archived":false,"fork":false,"pushed_at":"2024-01-28T08:57:57.000Z","size":97,"stargazers_count":27,"open_issues_count":0,"forks_count":2,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-27T18:21:28.215Z","etag":null,"topics":["callback","emitter","event-management","events","jbzoo","listen","listner","observer"],"latest_commit_sha":null,"homepage":"","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/JBZoo.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2016-01-27T19:55:48.000Z","updated_at":"2025-03-21T10:14:47.000Z","dependencies_parsed_at":"2024-01-28T09:51:48.479Z","dependency_job_id":null,"html_url":"https://github.com/JBZoo/Event","commit_stats":{"total_commits":103,"total_committers":6,"mean_commits":"17.166666666666668","dds":0.6893203883495146,"last_synced_commit":"09c87f83acc79252cc7f01b173c4c6eb399e62a1"},"previous_names":[],"tags_count":26,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JBZoo%2FEvent","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JBZoo%2FEvent/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JBZoo%2FEvent/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JBZoo%2FEvent/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JBZoo","download_url":"https://codeload.github.com/JBZoo/Event/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248821691,"owners_count":21166931,"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":["callback","emitter","event-management","events","jbzoo","listen","listner","observer"],"created_at":"2024-11-27T17:33:15.417Z","updated_at":"2025-04-14T04:31:08.529Z","avatar_url":"https://github.com/JBZoo.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JBZoo / Event\n\n[![CI](https://github.com/JBZoo/Event/actions/workflows/main.yml/badge.svg?branch=master)](https://github.com/JBZoo/Event/actions/workflows/main.yml?query=branch%3Amaster)    [![Coverage Status](https://coveralls.io/repos/github/JBZoo/Event/badge.svg?branch=master)](https://coveralls.io/github/JBZoo/Event?branch=master)    [![Psalm Coverage](https://shepherd.dev/github/JBZoo/Event/coverage.svg)](https://shepherd.dev/github/JBZoo/Event)    [![Psalm Level](https://shepherd.dev/github/JBZoo/Event/level.svg)](https://shepherd.dev/github/JBZoo/Event)    [![CodeFactor](https://www.codefactor.io/repository/github/jbzoo/event/badge)](https://www.codefactor.io/repository/github/jbzoo/event/issues)    \n[![Stable Version](https://poser.pugx.org/jbzoo/event/version)](https://packagist.org/packages/jbzoo/event/)    [![Total Downloads](https://poser.pugx.org/jbzoo/event/downloads)](https://packagist.org/packages/jbzoo/event/stats)    [![Dependents](https://poser.pugx.org/jbzoo/event/dependents)](https://packagist.org/packages/jbzoo/event/dependents?order_by=downloads)    [![GitHub License](https://img.shields.io/github/license/jbzoo/event)](https://github.com/JBZoo/Event/blob/master/LICENSE)\n\n\nThe EventEmitter is a simple pattern that allows you to create an object that emits events, and allow you to listen to those events.\n\n### Install\n```sh\ncomposer require jbzoo/event\n```\n\n\n### Simple example\n```php\nuse JBZoo\\Event\\EventManager;\n\n$eManager = new EventManager();\n\n// Simple\n$eManager-\u003eon('create', function () {\n    echo \"Something action\";\n});\n\n// Just do it!\n$eManager-\u003etrigger('create');\n```\n\n\n### Set priority\nBy supplying a priority, you are ensured that subscribers handle in a specific order. The default priority is EventManager::MID.\nAnything below that will be triggered earlier, anything higher later.\nIf there are two subscribers with the same priority, they will execute in an undefined, but deterministic order.\n```php\n// Run it first\n$eManager-\u003eon('create', function () {\n    echo \"Something high priority action\";\n}, EventManager::HIGH);\n\n// Run it latest\n$eManager-\u003eon('create', function () {\n    echo \"Something another action\";\n}, EventManager::LOW);\n\n// Custom index\n$eManager-\u003eon('create', function () {\n    echo \"Something action\";\n}, 42);\n\n// Don't care...\n$eManager-\u003eon('create', function () {\n    echo \"Something action\";\n});\n```\n\n### Types of Callback\nAll default PHP callbacks are supported, so closures are not required.\n```php\n$eManager-\u003eon('create', function(){ /* ... */ }); // Custom function\n$eManager-\u003eon('create', 'myFunction');            // Custom function name\n$eManager-\u003eon('create', ['myClass', 'myMethod']); // Static function\n$eManager-\u003eon('create', [$object, 'Method']);     // Method of instance\n```\n\n\n###  Cancel queue of events\n```php\nuse JBZoo\\Event\\ExceptionStop;\n\n$eManager-\u003eon('create', function () {\n    throw new ExceptionStop('Some reason'); // Special exception for JBZoo/Event\n});\n\n$eManager-\u003etrigger('create'); // return 'Some reason' or TRUE if all events done\n```\n\n\n### Passing arguments\nArguments can be passed as an array.\n```php\n$eManager-\u003eon('create', function ($entityId) {\n    echo \"An entity with id \", $entityId, \" just got created.\\n\";\n});\n$entityId = 5;\n$eManager-\u003etrigger('create', [$entityId]);\n```\n\nBecause you cannot really do anything with the return value of a listener, you can pass arguments by reference to communicate between listeners and back to the emitter.\n```php\n$eManager-\u003eon('create', function ($entityId, \u0026$warnings) {\n    echo \"An entity with id \", $entityId, \" just got created.\\n\";\n    $warnings[] = \"Something bad may or may not have happened.\\n\";\n});\n$warnings = [];\n$eManager-\u003etrigger('create', [$entityId, \u0026$warnings]);\n```\n\n### Namespaces\n```php\n$eManager-\u003eon('item.*', function () {\n    // item.init\n    // item.save\n    echo \"Any actions with item\";\n});\n\n$eManager-\u003eon('*.init', function () {\n    // tag.init\n    // item.init\n    echo \"Init any entity\";\n});\n\n$eManager-\u003eon('*.save', function () {\n    // tag.save\n    // item.save\n    echo \"Saving any entity in system\";\n});\n\n$eManager-\u003eon('*.save.after', function () {\n    // tag.save.after\n    // item.save.after\n    echo \"Any entity on after save\";\n});\n\n$eManager-\u003etrigger('tag.init');\n$eManager-\u003etrigger('tag.save.before');\n$eManager-\u003etrigger('tag.save');\n$eManager-\u003etrigger('tag.save.after');\n\n$eManager-\u003etrigger('item.init');\n$eManager-\u003etrigger('item.save.before');\n$eManager-\u003etrigger('item.save');\n$eManager-\u003etrigger('item.save.after');\n```\n\n\n## Summary benchmark info (execution time) PHP v7.4\nAll benchmark tests are executing without xdebug and with a huge random array and 100.000 iterations.\n\nBenchmark tests based on the tool [phpbench/phpbench](https://github.com/phpbench/phpbench). See details [here](tests/phpbench).   \n\nPlease, pay attention - `1μs = 1/1.000.000 of second!`\n\n**benchmark: ManyCallbacks**\n\nsubject | groups | its | revs | mean | stdev | rstdev | mem_real | diff\n --- | --- | --- | --- | --- | --- | --- | --- | --- \nbenchOneUndefined | undefined | 10 | 100000 | 0.65μs | 0.01μs | 1.00% | 6,291,456b | 1.00x\nbenchOneWithStarBegin | *.bar | 10 | 100000 | 0.67μs | 0.01μs | 1.44% | 6,291,456b | 1.04x\nbenchOneWithAllStars | \\*.\\* | 10 | 100000 | 0.68μs | 0.03μs | 4.18% | 6,291,456b | 1.04x\nbenchOneWithStarEnd | foo.* | 10 | 100000 | 0.68μs | 0.01μs | 1.24% | 6,291,456b | 1.04x\nbenchOneNested | foo.bar | 10 | 100000 | 43.23μs | 0.46μs | 1.07% | 6,291,456b | 66.56x\nbenchOneSimple | foo | 10 | 100000 | 45.07μs | 2.63μs | 5.83% | 6,291,456b | 69.39x\n\n**benchmark: ManyCallbacksWithPriority**\n\nsubject | groups | its | revs | mean | stdev | rstdev | mem_real | diff\n --- | --- | --- | --- | --- | --- | --- | --- | --- \nbenchOneUndefined | undefined | 10 | 100000 | 0.65μs | 0.01μs | 1.35% | 6,291,456b | 1.00x\nbenchOneNestedStarAll | \\*.\\* | 10 | 100000 | 0.67μs | 0.01μs | 1.34% | 6,291,456b | 1.03x\nbenchOneWithStarBegin | *.bar | 10 | 100000 | 0.67μs | 0.01μs | 1.10% | 6,291,456b | 1.04x\nbenchOneWithStarEnd | foo.* | 10 | 100000 | 0.68μs | 0.01μs | 1.13% | 6,291,456b | 1.05x\nbenchOneSimple | foo | 10 | 100000 | 4.54μs | 0.02μs | 0.35% | 6,291,456b | 7.03x\nbenchOneNested | foo.bar | 10 | 100000 | 4.58μs | 0.04μs | 0.81% | 6,291,456b | 7.10x\n\n**benchmark: OneCallback**\n\nsubject | groups | its | revs | mean | stdev | rstdev | mem_real | diff\n --- | --- | --- | --- | --- | --- | --- | --- | --- \nbenchOneWithStarBegin | *.bar | 10 | 100000 | 0.69μs | 0.03μs | 4.00% | 6,291,456b | 1.00x\nbenchOneWithStarEnd | foo.* | 10 | 100000 | 0.70μs | 0.03μs | 4.22% | 6,291,456b | 1.00x\nbenchOneNestedStarAll | \\*.\\* | 10 | 100000 | 0.70μs | 0.04μs | 6.02% | 6,291,456b | 1.01x\nbenchOneUndefined | undefined | 10 | 100000 | 0.71μs | 0.05μs | 7.44% | 6,291,456b | 1.02x\nbenchOneSimple | foo | 10 | 100000 | 1.18μs | 0.03μs | 2.27% | 6,291,456b | 1.70x\nbenchOneNested | foo.bar | 10 | 100000 | 1.25μs | 0.03μs | 2.46% | 6,291,456b | 1.81x\n\n**benchmark: Random**\n\nsubject | groups | its | revs | mean | stdev | rstdev | mem_real | diff\n --- | --- | --- | --- | --- | --- | --- | --- | --- \nbenchOneSimple | random.*.triggers | 10 | 100000 | 4.29μs | 0.33μs | 7.69% | 6,291,456b | 1.00x\n\n\n## Unit tests and check code style\n```sh\nmake update\nmake test-all\n```\n\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjbzoo%2Fevent","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjbzoo%2Fevent","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjbzoo%2Fevent/lists"}