├── LICENSE ├── README.md ├── SECURITY.md ├── composer.json └── src ├── AbstractEvent.php ├── Command └── DebugEventDispatcherCommand.php ├── Dispatcher.php ├── DispatcherAwareInterface.php ├── DispatcherAwareTrait.php ├── DispatcherInterface.php ├── Event.php ├── EventImmutable.php ├── EventInterface.php ├── LazyServiceEventListener.php ├── ListenersPriorityQueue.php ├── Priority.php └── SubscriberInterface.php /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc. 5 | 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Library General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License 307 | along with this program; if not, write to the Free Software 308 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 309 | 310 | 311 | Also add information on how to contact you by electronic and paper mail. 312 | 313 | If the program is interactive, make it output a short notice like this 314 | when it starts in an interactive mode: 315 | 316 | Gnomovision version 69, Copyright (C) year name of author 317 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 318 | This is free software, and you are welcome to redistribute it 319 | under certain conditions; type `show c' for details. 320 | 321 | The hypothetical commands `show w' and `show c' should show the appropriate 322 | parts of the General Public License. Of course, the commands you use may 323 | be called something other than `show w' and `show c'; they could even be 324 | mouse-clicks or menu items--whatever suits your program. 325 | 326 | You should also get your employer (if you work as a programmer) or your 327 | school, if any, to sign a "copyright disclaimer" for the program, if 328 | necessary. Here is a sample; alter the names: 329 | 330 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 331 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 332 | 333 | , 1 April 1989 334 | Ty Coon, President of Vice 335 | 336 | This General Public License does not permit incorporating your program into 337 | proprietary programs. If your program is a subroutine library, you may 338 | consider it more useful to permit linking proprietary applications with the 339 | library. If this is what you want to do, use the GNU Library General 340 | Public License instead of this License. 341 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # The Event Package [![Build Status](https://ci.joomla.org/api/badges/joomla-framework/event/status.svg?ref=refs/heads/3.x-dev)](https://ci.joomla.org/joomla-framework/event) 2 | 3 | [![Latest Stable Version](https://poser.pugx.org/joomla/event/v/stable)](https://packagist.org/packages/joomla/event) 4 | [![Total Downloads](https://poser.pugx.org/joomla/event/downloads)](https://packagist.org/packages/joomla/event) 5 | [![Latest Unstable Version](https://poser.pugx.org/joomla/event/v/unstable)](https://packagist.org/packages/joomla/event) 6 | [![License](https://poser.pugx.org/joomla/event/license)](https://packagist.org/packages/joomla/event) 7 | 8 | The Joomla! **Event** package provides foundations to build event systems and an implementation supporting prioritized listeners. 9 | 10 | ## Requirements 11 | 12 | * PHP 8.1 or later 13 | 14 | ## Installation via Composer 15 | 16 | Add `"joomla/event": "~3.0"` to the require block in your composer.json and then run `composer install`. 17 | 18 | ```json 19 | { 20 | "require": { 21 | "joomla/event": "~3.0" 22 | } 23 | } 24 | ``` 25 | 26 | Alternatively, you can simply run the following from the command line: 27 | 28 | ```sh 29 | composer require joomla/event "~3.0" 30 | ``` 31 | 32 | If you want to include the test sources and docs, use 33 | 34 | ```sh 35 | composer require --prefer-source joomla/event "~3.0" 36 | ``` 37 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | ## Supported Versions 4 | 5 | These versions are currently being supported with security updates: 6 | 7 | | Version | Supported | 8 | | ------- | ------------------ | 9 | | 3.x.x | :white_check_mark: | 10 | | 2.0.x | :white_check_mark: | 11 | | 1.3.x | :x: | 12 | | < 1.3 | :x: | 13 | 14 | ## Reporting a Vulnerability 15 | 16 | To report a security issue in the core Joomla! CMS or Framework, or with a joomla.org website, please submit 17 | [the form on our portal](https://developer.joomla.org/security/contact-the-team.html) containing as much detail 18 | as possible about the issue. Additional information about our security team and their processes may be found on 19 | our [Security page](https://developer.joomla.org/security.html). 20 | 21 | To report an issue in a Joomla! extension, please submit it to the [Vulnerable Extensions List](https://vel.joomla.org/submit-vel). 22 | 23 | For support with a site which has been attacked, please visit the [Joomla! Forum](https://forum.joomla.org/viewforum.php?f=714). 24 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "joomla/event", 3 | "type": "joomla-package", 4 | "description": "Joomla Event Package", 5 | "keywords": ["joomla", "framework", "event"], 6 | "homepage": "https://github.com/joomla-framework/event", 7 | "license": "GPL-2.0-or-later", 8 | "require": { 9 | "php": "^8.1.0", 10 | "symfony/deprecation-contracts": "^2|^3" 11 | }, 12 | "require-dev": { 13 | "joomla/console": "^3.0", 14 | "phpunit/phpunit": "^9.5.28", 15 | "psr/container": "^1.0", 16 | "squizlabs/php_codesniffer": "~3.7.2", 17 | "phpstan/phpstan": "^1.10.7", 18 | "phan/phan": "^5.4.2" 19 | }, 20 | "suggest": { 21 | "joomla/console": "If you want to use the DebugEventDispatcherCommand class, please install joomla/console", 22 | "psr/container-implementation": "If you want to use the LazyServiceEventListener class, please install a PSR-11 container" 23 | }, 24 | "autoload": { 25 | "psr-4": { 26 | "Joomla\\Event\\": "src/" 27 | } 28 | }, 29 | "autoload-dev": { 30 | "psr-4": { 31 | "Joomla\\Event\\Tests\\": "Tests/" 32 | } 33 | }, 34 | "minimum-stability": "dev", 35 | "extra": { 36 | "branch-alias": { 37 | "dev-2.0-dev": "2.0-dev", 38 | "dev-3.x-dev": "3.0-dev" 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/AbstractEvent.php: -------------------------------------------------------------------------------- 1 | name = $name; 58 | $this->arguments = $arguments; 59 | } 60 | 61 | /** 62 | * Get the event name. 63 | * 64 | * @return string The event name. 65 | * 66 | * @since 1.0 67 | */ 68 | public function getName() 69 | { 70 | return $this->name; 71 | } 72 | 73 | /** 74 | * Get an event argument value. 75 | * 76 | * @param string $name The argument name. 77 | * @param mixed $default The default value if not found. 78 | * 79 | * @return mixed The argument value or the default value. 80 | * 81 | * @since 1.0 82 | */ 83 | public function getArgument($name, $default = null) 84 | { 85 | if (isset($this->arguments[$name])) { 86 | return $this->arguments[$name]; 87 | } 88 | 89 | return $default; 90 | } 91 | 92 | /** 93 | * Tell if the given event argument exists. 94 | * 95 | * @param string $name The argument name. 96 | * 97 | * @return boolean True if it exists, false otherwise. 98 | * 99 | * @since 1.0 100 | */ 101 | public function hasArgument($name) 102 | { 103 | return isset($this->arguments[$name]); 104 | } 105 | 106 | /** 107 | * Get all event arguments. 108 | * 109 | * @return array An associative array of argument names as keys and their values as values. 110 | * 111 | * @since 1.0 112 | */ 113 | public function getArguments() 114 | { 115 | return $this->arguments; 116 | } 117 | 118 | /** 119 | * Tell if the event propagation is stopped. 120 | * 121 | * @return boolean True if stopped, false otherwise. 122 | * 123 | * @since 1.0 124 | */ 125 | public function isStopped() 126 | { 127 | return $this->stopped === true; 128 | } 129 | 130 | /** 131 | * Stops the propagation of the event to further event listeners. 132 | * 133 | * @return void 134 | * 135 | * @since 2.0.0 136 | */ 137 | public function stopPropagation(): void 138 | { 139 | $this->stopped = true; 140 | } 141 | 142 | /** 143 | * Count the number of arguments. 144 | * 145 | * @return integer The number of arguments. 146 | * 147 | * @since 1.0 148 | */ 149 | #[\ReturnTypeWillChange] 150 | public function count() 151 | { 152 | return \count($this->arguments); 153 | } 154 | 155 | /** 156 | * Serialize the event. 157 | * 158 | * @return string The serialized event. 159 | * 160 | * @since 1.0 161 | */ 162 | public function serialize() 163 | { 164 | return serialize($this->__serialize()); 165 | } 166 | 167 | /** 168 | * Serialize the event. 169 | * 170 | * @return array The data to be serialized 171 | * 172 | * @since 2.0.0 173 | */ 174 | public function __serialize() 175 | { 176 | return [ 177 | 'name' => $this->name, 178 | 'arguments' => $this->arguments, 179 | 'stopped' => $this->stopped, 180 | ]; 181 | } 182 | 183 | /** 184 | * Unserialize the event. 185 | * 186 | * @param string $serialized The serialized event. 187 | * 188 | * @return void 189 | * 190 | * @since 1.0 191 | */ 192 | public function unserialize($serialized) 193 | { 194 | $this->__unserialize(unserialize($serialized)); 195 | } 196 | 197 | /** 198 | * Unserialize the event. 199 | * 200 | * @param array $data The serialized event. 201 | * 202 | * @return void 203 | * 204 | * @since 2.0.0 205 | */ 206 | public function __unserialize(array $data) 207 | { 208 | $this->name = $data['name']; 209 | $this->arguments = $data['arguments']; 210 | $this->stopped = $data['stopped']; 211 | } 212 | 213 | /** 214 | * Tell if the given event argument exists. 215 | * 216 | * @param string $name The argument name. 217 | * 218 | * @return boolean True if it exists, false otherwise. 219 | * 220 | * @since 1.0 221 | */ 222 | #[\ReturnTypeWillChange] 223 | public function offsetExists($name) 224 | { 225 | return $this->hasArgument($name); 226 | } 227 | 228 | /** 229 | * Get an event argument value. 230 | * 231 | * @param string $name The argument name. 232 | * 233 | * @return mixed The argument value or null if not existing. 234 | * 235 | * @since 1.0 236 | */ 237 | #[\ReturnTypeWillChange] 238 | public function offsetGet($name) 239 | { 240 | return $this->getArgument($name); 241 | } 242 | } 243 | -------------------------------------------------------------------------------- /src/Command/DebugEventDispatcherCommand.php: -------------------------------------------------------------------------------- 1 | setDispatcher($dispatcher); 48 | 49 | parent::__construct(); 50 | } 51 | 52 | /** 53 | * Configure the command. 54 | * 55 | * @return void 56 | * 57 | * @since 2.0.0 58 | */ 59 | protected function configure(): void 60 | { 61 | $this->setDescription("Displays information about the application's event dispatcher"); 62 | $this->addArgument('event', InputArgument::OPTIONAL, 'Show the listeners for a specific event'); 63 | $this->setHelp(<<<'EOF' 64 | The %command.name% command lists all of the registered event handlers in an application's event dispatcher: 65 | 66 | php %command.full_name% 67 | 68 | To get specific listeners for an event, specify its name: 69 | 70 | php %command.full_name% application.before_execute 71 | EOF 72 | ); 73 | } 74 | 75 | /** 76 | * Internal function to execute the command. 77 | * 78 | * @param InputInterface $input The input to inject into the command. 79 | * @param OutputInterface $output The output to inject into the command. 80 | * 81 | * @return integer The command exit code 82 | * 83 | * @since 2.0.0 84 | */ 85 | protected function doExecute(InputInterface $input, OutputInterface $output): int 86 | { 87 | $io = new SymfonyStyle($input, $output); 88 | 89 | if ($event = $input->getArgument('event')) { 90 | $listeners = $this->dispatcher->getListeners($event); 91 | 92 | if (empty($listeners)) { 93 | $io->warning(sprintf('The event "%s" does not have any registered listeners.', $event)); 94 | 95 | return 0; 96 | } 97 | 98 | $io->title(sprintf('%s Registered Listeners for "%s" Event', $this->getApplication()->getName(), $event)); 99 | 100 | $this->renderEventListenerTable($listeners, $io); 101 | 102 | return 0; 103 | } 104 | 105 | $listeners = $this->dispatcher->getListeners(); 106 | 107 | if (empty($listeners)) { 108 | $io->comment('There are no listeners registered to the event dispatcher.'); 109 | 110 | return 0; 111 | } 112 | 113 | $io->title(sprintf('%s Registered Listeners Grouped By Event', $this->getApplication()->getName())); 114 | 115 | ksort($listeners); 116 | 117 | foreach ($listeners as $subscribedEvent => $eventListeners) { 118 | $io->section(sprintf('"%s" event', $subscribedEvent)); 119 | 120 | $this->renderEventListenerTable($eventListeners, $io); 121 | } 122 | 123 | return 0; 124 | } 125 | 126 | /** 127 | * Formats a callable resource to be displayed in the console output 128 | * 129 | * @param callable $callable A callable resource to format 130 | * 131 | * @return string 132 | * 133 | * @since 2.0.0 134 | * @throws \ReflectionException 135 | * @note This method is based on \Symfony\Bundle\FrameworkBundle\Console\Descriptor\TextDescriptor::formatCallable() 136 | */ 137 | private function formatCallable($callable): string 138 | { 139 | if (\is_array($callable)) { 140 | if (\is_object($callable[0])) { 141 | return sprintf('%s::%s()', \get_class($callable[0]), $callable[1]); 142 | } 143 | 144 | return sprintf('%s::%s()', $callable[0], $callable[1]); 145 | } 146 | 147 | if (\is_string($callable)) { 148 | return sprintf('%s()', $callable); 149 | } 150 | 151 | if ($callable instanceof \Closure) { 152 | $r = new \ReflectionFunction($callable); 153 | 154 | if (strpos($r->name, '{closure}') !== false) { 155 | return 'Closure()'; 156 | } 157 | 158 | if (null !== $class = $r->getClosureScopeClass()) { 159 | return sprintf('%s::%s()', $class->name, $r->name); 160 | } 161 | 162 | return $r->name . '()'; 163 | } 164 | 165 | if (method_exists($callable, '__invoke')) { 166 | return sprintf('%s::__invoke()', \get_class($callable)); 167 | } 168 | 169 | throw new \InvalidArgumentException('Callable is not describable.'); 170 | } 171 | 172 | /** 173 | * Renders the table of listeners for an event 174 | * 175 | * @param array $eventListeners The listeners for an event 176 | * @param SymfonyStyle $io The I/O helper 177 | * 178 | * @return void 179 | * 180 | * @since 2.0.0 181 | */ 182 | private function renderEventListenerTable(array $eventListeners, SymfonyStyle $io): void 183 | { 184 | $tableHeaders = ['Order', 'Callable']; 185 | $tableRows = []; 186 | 187 | foreach ($eventListeners as $order => $listener) { 188 | $tableRows[] = [ 189 | sprintf('#%d', $order + 1), 190 | $this->formatCallable($listener), 191 | ]; 192 | } 193 | 194 | $io->table($tableHeaders, $tableRows); 195 | } 196 | } 197 | -------------------------------------------------------------------------------- /src/Dispatcher.php: -------------------------------------------------------------------------------- 1 | events[$event->getName()] = $event; 56 | 57 | return $this; 58 | } 59 | 60 | /** 61 | * Add an event to this dispatcher, only if it is not existing. 62 | * 63 | * @param EventInterface $event The event. 64 | * 65 | * @return $this 66 | * 67 | * @since 1.0 68 | * @deprecated 3.0 Default event objects will no longer be supported 69 | */ 70 | public function addEvent(EventInterface $event) 71 | { 72 | trigger_deprecation( 73 | 'joomla/event', 74 | '2.0.0', 75 | '%s() is deprecated and will be removed in 3.0.', 76 | __METHOD__ 77 | ); 78 | 79 | if (!isset($this->events[$event->getName()])) { 80 | $this->events[$event->getName()] = $event; 81 | } 82 | 83 | return $this; 84 | } 85 | 86 | /** 87 | * Tell if the given event has been added to this dispatcher. 88 | * 89 | * @param EventInterface|string $event The event object or name. 90 | * 91 | * @return boolean True if the listener has the given event, false otherwise. 92 | * 93 | * @since 1.0 94 | * @deprecated 3.0 Default event objects will no longer be supported 95 | */ 96 | public function hasEvent($event) 97 | { 98 | trigger_deprecation( 99 | 'joomla/event', 100 | '2.0.0', 101 | '%s() is deprecated and will be removed in 3.0.', 102 | __METHOD__ 103 | ); 104 | 105 | if ($event instanceof EventInterface) { 106 | $event = $event->getName(); 107 | } 108 | 109 | return isset($this->events[$event]); 110 | } 111 | 112 | /** 113 | * Get the event object identified by the given name. 114 | * 115 | * @param string $name The event name. 116 | * @param mixed $default The default value if the event was not registered. 117 | * 118 | * @return EventInterface|mixed The event of the default value. 119 | * 120 | * @since 1.0 121 | * @deprecated 3.0 Default event objects will no longer be supported 122 | */ 123 | public function getEvent($name, $default = null) 124 | { 125 | trigger_deprecation( 126 | 'joomla/event', 127 | '2.0.0', 128 | '%s() is deprecated and will be removed in 3.0.', 129 | __METHOD__ 130 | ); 131 | 132 | if (isset($this->events[$name])) { 133 | return $this->events[$name]; 134 | } 135 | 136 | return $default; 137 | } 138 | 139 | /** 140 | * Remove an event from this dispatcher. The registered listeners will remain. 141 | * 142 | * @param EventInterface|string $event The event object or name. 143 | * 144 | * @return $this 145 | * 146 | * @since 1.0 147 | * @deprecated 3.0 Default event objects will no longer be supported 148 | */ 149 | public function removeEvent($event) 150 | { 151 | trigger_deprecation( 152 | 'joomla/event', 153 | '2.0.0', 154 | '%s() is deprecated and will be removed in 3.0.', 155 | __METHOD__ 156 | ); 157 | 158 | if ($event instanceof EventInterface) { 159 | $event = $event->getName(); 160 | } 161 | 162 | if (isset($this->events[$event])) { 163 | unset($this->events[$event]); 164 | } 165 | 166 | return $this; 167 | } 168 | 169 | /** 170 | * Get the registered events. 171 | * 172 | * @return EventInterface[] The registered event. 173 | * 174 | * @since 1.0 175 | * @deprecated 3.0 Default event objects will no longer be supported 176 | */ 177 | public function getEvents() 178 | { 179 | trigger_deprecation( 180 | 'joomla/event', 181 | '2.0.0', 182 | '%s() is deprecated and will be removed in 3.0.', 183 | __METHOD__ 184 | ); 185 | 186 | return $this->events; 187 | } 188 | 189 | /** 190 | * Clear all events. 191 | * 192 | * @return EventInterface[] The old events. 193 | * 194 | * @since 1.0 195 | * @deprecated 3.0 Default event objects will no longer be supported 196 | */ 197 | public function clearEvents() 198 | { 199 | trigger_deprecation( 200 | 'joomla/event', 201 | '2.0.0', 202 | '%s() is deprecated and will be removed in 3.0.', 203 | __METHOD__ 204 | ); 205 | 206 | $events = $this->events; 207 | $this->events = []; 208 | 209 | return $events; 210 | } 211 | 212 | /** 213 | * Count the number of registered event. 214 | * 215 | * @return integer The number of registered events. 216 | * 217 | * @since 1.0 218 | * @deprecated 3.0 Default event objects will no longer be supported 219 | */ 220 | public function countEvents() 221 | { 222 | trigger_deprecation( 223 | 'joomla/event', 224 | '2.0.0', 225 | '%s() is deprecated and will be removed in 3.0.', 226 | __METHOD__ 227 | ); 228 | 229 | return \count($this->events); 230 | } 231 | 232 | /** 233 | * Attaches a listener to an event 234 | * 235 | * @param string $eventName The event to listen to. 236 | * @param callable $callback A callable function 237 | * @param integer $priority The priority at which the $callback executed 238 | * 239 | * @return boolean 240 | * 241 | * @since 1.0 242 | */ 243 | public function addListener(string $eventName, callable $callback, int $priority = 0): bool 244 | { 245 | if (!isset($this->listeners[$eventName])) { 246 | $this->listeners[$eventName] = new ListenersPriorityQueue(); 247 | } 248 | 249 | $this->listeners[$eventName]->add($callback, $priority); 250 | 251 | return true; 252 | } 253 | 254 | /** 255 | * Get the priority of the given listener for the given event. 256 | * 257 | * @param string $eventName The event to listen to. 258 | * @param callable $callback A callable function 259 | * 260 | * @return mixed The listener priority or null if the listener doesn't exist. 261 | * 262 | * @since 1.0 263 | */ 264 | public function getListenerPriority($eventName, callable $callback) 265 | { 266 | if (isset($this->listeners[$eventName])) { 267 | return $this->listeners[$eventName]->getPriority($callback); 268 | } 269 | } 270 | 271 | /** 272 | * Get the listeners registered to the given event. 273 | * 274 | * @param string|null $event The event to fetch listeners for or null to fetch all listeners 275 | * 276 | * @return callable[] An array of registered listeners sorted according to their priorities. 277 | * 278 | * @since 1.0 279 | */ 280 | public function getListeners(?string $event = null) 281 | { 282 | if ($event !== null) { 283 | if (isset($this->listeners[$event])) { 284 | return $this->listeners[$event]->getAll(); 285 | } 286 | 287 | return []; 288 | } 289 | 290 | $dispatcherListeners = []; 291 | 292 | foreach ($this->listeners as $registeredEvent => $listeners) { 293 | $dispatcherListeners[$registeredEvent] = $listeners->getAll(); 294 | } 295 | 296 | return $dispatcherListeners; 297 | } 298 | 299 | /** 300 | * Tell if the given listener has been added. 301 | * 302 | * If an event is specified, it will tell if the listener is registered for that event. 303 | * 304 | * @param callable $callback The callable to check is listening to the event. 305 | * @param ?string $eventName An optional event name to check a listener is subscribed to. 306 | * 307 | * @return boolean True if the listener is registered, false otherwise. 308 | * 309 | * @since 1.0 310 | */ 311 | public function hasListener(callable $callback, ?string $eventName = null) 312 | { 313 | if ($eventName) { 314 | if (isset($this->listeners[$eventName])) { 315 | return $this->listeners[$eventName]->has($callback); 316 | } 317 | } else { 318 | foreach ($this->listeners as $queue) { 319 | if ($queue->has($callback)) { 320 | return true; 321 | } 322 | } 323 | } 324 | 325 | return false; 326 | } 327 | 328 | /** 329 | * Removes an event listener from the specified event. 330 | * 331 | * @param string $eventName The event to remove a listener from. 332 | * @param callable $listener The listener to remove. 333 | * 334 | * @return void 335 | * 336 | * @since 2.0.0 337 | */ 338 | public function removeListener(string $eventName, callable $listener): void 339 | { 340 | if (isset($this->listeners[$eventName])) { 341 | $this->listeners[$eventName]->remove($listener); 342 | } 343 | } 344 | 345 | /** 346 | * Clear the listeners in this dispatcher. 347 | * 348 | * If an event is specified, the listeners will be cleared only for that event. 349 | * 350 | * @param string $event The event name. 351 | * 352 | * @return $this 353 | * 354 | * @since 1.0 355 | */ 356 | public function clearListeners($event = null) 357 | { 358 | if ($event) { 359 | if (isset($this->listeners[$event])) { 360 | unset($this->listeners[$event]); 361 | } 362 | } else { 363 | $this->listeners = []; 364 | } 365 | 366 | return $this; 367 | } 368 | 369 | /** 370 | * Count the number of registered listeners for the given event. 371 | * 372 | * @param string $event The event name. 373 | * 374 | * @return integer 375 | * 376 | * @since 1.0 377 | */ 378 | public function countListeners($event) 379 | { 380 | return isset($this->listeners[$event]) ? \count($this->listeners[$event]) : 0; 381 | } 382 | 383 | /** 384 | * Adds an event subscriber. 385 | * 386 | * @param SubscriberInterface $subscriber The subscriber. 387 | * 388 | * @return void 389 | * 390 | * @since 2.0.0 391 | */ 392 | public function addSubscriber(SubscriberInterface $subscriber): void 393 | { 394 | foreach ($subscriber->getSubscribedEvents() as $eventName => $params) { 395 | if (\is_array($params)) { 396 | $this->addListener($eventName, [$subscriber, $params[0]], $params[1] ?? Priority::NORMAL); 397 | } else { 398 | $this->addListener($eventName, [$subscriber, $params]); 399 | } 400 | } 401 | } 402 | 403 | /** 404 | * Removes an event subscriber. 405 | * 406 | * @param SubscriberInterface $subscriber The subscriber. 407 | * 408 | * @return void 409 | * 410 | * @since 2.0.0 411 | */ 412 | public function removeSubscriber(SubscriberInterface $subscriber): void 413 | { 414 | foreach ($subscriber->getSubscribedEvents() as $eventName => $params) { 415 | if (\is_array($params)) { 416 | $this->removeListener($eventName, [$subscriber, $params[0]]); 417 | } else { 418 | $this->removeListener($eventName, [$subscriber, $params]); 419 | } 420 | } 421 | } 422 | 423 | /** 424 | * Dispatches an event to all registered listeners. 425 | * 426 | * @param string $name The name of the event to dispatch. 427 | * @param ?EventInterface $event The event to pass to the event handlers/listeners. 428 | * If not supplied, an empty EventInterface instance is created. 429 | * Note, not passing an event is deprecated and will be required as of 3.0. 430 | * 431 | * @return EventInterface 432 | * 433 | * @since 2.0.0 434 | */ 435 | public function dispatch(string $name, ?EventInterface $event = null): EventInterface 436 | { 437 | if (!($event instanceof EventInterface)) { 438 | trigger_deprecation( 439 | 'joomla/event', 440 | '2.0.0', 441 | 'Not passing an event object to %s() is deprecated, as of 3.0 the $event argument will be required.', 442 | __METHOD__ 443 | ); 444 | 445 | $event = $this->getDefaultEvent($name); 446 | } 447 | 448 | if (isset($this->listeners[$event->getName()])) { 449 | foreach ($this->listeners[$event->getName()] as $listener) { 450 | if ($event->isStopped()) { 451 | return $event; 452 | } 453 | 454 | $listener($event); 455 | } 456 | } 457 | 458 | return $event; 459 | } 460 | 461 | /** 462 | * Trigger an event. 463 | * 464 | * @param EventInterface|string $event The event object or name. 465 | * 466 | * @return EventInterface The event after being passed through all listeners. 467 | * 468 | * @since 1.0 469 | * @deprecated 3.0 Use dispatch() instead. 470 | */ 471 | public function triggerEvent($event) 472 | { 473 | trigger_deprecation( 474 | 'joomla/event', 475 | '2.0.0', 476 | '%s() is deprecated and will be removed in 3.0, use %s::dispatch() instead.', 477 | __METHOD__, 478 | DispatcherInterface::class 479 | ); 480 | 481 | if (!($event instanceof EventInterface)) { 482 | $event = $this->getDefaultEvent($event); 483 | } 484 | 485 | return $this->dispatch($event->getName(), $event); 486 | } 487 | 488 | /** 489 | * Get an event object for the specified event name 490 | * 491 | * @param string $name The event name to get an EventInterface object for 492 | * 493 | * @return EventInterface 494 | * 495 | * @since 2.0.0 496 | * @deprecated 3.0 Default event objects will no longer be supported 497 | */ 498 | private function getDefaultEvent(string $name): EventInterface 499 | { 500 | if (isset($this->events[$name])) { 501 | return $this->events[$name]; 502 | } 503 | 504 | return new Event($name); 505 | } 506 | } 507 | -------------------------------------------------------------------------------- /src/DispatcherAwareInterface.php: -------------------------------------------------------------------------------- 1 | dispatcher) { 38 | return $this->dispatcher; 39 | } 40 | 41 | throw new \UnexpectedValueException('Dispatcher not set in ' . __CLASS__); 42 | } 43 | 44 | /** 45 | * Set the dispatcher to use. 46 | * 47 | * @param DispatcherInterface $dispatcher The dispatcher to use. 48 | * 49 | * @return $this 50 | * 51 | * @since 1.2.0 52 | */ 53 | public function setDispatcher(DispatcherInterface $dispatcher) 54 | { 55 | $this->dispatcher = $dispatcher; 56 | 57 | return $this; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/DispatcherInterface.php: -------------------------------------------------------------------------------- 1 | arguments[$name])) { 34 | $this->arguments[$name] = $value; 35 | } 36 | 37 | return $this; 38 | } 39 | 40 | /** 41 | * Add argument to event. 42 | * 43 | * @param string $name Argument name. 44 | * @param mixed $value Value. 45 | * 46 | * @return $this 47 | * 48 | * @since 1.0 49 | */ 50 | public function setArgument($name, $value) 51 | { 52 | $this->arguments[$name] = $value; 53 | 54 | return $this; 55 | } 56 | 57 | /** 58 | * Remove an event argument. 59 | * 60 | * @param string $name The argument name. 61 | * 62 | * @return mixed The old argument value or null if it is not existing. 63 | * 64 | * @since 1.0 65 | */ 66 | public function removeArgument($name) 67 | { 68 | $return = null; 69 | 70 | if (isset($this->arguments[$name])) { 71 | $return = $this->arguments[$name]; 72 | unset($this->arguments[$name]); 73 | } 74 | 75 | return $return; 76 | } 77 | 78 | /** 79 | * Clear all event arguments. 80 | * 81 | * @return array The old arguments. 82 | * 83 | * @since 1.0 84 | */ 85 | public function clearArguments() 86 | { 87 | $arguments = $this->arguments; 88 | $this->arguments = []; 89 | 90 | return $arguments; 91 | } 92 | 93 | /** 94 | * Stop the event propagation. 95 | * 96 | * @return void 97 | * 98 | * @since 1.0 99 | * @deprecated 3.0 Use stopPropagation instead 100 | */ 101 | public function stop() 102 | { 103 | trigger_deprecation( 104 | 'joomla/event', 105 | '2.0.0', 106 | '%s() is deprecated and will be removed in 3.0, use %s::stopPropagation() instead.', 107 | __METHOD__, 108 | EventInterface::class 109 | ); 110 | 111 | $this->stopPropagation(); 112 | } 113 | 114 | /** 115 | * Set the value of an event argument. 116 | * 117 | * @param string $name The argument name. 118 | * @param mixed $value The argument value. 119 | * 120 | * @return void 121 | * 122 | * @since 1.0 123 | * @throws InvalidArgumentException If the argument name is null. 124 | */ 125 | #[\ReturnTypeWillChange] 126 | public function offsetSet($name, $value) 127 | { 128 | if ($name === null) { 129 | throw new InvalidArgumentException('The argument name cannot be null.'); 130 | } 131 | 132 | $this->setArgument($name, $value); 133 | } 134 | 135 | /** 136 | * Remove an event argument. 137 | * 138 | * @param string $name The argument name. 139 | * 140 | * @return void 141 | * 142 | * @since 1.0 143 | */ 144 | #[\ReturnTypeWillChange] 145 | public function offsetUnset($name) 146 | { 147 | $this->removeArgument($name); 148 | } 149 | } 150 | -------------------------------------------------------------------------------- /src/EventImmutable.php: -------------------------------------------------------------------------------- 1 | constructed) { 49 | throw new BadMethodCallException( 50 | sprintf('Cannot reconstruct the EventImmutable %s.', $this->name) 51 | ); 52 | } 53 | 54 | $this->constructed = true; 55 | 56 | parent::__construct($name, $arguments); 57 | } 58 | 59 | /** 60 | * Set the value of an event argument. 61 | * 62 | * @param string $name The argument name. 63 | * @param mixed $value The argument value. 64 | * 65 | * @return void 66 | * 67 | * @since 1.0 68 | * @throws BadMethodCallException 69 | */ 70 | public function offsetSet($name, $value) 71 | { 72 | throw new BadMethodCallException( 73 | sprintf( 74 | 'Cannot set the argument %s of the immutable event %s.', 75 | $name, 76 | $this->name 77 | ) 78 | ); 79 | } 80 | 81 | /** 82 | * Remove an event argument. 83 | * 84 | * @param string $name The argument name. 85 | * 86 | * @return void 87 | * 88 | * @throws BadMethodCallException 89 | * 90 | * @since 1.0 91 | */ 92 | public function offsetUnset($name) 93 | { 94 | throw new BadMethodCallException( 95 | sprintf( 96 | 'Cannot remove the argument %s of the immutable event %s.', 97 | $name, 98 | $this->name 99 | ) 100 | ); 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /src/EventInterface.php: -------------------------------------------------------------------------------- 1 | container = $container; 68 | $this->serviceId = $serviceId; 69 | $this->method = $method; 70 | } 71 | 72 | /** 73 | * Load a service from the container to listen to an event. 74 | * 75 | * @param EventInterface $event The event to process 76 | * 77 | * @return void 78 | * 79 | * @since 2.0.0 80 | * @throws \InvalidArgumentException if the constructor's $method parameter is empty when not executing a callable service 81 | * @throws \RuntimeException if the service cannot be executed 82 | */ 83 | public function __invoke(EventInterface $event): void 84 | { 85 | if (!$this->container->has($this->serviceId)) { 86 | throw new \RuntimeException( 87 | sprintf( 88 | 'The "%s" service has not been registered to the service container', 89 | $this->serviceId 90 | ) 91 | ); 92 | } 93 | 94 | $service = $this->container->get($this->serviceId); 95 | 96 | // If the service is callable on its own, just execute it 97 | if (\is_callable($service)) { 98 | \call_user_func($service, $event); 99 | 100 | return; 101 | } 102 | 103 | if (empty($this->method)) { 104 | throw new \InvalidArgumentException( 105 | sprintf( 106 | 'The $method argument is required when creating a "%s" to call a method from the "%s" service.', 107 | self::class, 108 | $this->serviceId 109 | ) 110 | ); 111 | } 112 | 113 | if (!method_exists($service, $this->method)) { 114 | throw new \RuntimeException( 115 | sprintf( 116 | 'The "%s" method does not exist on "%s" (from service "%s")', 117 | $this->method, 118 | \get_class($service), 119 | $this->serviceId 120 | ) 121 | ); 122 | } 123 | 124 | \call_user_func([$service, $this->method], $event); 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /src/ListenersPriorityQueue.php: -------------------------------------------------------------------------------- 1 | listeners[$priority][] = $callback; 41 | 42 | return $this; 43 | } 44 | 45 | /** 46 | * Remove a listener from the queue. 47 | * 48 | * @param callable $callback A callable function acting as an event listener. 49 | * 50 | * @return $this 51 | * 52 | * @since 1.0 53 | */ 54 | public function remove(callable $callback): self 55 | { 56 | foreach ($this->listeners as $priority => $listeners) { 57 | if (($key = array_search($callback, $listeners, true)) !== false) { 58 | unset($this->listeners[$priority][$key]); 59 | } 60 | } 61 | 62 | return $this; 63 | } 64 | 65 | /** 66 | * Tell if the listener exists in the queue. 67 | * 68 | * @param callable $callback A callable function acting as an event listener. 69 | * 70 | * @return boolean True if it exists, false otherwise. 71 | * 72 | * @since 1.0 73 | */ 74 | public function has(callable $callback): bool 75 | { 76 | foreach ($this->listeners as $priority => $listeners) { 77 | if (($key = array_search($callback, $listeners, true)) !== false) { 78 | return true; 79 | } 80 | } 81 | 82 | return false; 83 | } 84 | 85 | /** 86 | * Get the priority of the given listener. 87 | * 88 | * @param callable $callback A callable function acting as an event listener. 89 | * @param mixed $default The default value to return if the listener doesn't exist. 90 | * 91 | * @return mixed The listener priority if it exists or the specified default value 92 | * 93 | * @since 1.0 94 | */ 95 | public function getPriority(callable $callback, $default = null) 96 | { 97 | foreach ($this->listeners as $priority => $listeners) { 98 | if (($key = array_search($callback, $listeners, true)) !== false) { 99 | return $priority; 100 | } 101 | } 102 | 103 | return $default; 104 | } 105 | 106 | /** 107 | * Get all listeners contained in this queue, sorted according to their priority. 108 | * 109 | * @return callable[] An array of listeners. 110 | * 111 | * @since 1.0 112 | */ 113 | public function getAll(): array 114 | { 115 | if (empty($this->listeners)) { 116 | return []; 117 | } 118 | 119 | krsort($this->listeners); 120 | 121 | return \call_user_func_array('array_merge', $this->listeners); 122 | } 123 | 124 | /** 125 | * Get the priority queue. 126 | * 127 | * @return \ArrayIterator 128 | * 129 | * @since 1.0 130 | */ 131 | #[\ReturnTypeWillChange] 132 | public function getIterator() 133 | { 134 | return new \ArrayIterator($this->getAll()); 135 | } 136 | 137 | /** 138 | * Count the number of listeners in the queue. 139 | * 140 | * @return integer The number of listeners in the queue. 141 | * 142 | * @since 1.0 143 | */ 144 | #[\ReturnTypeWillChange] 145 | public function count() 146 | { 147 | $count = 0; 148 | 149 | foreach ($this->listeners as $priority) { 150 | $count += \count($priority); 151 | } 152 | 153 | return $count; 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /src/Priority.php: -------------------------------------------------------------------------------- 1 | 'methodName') 30 | * * array('eventName' => array('methodName', $priority)) 31 | * 32 | * @return array 33 | * 34 | * @since 2.0.0 35 | */ 36 | public static function getSubscribedEvents(): array; 37 | } 38 | --------------------------------------------------------------------------------