Skip to content

Commit

Permalink
fix: add check for duplicate registrar auto-discovery runs
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Jul 26, 2024
1 parent 3e88f85 commit d970f76
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion system/Config/BaseConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace CodeIgniter\Config;

use CodeIgniter\Exceptions\ConfigException;
use Config\Encryption;
use Config\Modules;
use ReflectionClass;
Expand Down Expand Up @@ -45,12 +46,22 @@ class BaseConfig
public static bool $override = true;

/**
* Has module discovery happened yet?
* Has module discovery completed?
*
* @var bool
*/
protected static $didDiscovery = false;

/**
* Is module discovery running or not?
*/
protected static bool $discovering = false;

/**
* The processing Registrar file for error message.
*/
protected static string $registrarFile = '';

/**
* The modules configuration.
*
Expand Down Expand Up @@ -230,10 +241,24 @@ protected function registerProperties()
}

if (! static::$didDiscovery) {
// Discovery must be completed before the first instantiation of any Config class.
if (static::$discovering) {
throw new ConfigException(
'During Auto-Discovery of Registrars,'
. ' "' . static::class . '" executes Auto-Discovery again.'
. ' "' . clean_path(static::$registrarFile) . '" seems to have bad code.'
);
}

static::$discovering = true;

$locator = service('locator');
$registrarsFiles = $locator->search('Config/Registrar.php');

foreach ($registrarsFiles as $file) {
// Saves the file for error message.
static::$registrarFile = $file;

$className = $locator->findQualifiedNameFromPath($file);

if ($className === false) {
Expand All @@ -244,6 +269,7 @@ protected function registerProperties()
}

static::$didDiscovery = true;
static::$discovering = false;
}

$shortName = (new ReflectionClass($this))->getShortName();
Expand Down

0 comments on commit d970f76

Please sign in to comment.