diff --git a/bin/composer-dependency-analyser b/bin/composer-dependency-analyser index 5750e5a..16307bd 100755 --- a/bin/composer-dependency-analyser +++ b/bin/composer-dependency-analyser @@ -93,7 +93,7 @@ try { } // load vendor that belongs to given composer.json -$autoloadFile = dirname($composerJsonPath) . '/' . $composerJson->vendorDir . '/autoload.php'; +$autoloadFile = $composerJson->composerAutoloadPath; if (is_file($autoloadFile)) { require_once $autoloadFile; } else { diff --git a/src/ComposerJson.php b/src/ComposerJson.php index fc6406e..bac8fb8 100644 --- a/src/ComposerJson.php +++ b/src/ComposerJson.php @@ -19,6 +19,7 @@ use function json_last_error; use function json_last_error_msg; use function realpath; +use function rtrim; use function strpos; use const ARRAY_FILTER_USE_KEY; use const JSON_ERROR_NONE; @@ -30,7 +31,7 @@ class ComposerJson * @readonly * @var string */ - public $vendorDir; + public $composerAutoloadPath; /** * Package => isDev @@ -57,7 +58,7 @@ public function __construct(string $composerJsonPath) $basePath = dirname($composerJsonPath); $composerJsonData = $this->parseComposerJson($composerJsonPath); - $this->vendorDir = $composerJsonData['vendor-dir'] ?? 'vendor'; + $this->composerAutoloadPath = $this->resolveComposerAutoloadPath($basePath, $composerJsonData['config']['vendor-dir'] ?? 'vendor'); $requiredPackages = $composerJsonData['require'] ?? []; $requiredDevPackages = $composerJsonData['require-dev'] ?? []; @@ -147,7 +148,9 @@ private function realpath(string $path): string * @return array{ * require?: array, * require-dev?: array, - * vendor-dir?: string, + * config?: array{ + * vendor-dir?: string, + * }, * autoload?: array{ * psr-0?: array, * psr-4?: array, @@ -186,4 +189,15 @@ private function parseComposerJson(string $composerJsonPath): array return $composerJsonData; // @phpstan-ignore-line ignore mixed returned } + private function resolveComposerAutoloadPath(string $basePath, string $vendorDir): string + { + $vendorDir = rtrim($vendorDir, '/'); + + if (is_dir($vendorDir)) { + return $vendorDir . '/autoload.php'; + } + + return $basePath . '/' . $vendorDir . '/autoload.php'; + } + } diff --git a/tests/ComposerJsonTest.php b/tests/ComposerJsonTest.php index abff308..b0c5f0d 100644 --- a/tests/ComposerJsonTest.php +++ b/tests/ComposerJsonTest.php @@ -3,14 +3,24 @@ namespace ShipMonk\ComposerDependencyAnalyser; use PHPUnit\Framework\TestCase; +use function dirname; +use function file_put_contents; +use function json_encode; use function realpath; +use function sys_get_temp_dir; class ComposerJsonTest extends TestCase { public function testComposerJson(): void { - $composerJson = new ComposerJson(__DIR__ . '/data/not-autoloaded/composer/sample.json'); + $composerJsonPath = __DIR__ . '/data/not-autoloaded/composer/sample.json'; + $composerJson = new ComposerJson($composerJsonPath); + + self::assertSame( + dirname($composerJsonPath) . '/custom-vendor/autoload.php', + $composerJson->composerAutoloadPath + ); self::assertSame( [ @@ -30,4 +40,24 @@ public function testComposerJson(): void ); } + public function testAbsoluteCustomVendorDir(): void + { + $generatedComposerJson = sys_get_temp_dir() . '/custom-vendor.json'; + file_put_contents($generatedComposerJson, json_encode([ + 'require' => [ + 'nette/utils' => '^3.0', + ], + 'config' => [ + 'vendor-dir' => sys_get_temp_dir(), + ], + ])); + + $composerJson = new ComposerJson($generatedComposerJson); + + self::assertSame( + sys_get_temp_dir() . '/autoload.php', + $composerJson->composerAutoloadPath + ); + } + } diff --git a/tests/data/not-autoloaded/composer/sample.json b/tests/data/not-autoloaded/composer/sample.json index 6e6ffb1..7cf5ac1 100644 --- a/tests/data/not-autoloaded/composer/sample.json +++ b/tests/data/not-autoloaded/composer/sample.json @@ -13,5 +13,8 @@ "files": [ "dir2/file1.php" ] + }, + "config": { + "vendor-dir": "custom-vendor/" } }