Skip to content

Commit

Permalink
Fixes classloader performance
Browse files Browse the repository at this point in the history
  • Loading branch information
daftspunk committed May 9, 2024
1 parent ba495f9 commit ccb93de
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/Support/ClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class ClassLoader
*/
public $manifest = [];

/**
* @var array unknownClasses cache
*/
protected $unknownClasses = [];

/**
* @var bool manifestDirty if manifest needs to be written
*/
Expand Down Expand Up @@ -68,6 +73,10 @@ public function __construct(Filesystem $files, string $basePath)
*/
public function load($class): bool
{
if (!str_contains($class, '\\')) {
return false;
}

if (
isset($this->manifest[$class]) &&
is_file($fullPath = $this->basePath.DIRECTORY_SEPARATOR.$this->manifest[$class])
Expand All @@ -76,6 +85,10 @@ public function load($class): bool
return true;
}

if (isset($this->unknownClasses[$class])) {
return false;
}

[$lowerClass, $upperClass] = $this->normalizeClass($class);

// Load namespaces
Expand All @@ -94,6 +107,8 @@ public function load($class): bool
}
}

$this->unknownClasses[$class] = true;

return false;
}

Expand Down Expand Up @@ -150,7 +165,7 @@ public function register(): void

$this->registered = spl_autoload_register(function($class) {
$this->load($class);
}, true, true);
});
}

/**
Expand Down

0 comments on commit ccb93de

Please sign in to comment.