├── LICENSE ├── README.md ├── UPGRADE.md ├── composer.json └── src ├── EventArgs.php ├── EventManager.php └── EventSubscriber.php /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2006-2015 Doctrine Project 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | this software and associated documentation files (the "Software"), to deal in 5 | the Software without restriction, including without limitation the rights to 6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 7 | of the Software, and to permit persons to whom the Software is furnished to do 8 | so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Doctrine Event Manager 2 | 3 | [![Build Status](https://github.com/doctrine/event-manager/workflows/Continuous%20Integration/badge.svg)](https://github.com/doctrine/event-manager/actions) 4 | [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/doctrine/event-manager/badges/quality-score.png?b=1.2.x)](https://scrutinizer-ci.com/g/doctrine/event-manager/?branch=1.2.x) 5 | [![Code Coverage](https://scrutinizer-ci.com/g/doctrine/event-manager/badges/coverage.png?b=1.2.x)](https://scrutinizer-ci.com/g/doctrine/event-manager/?branch=1.2.x) 6 | 7 | The Doctrine Event Manager is a library that provides a simple event system. 8 | 9 | ## More resources: 10 | 11 | * [Website](https://www.doctrine-project.org/) 12 | * [Documentation](https://www.doctrine-project.org/projects/doctrine-event-manager/en/latest/) 13 | * [Downloads](https://github.com/doctrine/event-manager/releases) 14 | -------------------------------------------------------------------------------- /UPGRADE.md: -------------------------------------------------------------------------------- 1 | # Upgrade to 2.0 2 | 3 | ## Made the `$event` parameter of `EventManager::getListeners()` mandatory 4 | 5 | When calling `EventManager::getListeners()` you need to specify the event that 6 | you want to fetch the listeners for. Call `getAllListeners()` instead if you 7 | want to access the listeners of all events. 8 | 9 | # Upgrade to 1.2 10 | 11 | ## Deprecated calling `EventManager::getListeners()` without an event name 12 | 13 | When calling `EventManager::getListeners()` without an event name, all 14 | listeners were returned, keyed by event name. A new method `getAllListeners()` 15 | has been added to provide this functionality. It should be used instead. 16 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "doctrine/event-manager", 3 | "description": "The Doctrine Event Manager is a simple PHP event system that was built to be used with the various Doctrine projects.", 4 | "license": "MIT", 5 | "type": "library", 6 | "keywords": [ 7 | "events", 8 | "event", 9 | "event dispatcher", 10 | "event manager", 11 | "event system" 12 | ], 13 | "authors": [ 14 | { 15 | "name": "Guilherme Blanco", 16 | "email": "guilhermeblanco@gmail.com" 17 | }, 18 | { 19 | "name": "Roman Borschel", 20 | "email": "roman@code-factory.org" 21 | }, 22 | { 23 | "name": "Benjamin Eberlei", 24 | "email": "kontakt@beberlei.de" 25 | }, 26 | { 27 | "name": "Jonathan Wage", 28 | "email": "jonwage@gmail.com" 29 | }, 30 | { 31 | "name": "Johannes Schmitt", 32 | "email": "schmittjoh@gmail.com" 33 | }, 34 | { 35 | "name": "Marco Pivetta", 36 | "email": "ocramius@gmail.com" 37 | } 38 | ], 39 | "homepage": "https://www.doctrine-project.org/projects/event-manager.html", 40 | "require": { 41 | "php": "^8.1" 42 | }, 43 | "require-dev": { 44 | "doctrine/coding-standard": "^12", 45 | "phpdocumentor/guides-cli": "^1.4", 46 | "phpstan/phpstan": "^1.8.8", 47 | "phpunit/phpunit": "^10.5" 48 | }, 49 | "conflict": { 50 | "doctrine/common": "<2.9" 51 | }, 52 | "autoload": { 53 | "psr-4": { 54 | "Doctrine\\Common\\": "src" 55 | } 56 | }, 57 | "autoload-dev": { 58 | "psr-4": { 59 | "Doctrine\\Tests\\Common\\": "tests" 60 | } 61 | }, 62 | "config": { 63 | "allow-plugins": { 64 | "dealerdirect/phpcodesniffer-composer-installer": true 65 | }, 66 | "sort-packages": true 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/EventArgs.php: -------------------------------------------------------------------------------- 1 | => 19 | * 20 | * @var array 21 | */ 22 | private array $listeners = []; 23 | 24 | /** 25 | * Dispatches an event to all registered listeners. 26 | * 27 | * @param string $eventName The name of the event to dispatch. The name of the event is 28 | * the name of the method that is invoked on listeners. 29 | * @param EventArgs|null $eventArgs The event arguments to pass to the event handlers/listeners. 30 | * If not supplied, the single empty EventArgs instance is used. 31 | */ 32 | public function dispatchEvent(string $eventName, EventArgs|null $eventArgs = null): void 33 | { 34 | if (! isset($this->listeners[$eventName])) { 35 | return; 36 | } 37 | 38 | $eventArgs ??= EventArgs::getEmptyInstance(); 39 | 40 | foreach ($this->listeners[$eventName] as $listener) { 41 | $listener->$eventName($eventArgs); 42 | } 43 | } 44 | 45 | /** 46 | * Gets the listeners of a specific event. 47 | * 48 | * @param string $event The name of the event. 49 | * 50 | * @return object[] 51 | */ 52 | public function getListeners(string $event): array 53 | { 54 | return $this->listeners[$event] ?? []; 55 | } 56 | 57 | /** 58 | * Gets all listeners keyed by event name. 59 | * 60 | * @return array The event listeners for the specified event, or all event listeners. 61 | */ 62 | public function getAllListeners(): array 63 | { 64 | return $this->listeners; 65 | } 66 | 67 | /** 68 | * Checks whether an event has any registered listeners. 69 | */ 70 | public function hasListeners(string $event): bool 71 | { 72 | return ! empty($this->listeners[$event]); 73 | } 74 | 75 | /** 76 | * Adds an event listener that listens on the specified events. 77 | * 78 | * @param string|string[] $events The event(s) to listen on. 79 | * @param object $listener The listener object. 80 | */ 81 | public function addEventListener(string|array $events, object $listener): void 82 | { 83 | // Picks the hash code related to that listener 84 | $hash = spl_object_hash($listener); 85 | 86 | foreach ((array) $events as $event) { 87 | // Overrides listener if a previous one was associated already 88 | // Prevents duplicate listeners on same event (same instance only) 89 | $this->listeners[$event][$hash] = $listener; 90 | } 91 | } 92 | 93 | /** 94 | * Removes an event listener from the specified events. 95 | * 96 | * @param string|string[] $events 97 | */ 98 | public function removeEventListener(string|array $events, object $listener): void 99 | { 100 | // Picks the hash code related to that listener 101 | $hash = spl_object_hash($listener); 102 | 103 | foreach ((array) $events as $event) { 104 | unset($this->listeners[$event][$hash]); 105 | } 106 | } 107 | 108 | /** 109 | * Adds an EventSubscriber. 110 | * 111 | * The subscriber is asked for all the events it is interested in and added 112 | * as a listener for these events. 113 | */ 114 | public function addEventSubscriber(EventSubscriber $subscriber): void 115 | { 116 | $this->addEventListener($subscriber->getSubscribedEvents(), $subscriber); 117 | } 118 | 119 | /** 120 | * Removes an EventSubscriber. 121 | * 122 | * The subscriber is asked for all the events it is interested in and removed 123 | * as a listener for these events. 124 | */ 125 | public function removeEventSubscriber(EventSubscriber $subscriber): void 126 | { 127 | $this->removeEventListener($subscriber->getSubscribedEvents(), $subscriber); 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /src/EventSubscriber.php: -------------------------------------------------------------------------------- 1 |