Skip to content

Commit

Permalink
Support even absolute path in config.vendor-dir
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal committed Mar 14, 2024
1 parent 84d9c09 commit b0fb5a4
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
2 changes: 1 addition & 1 deletion bin/composer-dependency-analyser
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 17 additions & 3 deletions src/ComposerJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,7 +31,7 @@ class ComposerJson
* @readonly
* @var string
*/
public $vendorDir;
public $composerAutoloadPath;

/**
* Package => isDev
Expand All @@ -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'] ?? [];
Expand Down Expand Up @@ -147,7 +148,9 @@ private function realpath(string $path): string
* @return array{
* require?: array<string, string>,
* require-dev?: array<string, string>,
* vendor-dir?: string,
* config?: array{
* vendor-dir?: string,
* },
* autoload?: array{
* psr-0?: array<string, string|string[]>,
* psr-4?: array<string, string|string[]>,
Expand Down Expand Up @@ -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';
}

}
32 changes: 31 additions & 1 deletion tests/ComposerJsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
[
Expand All @@ -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
);
}

}
3 changes: 3 additions & 0 deletions tests/data/not-autoloaded/composer/sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
"files": [
"dir2/file1.php"
]
},
"config": {
"vendor-dir": "custom-vendor/"
}
}

0 comments on commit b0fb5a4

Please sign in to comment.