Skip to content

Commit

Permalink
Use only vendor of given composer.json
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal committed Mar 14, 2024
1 parent b559b8a commit 9c3d1b0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
42 changes: 23 additions & 19 deletions bin/composer-dependency-analyser
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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'];

Expand Down Expand Up @@ -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:");
Expand Down
8 changes: 8 additions & 0 deletions src/ComposerJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
class ComposerJson
{

/**
* @readonly
* @var string
*/
public $vendorDir;

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

0 comments on commit 9c3d1b0

Please sign in to comment.