1111
1212namespace CodeIgniter \Config ;
1313
14+ use CodeIgniter \Exceptions \ConfigException ;
1415use CodeIgniter \Exceptions \RuntimeException ;
1516use Config \Encryption ;
1617use Config \Modules ;
@@ -45,12 +46,22 @@ class BaseConfig
4546 public static bool $ override = true ;
4647
4748 /**
48- * Has module discovery happened yet ?
49+ * Has module discovery completed ?
4950 *
5051 * @var bool
5152 */
5253 protected static $ didDiscovery = false ;
5354
55+ /**
56+ * Is module discovery running or not?
57+ */
58+ protected static bool $ discovering = false ;
59+
60+ /**
61+ * The processing Registrar file for error message.
62+ */
63+ protected static string $ registrarFile = '' ;
64+
5465 /**
5566 * The modules configuration.
5667 *
@@ -230,10 +241,24 @@ protected function registerProperties()
230241 }
231242
232243 if (! static ::$ didDiscovery ) {
244+ // Discovery must be completed before the first instantiation of any Config class.
245+ if (static ::$ discovering ) {
246+ throw new ConfigException (
247+ 'During Auto-Discovery of Registrars, '
248+ . ' " ' . static ::class . '" executes Auto-Discovery again. '
249+ . ' " ' . clean_path (static ::$ registrarFile ) . '" seems to have bad code. '
250+ );
251+ }
252+
253+ static ::$ discovering = true ;
254+
233255 $ locator = service ('locator ' );
234256 $ registrarsFiles = $ locator ->search ('Config/Registrar.php ' );
235257
236258 foreach ($ registrarsFiles as $ file ) {
259+ // Saves the file for error message.
260+ static ::$ registrarFile = $ file ;
261+
237262 $ className = $ locator ->findQualifiedNameFromPath ($ file );
238263
239264 if ($ className === false ) {
@@ -244,6 +269,7 @@ protected function registerProperties()
244269 }
245270
246271 static ::$ didDiscovery = true ;
272+ static ::$ discovering = false ;
247273 }
248274
249275 $ shortName = (new ReflectionClass ($ this ))->getShortName ();
0 commit comments