Skip to content

Commit

Permalink
feat: Provide a non optimized requirement list
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry committed Mar 10, 2024
1 parent 131c795 commit 583198d
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/RequirementChecker/AppRequirementsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,47 @@ final class AppRequirementsFactory
{
private const SELF_PACKAGE = null;

public function createUnfiltered(
ComposerJson $composerJson,
ComposerLock $composerLock,
CompressionAlgorithm $compressionAlgorithm,
): Requirements {
return $this
->createBuilder(
$composerJson,
$composerLock,
$compressionAlgorithm,
)
->getAll();
}

public function create(
ComposerJson $composerJson,
ComposerLock $composerLock,
CompressionAlgorithm $compressionAlgorithm,
): Requirements {
return $this
->createBuilder(
$composerJson,
$composerLock,
$compressionAlgorithm,
)
->build();
}

private function createBuilder(
ComposerJson $composerJson,
ComposerLock $composerLock,
CompressionAlgorithm $compressionAlgorithm,
): RequirementsBuilder {
$requirementsBuilder = new RequirementsBuilder();

self::retrievePhpVersionRequirements($requirementsBuilder, $composerJson, $composerLock);
self::collectExtensionRequirementsFromCompressionAlgorithm($requirementsBuilder, $compressionAlgorithm);
self::collectComposerLockExtensionRequirements($composerLock, $requirementsBuilder);
self::collectComposerJsonExtensionRequirements($composerJson, $requirementsBuilder);

return $requirementsBuilder->build();
return $requirementsBuilder;
}

private static function retrievePhpVersionRequirements(
Expand Down
20 changes: 20 additions & 0 deletions src/RequirementChecker/Requirement.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,26 @@ public static function forRequiredExtension(string $extension, ?string $packageN
);
}

public static function forProvidedExtension(string $extension, ?string $packageName): self
{
return new self(
RequirementType::PROVIDED_EXTENSION,
$extension,
$packageName,
null === $packageName
? sprintf(
'This application provides the extension "%s".',
$extension,
)
: sprintf(
'The package "%s" provides the extension "%s".',
$packageName,
$extension,
),
'',
);
}

public static function forConflictingExtension(string $extension, ?string $packageName): self
{
return new self(
Expand Down
1 change: 1 addition & 0 deletions src/RequirementChecker/RequirementType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ enum RequirementType: string
{
case PHP = 'php';
case EXTENSION = 'extension';
case PROVIDED_EXTENSION = 'provided-extension';
case EXTENSION_CONFLICT = 'extension-conflict';
}
59 changes: 59 additions & 0 deletions src/RequirementChecker/RequirementsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,40 @@ public function addConflictingExtension(Extension $extension, ?string $source):
$this->conflictingExtensions[$extension->name][] = $source;
}

public function getAll(): Requirements
{
$requirements = $this->predefinedRequirements;

foreach ($this->getUnfilteredSortedRequiredExtensions() as $extensionName => $sources) {
foreach ($sources as $source) {
$requirements[] = Requirement::forRequiredExtension(
$extensionName,
$source,
);
}
}

foreach ($this->getSortedProvidedExtensions() as $extensionName => $sources) {
foreach ($sources as $source) {
$requirements[] = Requirement::forProvidedExtension(
$extensionName,
$source,
);
}
}

foreach ($this->getSortedConflictedExtensions() as $extensionName => $sources) {
foreach ($sources as $source) {
$requirements[] = Requirement::forConflictingExtension(
$extensionName,
$source,
);
}
}

return new Requirements($requirements);
}

public function build(): Requirements
{
$requirements = $this->predefinedRequirements;
Expand All @@ -74,6 +108,31 @@ public function build(): Requirements
return new Requirements($requirements);
}

/**
* @return array<string, list<string>>
*/
private function getUnfilteredSortedRequiredExtensions(): array
{
return array_map(
self::createSortedDistinctList(...),
self::sortByExtensionName(
$this->requiredExtensions,
),
);
}
/**
* @return array<string, list<string>>
*/
private function getSortedProvidedExtensions(): array
{
return array_map(
self::createSortedDistinctList(...),
self::sortByExtensionName(
$this->providedExtensions,
),
);
}

/**
* @return array<string, list<string>>
*/
Expand Down

0 comments on commit 583198d

Please sign in to comment.