11<?php namespace CodeIgniter \Events ;
22
3+ use Config \Services ;
4+
35/**
46 * CodeIgniter
57 *
@@ -53,19 +55,12 @@ class Events
5355 protected static $ listeners = [];
5456
5557 /**
56- * Flag to let us know if we've read from the Config file
58+ * Flag to let us know if we've read from the Config file(s)
5759 * and have all of the defined events.
5860 *
5961 * @var bool
6062 */
61- protected static $ haveReadFromFile = false ;
62-
63- /**
64- * The path to the file containing the events to load in.
65- *
66- * @var string
67- */
68- protected static $ eventsFile = '' ;
63+ protected static $ initialized = false ;
6964
7065 /**
7166 * If true, events will not actually be fired.
@@ -82,28 +77,48 @@ class Events
8277 */
8378 protected static $ performanceLog = [];
8479
80+ /**
81+ * A list of found files.
82+ * @var array
83+ */
84+ protected static $ files = [];
85+
8586 //--------------------------------------------------------------------
8687
8788 /**
8889 * Ensures that we have a events file ready.
89- *
90- * @param string|null $file
9190 */
92- public static function initialize (string $ file = null )
91+ public static function initialize ()
9392 {
9493 // Don't overwrite anything....
95- if ( ! empty ( self ::$ eventsFile ) )
94+ if (static ::$ initialized )
9695 {
9796 return ;
9897 }
9998
100- // Default value
101- if (empty ($ file ))
99+ $ config = config ('Modules ' );
100+
101+ $ files = [APPPATH .'Config/Events.php ' ];
102+
103+ if ($ config ->shouldDiscover ('events ' ))
104+ {
105+ $ locator = Services::locator ();
106+ $ files = $ locator ->search ('Config/Events.php ' );
107+ }
108+
109+ static ::$ files = $ files ;
110+
111+ foreach (static ::$ files as $ file )
102112 {
103- $ file = APPPATH . 'Config/Events.php ' ;
113+ if (! file_exists ($ file ))
114+ {
115+ continue ;
116+ }
117+
118+ include $ file ;
104119 }
105120
106- self ::$ eventsFile = $ file ;
121+ static ::$ initialized = true ;
107122 }
108123
109124 //--------------------------------------------------------------------
@@ -155,15 +170,9 @@ public static function on($event_name, callable $callback, $priority = EVENT_PRI
155170 public static function trigger ($ eventName , ...$ arguments ): bool
156171 {
157172 // Read in our Config/events file so that we have them all!
158- if ( ! self ::$ haveReadFromFile )
173+ if ( ! self ::$ initialized )
159174 {
160175 self ::initialize ();
161-
162- if (is_file (self ::$ eventsFile ))
163- {
164- include self ::$ eventsFile ;
165- }
166- self ::$ haveReadFromFile = true ;
167176 }
168177
169178 $ listeners = self ::listeners ($ eventName );
@@ -288,11 +297,23 @@ public static function removeAllListeners($event_name = null)
288297 /**
289298 * Sets the path to the file that routes are read from.
290299 *
291- * @param string $path
300+ * @param array $files
301+ */
302+ public static function setFiles (array $ files )
303+ {
304+ static ::$ files = $ files ;
305+ }
306+
307+ //--------------------------------------------------------------------
308+
309+ /**
310+ * Returns the files that were found/loaded during this request.
311+ *
312+ * @return mixed
292313 */
293- public static function setFile ( string $ path )
314+ public function getFiles ( )
294315 {
295- self ::$ eventsFile = $ path ;
316+ return static ::$ files ;
296317 }
297318
298319 //--------------------------------------------------------------------
0 commit comments