Skip to content

Commit 1bb95a4

Browse files
committed
wip
1 parent 19ed472 commit 1bb95a4

3 files changed

Lines changed: 33 additions & 5 deletions

File tree

system/Autoloader/Autoloader.php

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

96+
/**
97+
* Whether this autoloader has been registered with SPL.
98+
*/
99+
private bool $registered = false;
100+
96101
public function __construct(private readonly string $composerPath = COMPOSER_PATH)
97102
{
98103
}
@@ -170,8 +175,13 @@ private function loadComposerAutoloader(Modules $modules): void
170175
*/
171176
public function register()
172177
{
173-
spl_autoload_register($this->loadClassmap(...), true);
174-
spl_autoload_register($this->loadClass(...), true);
178+
if ($this->registered) {
179+
return;
180+
}
181+
182+
spl_autoload_register([$this, 'loadClassmap'], true);
183+
spl_autoload_register([$this, 'loadClass'], true);
184+
$this->registered = true;
175185

176186
foreach ($this->files as $file) {
177187
$this->includeFile($file);
@@ -183,8 +193,13 @@ public function register()
183193
*/
184194
public function unregister(): void
185195
{
186-
spl_autoload_unregister($this->loadClass(...));
187-
spl_autoload_unregister($this->loadClassmap(...));
196+
if (! $this->registered) {
197+
return;
198+
}
199+
200+
spl_autoload_unregister([$this, 'loadClass']);
201+
spl_autoload_unregister([$this, 'loadClassmap']);
202+
$this->registered = false;
188203
}
189204

190205
/**

system/Test/CIUnitTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ public function assertCloseEnoughString($expected, $actual, string $message = ''
514514
protected function createApplication()
515515
{
516516
// Initialize the autoloader.
517-
service('autoloader')->initialize(new Autoload(), new Modules());
517+
service('autoloader')->initialize(new Autoload(), new Modules())->register();
518518

519519
$app = new MockCodeIgniter(new App());
520520
$app->initialize();

tests/system/Config/ServicesTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,19 @@ public function testResetReRegistersAutoloader(): void
439439
$this->assertInstanceOf(Test::class, $test);
440440
}
441441

442+
#[PreserveGlobalState(false)]
443+
#[RunInSeparateProcess]
444+
public function testResetDoesNotDuplicateAutoloaderCallbacks(): void
445+
{
446+
$initial = count(spl_autoload_functions() ?: []);
447+
448+
Services::reset(true);
449+
$this->assertSame($initial, count(spl_autoload_functions() ?: []));
450+
451+
Services::reset(true);
452+
$this->assertSame($initial, count(spl_autoload_functions() ?: []));
453+
}
454+
442455
public function testFilters(): void
443456
{
444457
$result = Services::filters();

0 commit comments

Comments
 (0)