-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1174 from patrickkusebauch/feature/composer
Composer Collector
- Loading branch information
Showing
12 changed files
with
7,506 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace Qossmic\Deptrac\Contract\Config\Collector; | ||
|
||
use Qossmic\Deptrac\Contract\Config\CollectorConfig; | ||
use Qossmic\Deptrac\Contract\Config\CollectorType; | ||
|
||
final class ComposerConfig extends CollectorConfig | ||
{ | ||
protected CollectorType $collectorType = CollectorType::TYPE_COMPOSER; | ||
|
||
/** @var list<string> */ | ||
private array $packages = []; | ||
|
||
private function __construct( | ||
private readonly string $composerPath, | ||
private readonly string $composerLockPath, | ||
) { | ||
} | ||
|
||
/** | ||
* @param list<string> $packages | ||
*/ | ||
public static function create(string $composerPath = 'composer.json', string $composerLockPath = 'composer.lock', array $packages = []): self | ||
{ | ||
$result = new self($composerPath, $composerLockPath); | ||
foreach ($packages as $package) { | ||
$result->addPackage($package); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
public function addPackage(string $package): self | ||
{ | ||
$this->packages[] = $package; | ||
|
||
return $this; | ||
} | ||
|
||
/** @return array{ | ||
* composerPath: string, | ||
* composerLockPath: string, | ||
* packages: list<string>, | ||
* private: bool, | ||
* type: string} | ||
*/ | ||
public function toArray(): array | ||
{ | ||
return [ | ||
'composerPath' => $this->composerPath, | ||
'composerLockPath' => $this->composerLockPath, | ||
'packages' => $this->packages, | ||
'private' => $this->private, | ||
'type' => $this->collectorType->value, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Qossmic\Deptrac\Core\Layer\Collector; | ||
|
||
use Qossmic\Deptrac\Contract\Ast\CouldNotParseFileException; | ||
use Qossmic\Deptrac\Contract\Ast\TokenReferenceInterface; | ||
use Qossmic\Deptrac\Contract\Layer\CollectorInterface; | ||
use Qossmic\Deptrac\Contract\Layer\InvalidCollectorDefinitionException; | ||
use RuntimeException; | ||
|
||
final class ComposerCollector implements CollectorInterface | ||
{ | ||
public function __construct() | ||
{ | ||
} | ||
|
||
public function satisfy(array $config, TokenReferenceInterface $reference): bool | ||
{ | ||
if (!isset($config['composerPath']) || !is_string($config['composerPath'])) { | ||
throw InvalidCollectorDefinitionException::invalidCollectorConfiguration('ComposerCollector needs the path to the composer.json file as string.'); | ||
} | ||
|
||
if (!isset($config['composerLockPath']) || !is_string($config['composerLockPath'])) { | ||
throw InvalidCollectorDefinitionException::invalidCollectorConfiguration('ComposerCollector needs the path to the composer.lock file as string.'); | ||
} | ||
|
||
if (!isset($config['packages']) || !is_array($config['packages'])) { | ||
throw InvalidCollectorDefinitionException::invalidCollectorConfiguration('ComposerCollector needs the list of packages as string.'); | ||
} | ||
|
||
try { | ||
$composerFilesParser = new ComposerFilesParser($config['composerLockPath']); | ||
} catch (RuntimeException $exception) { | ||
throw new CouldNotParseFileException('Could not parse composer files.', 0, $exception); | ||
} | ||
|
||
$namespaces = $composerFilesParser->autoloadableNamespacesForRequirements($config['packages'], true); | ||
$token = $reference->getToken()->toString(); | ||
|
||
foreach ($namespaces as $namespace) { | ||
if (str_starts_with($token, $namespace)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
Oops, something went wrong.