Skip to content

Commit

Permalink
feat: Show the requirement checker in the PHAR info (#1111)
Browse files Browse the repository at this point in the history
Related to #857
  • Loading branch information
theofidry authored Oct 22, 2023
1 parent f330e3e commit 59d2d4f
Show file tree
Hide file tree
Showing 11 changed files with 421 additions and 8 deletions.
Binary file added fixtures/info/req-checker-conflict.phar
Binary file not shown.
Binary file added fixtures/info/req-checker-empty.phar
Binary file not shown.
Binary file not shown.
Binary file added fixtures/info/req-checker-ext-and-php.phar
Binary file not shown.
Binary file added fixtures/info/req-checker-ext.phar
Binary file not shown.
Binary file added fixtures/info/req-checker-old-req.phar
Binary file not shown.
Binary file added fixtures/info/req-checker-php.phar
Binary file not shown.
10 changes: 9 additions & 1 deletion src/Console/Command/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,15 @@ private static function showPharInfo(
bool $indent,
IO $io,
): int {
self::showPharMeta($pharInfo, $io);
PharInfoRenderer::renderVersion($pharInfo, $io);

$io->newLine();

PharInfoRenderer::renderShortSummary(
$pharInfo,
$io,
static fn () => $io->newLine(),
);

if ($content) {
PharInfoRenderer::renderContent(
Expand Down
138 changes: 138 additions & 0 deletions src/Console/PharInfoRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,20 @@
use KevinGH\Box\NotInstantiable;
use KevinGH\Box\Phar\CompressionAlgorithm;
use KevinGH\Box\Phar\PharInfo;
use KevinGH\Box\RequirementChecker\Requirement;
use KevinGH\Box\RequirementChecker\RequirementType;
use SplFileInfo;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Path;
use function array_filter;
use function array_key_last;
use function array_map;
use function array_reduce;
use function array_sum;
use function array_values;
use function count;
use function implode;
use function is_array;
use function KevinGH\Box\format_size;
use function KevinGH\Box\format_size as format_size1;
use function KevinGH\Box\noop;
Expand All @@ -44,6 +51,7 @@ final class PharInfoRenderer
{
use NotInstantiable;

private const BOX_REQUIREMENTS = '.box/.requirements.php';
private const INDENT_SIZE = 2;

public static function renderShortSummary(
Expand All @@ -58,6 +66,7 @@ public static function renderShortSummary(
self::renderSignature(...),
self::renderMetadata(...),
self::renderTimestamp(...),
self::renderRequirementChecker(...),
self::renderContentsSummary(...),
];

Expand Down Expand Up @@ -184,6 +193,41 @@ public static function renderTimestamp(PharInfo $pharInfo, IO $io): void
);
}

public static function renderRequirementChecker(
PharInfo $pharInfo,
IO $io,
): void {
$requirements = $pharInfo->getFiles()[self::BOX_REQUIREMENTS] ?? null;

if (null === $requirements) {
$io->writeln('<comment>RequirementChecker:</comment> Not found.');

return;
}

$evaluatedRequirements = require $requirements->getPathname();

if (!is_array($evaluatedRequirements)) {
$io->writeln('<comment>RequirementChecker:</comment> Could not be checked.');

return;
}

$io->write('<comment>RequirementChecker:</comment>');

if (0 === count($evaluatedRequirements)) {
$io->writeln(' No requirement found.');

return;
}
$io->writeln('');

[$required, $conflicting] = self::retrieveRequirements($evaluatedRequirements);

self::renderRequiredSection($required, $io);
self::renderConflictingSection($conflicting, $io);
}

public static function renderContentsSummary(PharInfo $pharInfo, IO $io): void
{
$count = array_filter($pharInfo->getFilesCompressionCount());
Expand Down Expand Up @@ -252,6 +296,100 @@ public static function renderContent(
}
}

/**
* @return array{Requirement[], Requirement[]}
*/
private static function retrieveRequirements(array $requirements): array
{
$evaluatedRequirements = array_map(
Requirement::fromArray(...),
$requirements,
);

[$required, $conflicting] = array_reduce(
$evaluatedRequirements,
static function ($carry, Requirement $requirement): array {
$hash = implode(
':',
[
$requirement->type->value,
$requirement->condition,
$requirement->source,
],
);

if (RequirementType::EXTENSION_CONFLICT === $requirement->type) {
$carry[1][$hash] = $requirement;
} else {
$carry[0][$hash] = $requirement;
}

return $carry;
},
[[], []],
);

return [
array_values($required),
array_values($conflicting),
];
}

/**
* @param Requirement[] $required
*/
private static function renderRequiredSection(
array $required,
IO $io,
): void {
if (0 === count($required)) {
return;
}

$io->writeln(' <comment>Required:</comment>');
$io->writeln(
array_map(
static fn (Requirement $requirement) => match ($requirement->type) {
RequirementType::PHP => sprintf(
' - PHP %s (%s)',
$requirement->condition,
$requirement->source ?? 'root',
),
RequirementType::EXTENSION => sprintf(
' - ext-%s (%s)',
$requirement->condition,
$requirement->source ?? 'root',
),
},
$required,
),
);
}

/**
* @param Requirement[] $conflicting
*/
private static function renderConflictingSection(
array $conflicting,
IO $io,
): void {
if (0 === count($conflicting)) {
return;
}

$io->writeln(' <comment>Conflict:</comment>');
$io->writeln(
array_map(
static fn (Requirement $requirement) => sprintf(
' - ext-%s (%s)',
$requirement->condition,
$requirement->source ?? 'root',
),
$conflicting,
),
);
}

private static function renderParentDirectoriesIfNecessary(
SplFileInfo $fileInfo,
OutputInterface $output,
Expand Down
Loading

0 comments on commit 59d2d4f

Please sign in to comment.