Skip to content

Commit

Permalink
Merge pull request #5850 from kenjis/refactor-autoloader
Browse files Browse the repository at this point in the history
fix: [Autoloader] Composer classmap usage
  • Loading branch information
kenjis authored Apr 3, 2022
2 parents e075dbf + c7e659f commit 3b8399b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
48 changes: 46 additions & 2 deletions system/Autoloader/Autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,28 @@ public function initialize(Autoload $config, Modules $modules)
$this->files = $config->files;
}

if (is_file(COMPOSER_PATH)) {
$this->loadComposerInfo($modules);
}

return $this;
}

private function loadComposerInfo(Modules $modules): void
{
/**
* @var ClassLoader $composer
*/
$composer = include COMPOSER_PATH;

$this->loadComposerClassmap($composer);

// Should we load through Composer's namespaces, also?
if ($modules->discoverInComposer) {
$this->discoverComposerNamespaces();
$this->loadComposerNamespaces($composer);
}

return $this;
unset($composer);
}

/**
Expand Down Expand Up @@ -296,8 +312,36 @@ public function sanitizeFilename(string $filename): string
return trim($filename, '.-_');
}

private function loadComposerNamespaces(ClassLoader $composer): void
{
$paths = $composer->getPrefixesPsr4();

// Get rid of CodeIgniter so we don't have duplicates
if (isset($paths['CodeIgniter\\'])) {
unset($paths['CodeIgniter\\']);
}

$newPaths = [];

foreach ($paths as $key => $value) {
// Composer stores namespaces with trailing slash. We don't.
$newPaths[rtrim($key, '\\ ')] = $value;
}

$this->prefixes = array_merge($this->prefixes, $newPaths);
}

private function loadComposerClassmap(ClassLoader $composer): void
{
$classes = $composer->getClassMap();

$this->classmap = array_merge($this->classmap, $classes);
}

/**
* Locates autoload information from Composer, if available.
*
* @deprecated No longer used.
*/
protected function discoverComposerNamespaces()
{
Expand Down
4 changes: 2 additions & 2 deletions tests/system/Autoloader/AutoloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function testServiceAutoLoaderFromShareInstances()
// look for Home controller, as that should be in base repo
$actual = $autoloader->loadClass('App\Controllers\Home');
$expected = APPPATH . 'Controllers' . DIRECTORY_SEPARATOR . 'Home.php';
$this->assertSame($expected, $actual);
$this->assertSame($expected, realpath($actual) ?: $actual);
}

public function testServiceAutoLoader()
Expand All @@ -99,7 +99,7 @@ public function testServiceAutoLoader()
// look for Home controller, as that should be in base repo
$actual = $autoloader->loadClass('App\Controllers\Home');
$expected = APPPATH . 'Controllers' . DIRECTORY_SEPARATOR . 'Home.php';
$this->assertSame($expected, $actual);
$this->assertSame($expected, realpath($actual) ?: $actual);
}

public function testExistingFile()
Expand Down
2 changes: 2 additions & 0 deletions user_guide_src/source/changelogs/v4.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ BREAKING
- ``public/index.php``
- ``spark``
- The method signature of ``CodeIgniter\CLI\CommandRunner::_remap()`` has been changed to fix a bug.
- The ``CodeIgniter\Autoloader\Autoloader::initialize()`` has changed the behavior to fix a bug. It used to use Composer classmap only when ``$modules->discoverInComposer`` is true. Now it always uses the Composer classmap if Composer is available.

Enhancements
************
Expand Down Expand Up @@ -55,6 +56,7 @@ Deprecations
- ``CodeIgniter\Log\Logger::cleanFilenames()`` and ``CodeIgniter\Test\TestLogger::cleanup()`` are both deprecated. Use the ``clean_path()`` function instead.
- ``CodeIgniter\Router\Router::setDefaultController()`` is deprecated.
- The constant ``SPARKED`` in **spark** is deprecated. Use the ``$context`` property in ``CodeIgniter\CodeIgniter`` instead.
- ``CodeIgniter\Autoloader\Autoloader::discoverComposerNamespaces()`` is deprecated, and no longer used.

Bugs Fixed
**********
Expand Down

0 comments on commit 3b8399b

Please sign in to comment.