Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
EventsManager.php
Go to the documentation of this file.
1<?php
2namespace Ubiquity\events;
3
5
17
18 private static string $key = 'events/events';
19
24 protected static $managedEvents = [];
25
26 public static function addOneListener(string $eventName, $action):void {
27 self::$managedEvents[$eventName] ??= [];
28 self::$managedEvents[$eventName][] = $action;
29 }
30
34 public static function start():void {
35 if (CacheManager::$cache->exists(self::$key)) {
36 self::$managedEvents = CacheManager::$cache->fetch(self::$key);
37 }
38 }
39
40
46 public static function addListener($eventNames, $action):void {
47 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
60 public static function addListeners(array $listeners):void {
61 foreach ($listeners as $eventName => $action) {
62 self::addOneListener($eventName, $action);
63 }
64 }
65
69 public static function store():void {
70 CacheManager::$cache->store(self::$key, self::$managedEvents);
71 }
72
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
Manager for caches (Router, Rest, models).
static start()
Starts the event manager (in app/config/services.php)
static addListeners(array $listeners)
Adds a list of listeners.
static addOneListener(string $eventName, $action)
static store()
Store the managed events in cache (do not use in prod)
static addListener($eventNames, $action)
Adds a listener on eventName.
static triggerOne($action, &$params)
static trigger(string $eventName, &... $params)
Trigger an event.