| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Ubiquity\events; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Ubiquity\cache\CacheManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * Manage events | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Ubiquity\contents\transformation\events$EventsManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * This class is part of Ubiquity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @author jcheron <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @version 1.0.2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class EventsManager { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     private static string $key = 'events/events'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var array|mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected static $managedEvents = []; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 25 | 3 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 | 3 |  |     public static function addOneListener(string $eventName, $action):void { | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |         self::$managedEvents[$eventName] ??= []; | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         self::$managedEvents[$eventName][] = $action; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * Starts the event manager (in app/config/services.php) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public static function start():void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         if (CacheManager::$cache->exists(self::$key)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 8 |  |             self::$managedEvents = CacheManager::$cache->fetch(self::$key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 8 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 8 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * Adds a listener on eventName. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @param string|array $eventNames | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param EventListenerInterface|callable $action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 1 |  |     public static function addListener($eventNames, $action):void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 1 |  |         if (\is_array($eventNames)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             foreach ($eventNames as $eventName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 self::addOneListener($eventName, $action); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             self::addOneListener($eventNames, $action); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 162 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 162 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 18 |  |      * Adds a list of listeners | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 18 |  |      * @param array $listeners | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     public static function addListeners(array $listeners):void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         foreach ($listeners as $eventName => $action) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             self::addOneListener($eventName, $action); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 18 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 18 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 18 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 18 |  |      * Store the managed events in cache (do not use in prod) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 18 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 18 |  |     public static function store():void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 18 |  |         CacheManager::$cache->store(self::$key, self::$managedEvents); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * Trigger an event | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param string $eventName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @param mixed ...$params | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     public static function trigger(string $eventName, &...$params):void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         if (isset(self::$managedEvents[$eventName])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             foreach (self::$managedEvents[$eventName] as $action) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 self::triggerOne($action, $params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     private static function triggerOne($action, &$params):void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         if (\is_callable($action)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             \call_user_func_array($action, $params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         } elseif (is_subclass_of($action, EventListenerInterface::class)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             \call_user_func_array([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 new $action(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 'on' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             ], $params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |  |