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

perf: autoloader #8005

Merged
merged 16 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
1 change: 0 additions & 1 deletion app/Config/Autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class Autoload extends AutoloadConfig
*/
public $psr4 = [
APP_NAMESPACE => APPPATH, // For custom app namespace
'Config' => APPPATH . 'Config',
];

/**
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@
},
"autoload": {
"psr-4": {
"CodeIgniter\\": "system/"
"App\\": "app/",
"CodeIgniter\\": "system/",
"Config\\": "app/Config/"
},
"exclude-from-classmap": [
"**/Database/Migrations/**"
Expand Down
27 changes: 11 additions & 16 deletions system/Autoloader/Autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,6 @@ private function loadComposerInfo(Modules $modules): void
*/
$composer = include COMPOSER_PATH;

$this->loadComposerClassmap($composer);

// Should we load through Composer's namespaces, also?
if ($modules->discoverInComposer) {
// @phpstan-ignore-next-line
Expand All @@ -155,11 +153,11 @@ private function loadComposerInfo(Modules $modules): void
*/
public function register()
{
// Prepend the PSR4 autoloader for maximum performance.
spl_autoload_register([$this, 'loadClass'], true, true);
// Register classmap loader for the files in our class map.
spl_autoload_register([$this, 'loadClassmap'], true);

// Now prepend another loader for the files in our class map.
spl_autoload_register([$this, 'loadClassmap'], true, true);
// Register the PSR-4 autoloader.
spl_autoload_register([$this, 'loadClass'], true);

// Load our non-class files
foreach ($this->files as $file) {
Expand Down Expand Up @@ -362,9 +360,13 @@ private function loadComposerNamespaces(ClassLoader $composer, array $composerPa
{
$namespacePaths = $composer->getPrefixesPsr4();

// Get rid of CodeIgniter so we don't have duplicates
if (isset($namespacePaths['CodeIgniter\\'])) {
unset($namespacePaths['CodeIgniter\\']);
// Get rid of duplicated namespaces.
$duplicatedNamespaces = ['CodeIgniter', APP_NAMESPACE, 'Config'];

foreach ($duplicatedNamespaces as $ns) {
if (isset($namespacePaths[$ns . '\\'])) {
unset($namespacePaths[$ns . '\\']);
}
}

if (! method_exists(InstalledVersions::class, 'getAllRawData')) {
Expand Down Expand Up @@ -428,13 +430,6 @@ private function loadComposerNamespaces(ClassLoader $composer, array $composerPa
$this->addNamespace($newPaths);
}

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

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

/**
* Locates autoload information from Composer, if available.
*
Expand Down
2 changes: 1 addition & 1 deletion system/Config/AutoloadConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class AutoloadConfig
*/
protected $corePsr4 = [
'CodeIgniter' => SYSTEMPATH,
'App' => APPPATH, // To ensure filters, etc still found,
'Config' => APPPATH . 'Config',
];

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/system/Commands/Utilities/NamespacesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public function testNamespacesCommandCodeIgniterOnly(): void
| Namespace | Path | Found? |
+---------------+-------------------------+--------+
| CodeIgniter | ROOTPATH/system | Yes |
| App | ROOTPATH/app | Yes |
| Config | APPPATH/Config | Yes |
| App | ROOTPATH/app | Yes |
| Tests\Support | ROOTPATH/tests/_support | Yes |
+---------------+-------------------------+--------+
EOL;
Expand Down
7 changes: 0 additions & 7 deletions tests/system/Config/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,6 @@ public function testCreateSharedInstance(): void
$this->assertSame($Config2, $Config);
}

public function testCreateNonConfig(): void
{
$Config = Config::get('Constants', false);

$this->assertNull($Config);
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
Expand Down
Loading