Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions system/Autoloader/Autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CodeIgniter\Autoloader;

use Closure;
use CodeIgniter\Exceptions\ConfigException;
use CodeIgniter\Exceptions\InvalidArgumentException;
use CodeIgniter\Exceptions\RuntimeException;
Expand Down Expand Up @@ -93,6 +94,14 @@ class Autoloader
*/
protected $helpers = ['url'];

/**
* Stores the closures registered with spl_autoload_register()
* so that unregister() can remove the exact same instances.
*
* @var list<Closure(string): void>
*/
private array $registeredClosures = [];

public function __construct(private readonly string $composerPath = COMPOSER_PATH)
{
}
Expand Down Expand Up @@ -170,8 +179,17 @@ private function loadComposerAutoloader(Modules $modules): void
*/
public function register()
{
spl_autoload_register($this->loadClassmap(...), true);
spl_autoload_register($this->loadClass(...), true);
// Store the exact Closure instances so unregister() can remove them.
// First-class callable syntax (e.g. $this->loadClass(...)) creates a
// new Closure object on every call, so we must reuse the same instances.
$loadClassmap = $this->loadClassmap(...);
$loadClass = $this->loadClass(...);

$this->registeredClosures[] = $loadClassmap;
$this->registeredClosures[] = $loadClass;

spl_autoload_register($loadClassmap, true);
spl_autoload_register($loadClass, true);

foreach ($this->files as $file) {
$this->includeFile($file);
Expand All @@ -183,8 +201,11 @@ public function register()
*/
public function unregister(): void
{
spl_autoload_unregister($this->loadClass(...));
spl_autoload_unregister($this->loadClassmap(...));
foreach ($this->registeredClosures as $closure) {
spl_autoload_unregister($closure);
}

$this->registeredClosures = [];
}

/**
Expand Down
39 changes: 39 additions & 0 deletions tests/system/Autoloader/AutoloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,45 @@ public function testServiceAutoLoaderFromShareInstances(): void
$this->assertSame($expected, $actual);
}

public function testUnregisterRemovesClosuresFromSplStack(): void
{
$countBefore = count(spl_autoload_functions());

$config = new Autoload();
$modules = new Modules();
$modules->discoverInComposer = false;
$config->psr4 = ['CodeIgniter' => SYSTEMPATH];

$loader = new Autoloader();
$loader->initialize($config, $modules)->register();

$this->assertCount($countBefore + 2, spl_autoload_functions());

$loader->unregister();

$this->assertCount($countBefore, spl_autoload_functions());
}

public function testUnregisterRemovesAllClosuresAfterMultipleRegistrations(): void
{
$countBefore = count(spl_autoload_functions());

$config = new Autoload();
$modules = new Modules();
$modules->discoverInComposer = false;
$config->psr4 = ['CodeIgniter' => SYSTEMPATH];

$loader = new Autoloader();
$loader->initialize($config, $modules)->register();
$loader->register();

$this->assertCount($countBefore + 4, spl_autoload_functions());

$loader->unregister();

$this->assertCount($countBefore, spl_autoload_functions());
}

public function testServiceAutoLoader(): void
{
$autoloader = service('autoloader', false);
Expand Down
2 changes: 2 additions & 0 deletions user_guide_src/source/changelogs/v4.7.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Deprecations
Bugs Fixed
**********

- **Autoloader:** Fixed a bug where ``Autoloader::unregister()`` (used during tests) silently failed to remove handlers from the SPL autoload stack, causing closures to accumulate permanently.

See the repo's
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
for a complete list of bugs fixed.
Loading