Created
June 9, 2021 08:39
-
-
Save ArrayIterator/60b2ac6f736a889936b7cfce86b663e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Event | |
| { | |
| protected static $events = []; | |
| public static function register( | |
| $name, | |
| $callable, | |
| $priority = 10 | |
| ) { | |
| self::$events[$name][] = [$priority, $callable]; | |
| uasort( | |
| self::$events[$name], | |
| fn ($a, $b) => $a[0] === $b[0] ? 0 : ($a[0] < $b[0] ? -1 : 1) | |
| ); | |
| } | |
| public static function dispatch($name) | |
| { | |
| if (($ev = self::$events[$name]??null)) { | |
| array_map( | |
| 'call_user_func', | |
| array_map(fn ($e) => $e[1], $ev) | |
| ); | |
| return; | |
| } | |
| throw new RuntimeException( | |
| 'Unregistered Event' | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment