{"id":18750642,"url":"https://github.com/webiny/eventmanager","last_synced_at":"2025-09-03T13:07:10.540Z","repository":{"id":20227028,"uuid":"23498868","full_name":"webiny/EventManager","owner":"webiny","description":"[READ-ONLY] EventManager is a PHP component that allows you to easily manage events throughout your application. (master at Webiny/Framework)","archived":false,"fork":false,"pushed_at":"2017-11-26T21:24:41.000Z","size":18,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-06-20T16:52:35.877Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://www.webiny.com/","language":"PHP","has_issues":false,"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/webiny.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}},"created_at":"2014-08-30T19:15:56.000Z","updated_at":"2023-05-21T17:34:48.000Z","dependencies_parsed_at":"2022-07-25T07:02:04.357Z","dependency_job_id":null,"html_url":"https://github.com/webiny/EventManager","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"purl":"pkg:github/webiny/EventManager","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FEventManager","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FEventManager/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FEventManager/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FEventManager/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/webiny","download_url":"https://codeload.github.com/webiny/EventManager/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FEventManager/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266535695,"owners_count":23944275,"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-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"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":[],"created_at":"2024-11-07T17:12:40.752Z","updated_at":"2025-07-22T17:03:38.303Z","avatar_url":"https://github.com/webiny.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Event Manager Component\n===============\n\n`EventManager` allows you to easily manage events throughout your application.\n\nInstall the component\n---------------------\nThe best way to install the component is using Composer.\n\n```bash\ncomposer require webiny/event-manager\n```\nFor additional versions of the package, visit the [Packagist page](https://packagist.org/packages/webiny/event-manager).\n\n## Usage\n\nAccessing `EventManager` can be done in 2 ways. The preferable way is using `EventManagerTrait`, but you can also access it directly, using `EventManager::getInstance()`. Let's see a simple example of subscribing to an event called `some.event` with an instance of `YourHandler`:\n\n```php\nclass YourClass{\n    use EventManagerTrait;\n    \n    public function index(){\n        $this-\u003eeventManager()-\u003elisten('some.event')-\u003ehandler(new YourHandler());\n    }\n}\n\n```\nYou're done! You have just subscribed to an event and the moment `some.event` is fired, your handler will process it.\n\n## Event handlers\n\nNow let's take a look at `YourHandler`. An event handler can be any class. `EventManager` will call `handle(Event $event)` method on your handler object, by default. You can, however, specify a method you want `EventManager` to call:\n\n```php\nclass YourHandler{\n\n    public function customHandle(Event $event){\n        // Do something with the $event...\n    }\n    \n}\n\n// Using your custom method\n$this-\u003eeventManager()-\u003elisten('some.event')-\u003ehandler(new YourHandler())-\u003emethod('customHandle');\n```\n\nBesides using classes, you can also respond to an event using a callable:\n\n```\n// Using callable as event handler\n\n$handler = function(Event $event){\n    // Do something with the $event...\n};\n\n$this-\u003eeventManager()-\u003elisten('some.event')-\u003ehandler($handler);\n```\n\n## Firing events\nTo fire a simple event use the following code:\n\n```php\n$this-\u003eeventManager()-\u003efire('some.event');\n```\nYou can also pass some data when firing an event, which will be passed to every event listener:\n\n```php\n$data = [\n    'some' =\u003e 'data',\n    'ip' =\u003e '192.168.1.10'\n];\n\n$this-\u003eeventManager()-\u003efire('some.event', $data);\n```\n\nAny given data that is not an `Event` object, will be converted to generic `Event` object and your data will be accessible either by using array keys, or as object properties:\n\n```php\nclass YourHandler{\n\n    public function customHandle(Event $event){\n        // Access your data \n        echo $event-\u003esome; // 'data'\n        echo $event['some'] // 'data'\n    }\n    \n}\n```\n\nIf you want to use custom `Event` data types, refer to section [Custom event classes](#custom-event-classes)\n\n## Firing events using a wildcard\nYou can also use wildcard to fire multiple events at once. The following code will fire all events starting with `event.` and pass `$data` to each one of them:\n```php\n$this-\u003eeventManager()-\u003efire('event.*', $data);\n```\n\n## Execution priority\n`EventManager` allows you to specify an execution priority using `priority()` method. Here's an example:\n\n```php\n// Specify a priority of execution for your event listeners\n$this-\u003eeventManager()-\u003elisten('some.event')-\u003ehandler(new YourHandler())-\u003emethod('customHandler')-\u003epriority(250);\n$this-\u003eeventManager()-\u003elisten('some.event')-\u003ehandler(new YourHandler())-\u003emethod('secondCustomHandler')-\u003epriority(400);\n$this-\u003eeventManager()-\u003elisten('some.event')-\u003ehandler(new YourHandler())-\u003emethod('thirdCustomHandler');\n\n// Now let's fire an event\n$this-\u003eeventManager()-\u003efire('some.event');\n```\n\nAfter firing an event, the event listeners will be ordered by priority in descending order. The higher the priority, the sooner the listener will be executed. In this example, the order of execution will be as follows: `secondCustomHandler`, `customHandler`, `thirdCustomHandler`. Default priority is `101`, so `thirdCustomHandler` is executed last.\n\n## Custom event classes\n\nWhen firing events, you can also pass your own event classes, that extend generic `Event` class. For example, you want to fire an event called `cms.page_saved` and pass the `Page` object. Of course, you could simply pass an array like `['page' =\u003e $pageObject]`, but for the sake of the example, let's pretend it's more complicated than that:\n\n```php\n// Create your `PageEvent` class\n\nclass PageEvent extends Event{\n\n    private $_page;\n\n    public function __construct(Page $page){\n        // Call constructor of parent Event class\n        parent::__construct();\n        \n        // Set your page object\n        $this-\u003e_page = $page;\n    }\n\n    public function getPage(){\n        return $this-\u003e_page;\n    }\n    \n}\n\n// Fire an event\n\n$pageEvent = new PageEvent($pageObject);\n\n$this-\u003eeventManager()-\u003efire('cms.page_saved', $pageEvent);\n\n// In your handler, you can now access page object using $event-\u003egetPage()\n\nclass YourHandler{\n\n    public function customHandle(PageEvent $event){\n        $pageObject = $event-\u003egetPage();\n    }\n    \n}\n\n```\n\nThis is a simple example, but it shows the power of creating your own `Event` classes and add as much functionality to your events as you need.\n\n## Event subscriber\n\nAnother cool feature of the `EventManager` is the ability to subscribe to multiple events at once. You will need to create a subscriber class implementing `EventSubscriberInterface`:\n\n```php\nclass PageEventSubscriber implements EventSubscriberInterface {\n    use EventManagerTrait;\n\n    /**\n     * Handle page creation event\n     */\n    public function onPageCreated($event)\n    {\n        //\n    }\n\n    /**\n     * Handle page update\n     */\n    public function onPageUpdated($event)\n    {\n        //\n    }\n\n    /**\n     * Register the listeners for the subscriber.\n     */\n    public function subscribe()\n    {\n        $this-\u003eeventManager()-\u003elisten('cms.page_created')-\u003ehandler($this)-\u003emethod('onPageCreated');\n        $this-\u003eeventManager()-\u003elisten('cms.page_updated')-\u003ehandler($this)-\u003emethod('onPageUpdated');\n    }\n\n}\n\n// Subscriber to multiple events using your new subscriber class\n$this-\u003eeventManager()-\u003esubscribe($subscriber);\n```\n\nThere are situations when you need to temporarily disable EventManager. For example, deleting a huge portion of files that are not directly related to the application (local cache files) does not require firing all of related events. In this case use the following methods:\n\n```php\n// Disabling EventManager\n$this-\u003eeventManager()-\u003edisable();\n\n// Do some work that would fire loads of unnecessary events...\n\n// Enabling EventManager\n$this-\u003eeventManager()-\u003eenable();\n\n```\n\nResources\n---------\n\nTo run unit tests, you need to use the following command:\n\n    $ cd path/to/Webiny/Component/EventManager/\n    $ composer.phar install\n    $ phpunit","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebiny%2Feventmanager","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebiny%2Feventmanager","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebiny%2Feventmanager/lists"}