Skip to content

Commit

Permalink
adding option to manually register psr0/psr4-like dependency autoloading
Browse files Browse the repository at this point in the history
might be a workaround for some of maglnet#55's issues
  • Loading branch information
Idrinth committed Apr 9, 2018
1 parent 7b46f44 commit bb4871a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
28 changes: 26 additions & 2 deletions src/ComposerRequireChecker/Cli/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,18 @@ protected function configure()
'the composer.json of your package, that should be checked',
'./composer.json'
)
->addOption(
->addOption(
'ignore-parse-errors',
null,
InputOption::VALUE_NONE,
'this will cause ComposerRequireChecker to ignore errors when files cannot be parsed, otherwise'
. ' errors will be thrown'
)
->addOption(
'register-namespace',
null,
InputOption::VALUE_IS_ARRAY|InputOption::VALUE_OPTIONAL,
'vendor/package:namespace:path as if it was psr0 or psr4'
);
}

Expand All @@ -64,13 +70,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$options = $this->getCheckOptions($input);

$getPackageSourceFiles = new LocateComposerPackageSourceFiles();
$manualRegistered = $this->buildManualList($input, $composerJson);

$sourcesASTs = $this->getASTFromFilesLocator($input);

$definedVendorSymbols = (new LocateDefinedSymbolsFromASTRoots())->__invoke($sourcesASTs(
(new ComposeGenerators())->__invoke(
$getPackageSourceFiles($composerJson),
(new LocateComposerPackageDirectDependenciesSourceFiles())->__invoke($composerJson)
(new LocateComposerPackageDirectDependenciesSourceFiles())->__invoke($composerJson, $manualRegistered)
)
));

Expand Down Expand Up @@ -145,4 +152,21 @@ private function getASTFromFilesLocator(InputInterface $input): LocateASTFromFil
return $sourcesASTs;
}

/**
* @return array
*/
private function buildManualList(InputInterface $input, $composerJson)
{
if(!$input->hasOption('register-namespace')) {
return [];
}
$packageDir = dirname($composerJson);
$namespaces = [];
foreach($input->getOption('register-namespace') as $option) {
if(preg_match('!^([^/:]+(/[^/:]+)+):([^:]+):([^:]+)$!i', $option, $matches)) {
$namespaces[$packageDir . '/vendor/' . $matches[1]][$matches[3]] = $matches[4];
}
}
return $namespaces;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

final class LocateComposerPackageDirectDependenciesSourceFiles
{
public function __invoke(string $composerJsonPath): Generator
public function __invoke(string $composerJsonPath, array $manual = []): Generator
{
$packageDir = dirname($composerJsonPath);

Expand All @@ -22,7 +22,10 @@ function (string $vendorName) use ($packageDir) {
continue;
}

yield from (new LocateComposerPackageSourceFiles())->__invoke($vendorDir . '/composer.json');
yield from (new LocateComposerPackageSourceFiles())->__invoke(
$vendorDir . '/composer.json',
$manual[$vendorDir] ?? []
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

final class LocateComposerPackageSourceFiles
{
public function __invoke(string $composerJsonPath): Generator
public function __invoke(string $composerJsonPath, array $manual = []): Generator
{
$packageDir = dirname($composerJsonPath);
$composerData = json_decode(file_get_contents($composerJsonPath), true);
Expand All @@ -29,6 +29,10 @@ public function __invoke(string $composerJsonPath): Generator
$this->getFilePaths($composerData['autoload']['psr-4'] ?? [], $packageDir),
$blacklist
);
yield from $this->locateFilesInFilesInFilesDefinitions(
$this->getFilePaths($manual, $packageDir),
$blacklist
);
}

private function getFilePaths(array $sourceDirs, string $packageDir): array
Expand Down

0 comments on commit bb4871a

Please sign in to comment.