From 9c3d1b0bc67706649a9a435612ca473ff1b0a303 Mon Sep 17 00:00:00 2001 From: Jan Nedbal Date: Thu, 14 Mar 2024 10:40:04 +0100 Subject: [PATCH] Use only vendor of given composer.json --- bin/composer-dependency-analyser | 42 +++++++++++++++++--------------- src/ComposerJson.php | 8 ++++++ 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/bin/composer-dependency-analyser b/bin/composer-dependency-analyser index 42c9793..309af29 100755 --- a/bin/composer-dependency-analyser +++ b/bin/composer-dependency-analyser @@ -39,15 +39,11 @@ Ignore options: EOD; -$autoloadFiles = [ - __DIR__ . '/../vendor/autoload.php', - __DIR__ . '/../../../autoload.php', -]; - -foreach ($autoloadFiles as $autoloadFile) { - if (file_exists($autoloadFile)) { - require_once $autoloadFile; - break; +// 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(); } } @@ -82,6 +78,24 @@ if (isset($providedOptions['help'])) { exit; } +$composerJsonPath = isset($providedOptions['composer-json']) + ? ($cwd . "/" . $providedOptions['composer-json']) + : ($cwd . "/composer.json"); + +try { + $composerJson = new ComposerJson($composerJsonPath); +} catch (OurRuntimeException $e) { + $exit($e->getMessage()); +} + +// load vendor that belongs to given composer.json +$autoloadFile = dirname($composerJsonPath) . '/' . $composerJson->vendorDir . '/autoload.php'; +if (is_file($autoloadFile)) { + require_once $autoloadFile; +} else { + $exit("Cannot find composer's autoload file, expected at '$autoloadFile'"); +} + if (isset($providedOptions['config'])) { $configPath = $cwd . "/" . $providedOptions['config']; @@ -130,16 +144,6 @@ if ($ignoreProdOnlyInDev) { $config->ignoreErrors([ErrorType::PROD_DEPENDENCY_ONLY_IN_DEV]); } -$composerJsonPath = isset($providedOptions['composer-json']) - ? ($cwd . "/" . $providedOptions['composer-json']) - : ($cwd . "/composer.json"); - -try { - $composerJson = new ComposerJson($composerJsonPath); -} catch (OurRuntimeException $e) { - $exit($e->getMessage()); -} - $loaders = ClassLoader::getRegisteredLoaders(); if (count($loaders) > 1) { $printer->printLine("\nDetected multiple class loaders:"); diff --git a/src/ComposerJson.php b/src/ComposerJson.php index d808f73..fc6406e 100644 --- a/src/ComposerJson.php +++ b/src/ComposerJson.php @@ -26,6 +26,12 @@ class ComposerJson { + /** + * @readonly + * @var string + */ + public $vendorDir; + /** * Package => isDev * @@ -51,6 +57,7 @@ public function __construct(string $composerJsonPath) $basePath = dirname($composerJsonPath); $composerJsonData = $this->parseComposerJson($composerJsonPath); + $this->vendorDir = $composerJsonData['vendor-dir'] ?? 'vendor'; $requiredPackages = $composerJsonData['require'] ?? []; $requiredDevPackages = $composerJsonData['require-dev'] ?? []; @@ -140,6 +147,7 @@ private function realpath(string $path): string * @return array{ * require?: array, * require-dev?: array, + * vendor-dir?: string, * autoload?: array{ * psr-0?: array, * psr-4?: array,