Skip to content

Commit

Permalink
Replace require with custom autoloader
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal committed Mar 14, 2024
1 parent 9c3d1b0 commit 84d9c09
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions bin/composer-dependency-analyser
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 84d9c09

Please sign in to comment.