PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.0.13
Kirki – Freeform Page Builder, Website Builder & Customizer v6.0.13
6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / libraries / framework / Managers / EventManager.php
kirki / libraries / framework / Managers Last commit date
DateManager.php 3 weeks ago EventManager.php 3 weeks ago LogManager.php 3 weeks ago OptionManager.php 3 weeks ago PolicyManager.php 3 weeks ago
EventManager.php
133 lines
1 <?php
2
3 /**
4 * Dispatches domain events to registered listener classes loaded from cache.
5 * Resolves listeners from the cached map, sorts by priority, and invokes handle on each match.
6 * Core of the framework event-driven architecture.
7 *
8 * @package Framework
9 * @subpackage Managers
10 * @since 1.0.0
11 */
12 namespace Kirki\Framework\Managers;
13
14 \defined('ABSPATH') || exit;
15 use Closure;
16 use Kirki\Framework\Listener;
17 use InvalidArgumentException;
18 use function Kirki\Framework\config_path;
19 class EventManager
20 {
21 /**
22 * The listeners array.
23 *
24 * @var array
25 *
26 * @since 1.0.0
27 */
28 protected array $listeners = [];
29 /**
30 * Create a new event manager instance.
31 *
32 * @return void
33 *
34 * @since 1.0.0
35 */
36 public function __construct()
37 {
38 $this->load_listeners();
39 }
40 /**
41 * Load the listeners from the cache.
42 *
43 * @return $this
44 *
45 * @since 1.0.0
46 */
47 protected function load_listeners()
48 {
49 $listeners_cache_path = config_path('listeners.cache.php');
50 if (!\file_exists($listeners_cache_path)) {
51 return $this;
52 }
53 $this->listeners = (require $listeners_cache_path);
54 return $this;
55 }
56 /**
57 * Dispatch the event.
58 *
59 * @param mixed $event The event.
60 *
61 * @return void
62 *
63 * @throws \InvalidArgumentException
64 *
65 * @since 1.0.0
66 */
67 public function dispatch($event)
68 {
69 if (!\is_object($event)) {
70 throw new InvalidArgumentException(\sprintf('The event must be an object, got [%s]', \gettype($event)));
71 }
72 $event_class = \get_class($event);
73 if (!isset($this->listeners[$event_class])) {
74 return;
75 }
76 $event_listeners = $this->listeners[$event_class] ?? [];
77 foreach ($event_listeners as $listener) {
78 $this->resolve($listener, $event);
79 }
80 }
81 /**
82 * Dispatch the event if the boolean is true.
83 *
84 * @param Closure $boolean The boolean.
85 * @param mixed $event The event.
86 *
87 * @return void
88 *
89 * @since 1.0.0
90 */
91 public function dispatch_if(Closure $boolean, $event)
92 {
93 if ($boolean($event)) {
94 $this->dispatch($event);
95 }
96 }
97 /**
98 * Dispatch the event unless the boolean is true.
99 *
100 * @param Closure $boolean The boolean.
101 * @param mixed $event The event.
102 *
103 * @return void
104 *
105 * @since 1.0.0
106 */
107 public function dispatch_unless(Closure $boolean, $event)
108 {
109 if (!$boolean($event)) {
110 $this->dispatch($event);
111 }
112 }
113 /**
114 * Resolve.
115 *
116 * @param mixed $listener The listener.
117 * @param mixed $event The event.
118 *
119 * @return void
120 *
121 * @throws \InvalidArgumentException
122 *
123 * @since 1.0.0
124 */
125 protected function resolve($listener, $event)
126 {
127 if (!\is_subclass_of($listener, Listener::class)) {
128 throw new InvalidArgumentException(\sprintf('The listener [%s] must be a subclass of [%s]', Listener::class));
129 }
130 return (new $listener())->handle($event);
131 }
132 }
133