Skip to content

Commit dc5a994

Browse files
authored
Ensure vendor beside composer.json is used (#97)
1 parent e04ea55 commit dc5a994

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/ComposerJson.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use function json_decode;
1919
use function json_last_error;
2020
use function json_last_error_msg;
21+
use function preg_match;
2122
use function realpath;
2223
use function rtrim;
2324
use function strpos;
@@ -193,11 +194,21 @@ private function resolveComposerAutoloadPath(string $basePath, string $vendorDir
193194
{
194195
$vendorDir = rtrim($vendorDir, '/');
195196

196-
if (is_dir($vendorDir)) {
197+
if ($this->isAbsolutePath($vendorDir)) {
197198
return $vendorDir . '/autoload.php';
198199
}
199200

200201
return $basePath . '/' . $vendorDir . '/autoload.php';
201202
}
202203

204+
/**
205+
* Based on Nette\Utils\FileSystem::isAbsolute
206+
*
207+
* @license https://github.com/nette/utils/blob/v4.0.4/license.md
208+
*/
209+
private function isAbsolutePath(string $vendorDir): bool
210+
{
211+
return (bool) preg_match('#([a-z]:)?[/\\\\]|[a-z][a-z0-9+.-]*://#Ai', $vendorDir);
212+
}
213+
203214
}

tests/ComposerJsonTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace ShipMonk\ComposerDependencyAnalyser;
44

55
use PHPUnit\Framework\TestCase;
6+
use function chdir;
67
use function dirname;
78
use function file_put_contents;
89
use function json_encode;
10+
use function mkdir;
911
use function realpath;
1012
use function sys_get_temp_dir;
1113

@@ -60,4 +62,32 @@ public function testAbsoluteCustomVendorDir(): void
6062
);
6163
}
6264

65+
/**
66+
* @runInSeparateProcess Do not spread chdir changes
67+
*/
68+
public function testUseVendorBesideGivenComposerJsonNotCwdOne(): void
69+
{
70+
self::assertDirectoryExists(__DIR__ . '/data'); // precondition
71+
72+
chdir(__DIR__);
73+
74+
$generatedComposerJson = sys_get_temp_dir() . '/vendor-beside/composer.json';
75+
mkdir(dirname($generatedComposerJson), 0777, true);
76+
file_put_contents($generatedComposerJson, json_encode([
77+
'require' => [
78+
'nette/utils' => '^3.0',
79+
],
80+
'config' => [
81+
'vendor-dir' => 'data',
82+
],
83+
]));
84+
85+
$composerJson = new ComposerJson($generatedComposerJson);
86+
87+
self::assertSame(
88+
sys_get_temp_dir() . '/vendor-beside/data/autoload.php',
89+
$composerJson->composerAutoloadPath
90+
);
91+
}
92+
6393
}

0 commit comments

Comments
 (0)