From 84d9c092697e86cb5743d2de784cab6cb3654e68 Mon Sep 17 00:00:00 2001 From: Jan Nedbal Date: Thu, 14 Mar 2024 11:12:02 +0100 Subject: [PATCH] Replace require with custom autoloader --- bin/composer-dependency-analyser | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/bin/composer-dependency-analyser b/bin/composer-dependency-analyser index 309af29..5750e5a 100755 --- a/bin/composer-dependency-analyser +++ b/bin/composer-dependency-analyser @@ -39,13 +39,17 @@ Ignore options: EOD; -// preload own classes (do not rely on presence in composer's autoloader) -foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__ . '/../src')) as $entry) { - /** @var DirectoryIterator $entry */ - if ($entry->isFile() && $entry->getExtension() === 'php') { - require_once $entry->getPathname(); +$psr4Prefix = 'ShipMonk\\ComposerDependencyAnalyser\\'; + +// autoloader for own classes (do not rely on presence in composer's autoloader) +spl_autoload_register(static function (string $class) use ($psr4Prefix): void { + if (strpos($class, $psr4Prefix) === 0) { + /** @var string $classWithoutPrefix */ + $classWithoutPrefix = substr($class, strlen($psr4Prefix)); + $file = __DIR__ . '/../src/' . str_replace('\\', '/', $classWithoutPrefix) . '.php'; + require $file; } -} +}); $cwd = getcwd(); $printer = new Printer($cwd === false ? '' : $cwd);