Skip to content

Commit f9a27d6

Browse files
committed
add suggested fix from copilot
1 parent 28d4f2e commit f9a27d6

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

system/Autoloader/Autoloader.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ class Autoloader
9393
*/
9494
protected $helpers = ['url'];
9595

96+
/**
97+
* Track if Composer namespaces have been loaded in this initialization.
98+
* Resets on each initialize() call to allow tests to load Composer.
99+
*/
100+
private bool $composerLoaded = false;
101+
96102
public function __construct(private readonly string $composerPath = COMPOSER_PATH)
97103
{
98104
}
@@ -109,6 +115,8 @@ public function initialize(Autoload $config, Modules $modules)
109115
$this->classmap = [];
110116
$this->files = [];
111117

118+
$this->composerLoaded = false;
119+
112120
// We have to have one or the other, though we don't enforce the need
113121
// to have both present in order to work.
114122
if ($config->psr4 === [] && $config->classmap === []) {
@@ -146,13 +154,19 @@ private function loadComposerAutoloader(Modules $modules): void
146154
define('VENDORPATH', dirname($this->composerPath) . DIRECTORY_SEPARATOR);
147155
}
148156

157+
// Skip if already loaded in this initialization
158+
if ($this->composerLoaded) {
159+
return;
160+
}
161+
149162
/** @var ClassLoader $composer */
150163
$composer = include $this->composerPath;
151164

152165
// Should we load through Composer's namespaces, also?
153166
if ($modules->discoverInComposer) {
154167
$composerPackages = $modules->composerPackages;
155168
$this->loadComposerNamespaces($composer, $composerPackages ?? []);
169+
$this->composerLoaded = true;
156170
}
157171

158172
unset($composer);
@@ -170,8 +184,8 @@ private function loadComposerAutoloader(Modules $modules): void
170184
*/
171185
public function register()
172186
{
173-
spl_autoload_register($this->loadClass(...), prepend: true);
174187
spl_autoload_register($this->loadClassmap(...), prepend: true);
188+
spl_autoload_register($this->loadClass(...), prepend: true);
175189

176190
foreach ($this->files as $file) {
177191
$this->includeFile($file);

0 commit comments

Comments
 (0)