Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [Autoloader] Composer classmap usage #5850

Merged
merged 8 commits into from
Apr 3, 2022
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
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()
paulbalandan marked this conversation as resolved.
Show resolved Hide resolved
{
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