Skip to content

Commit

Permalink
Info command improvements (#112)
Browse files Browse the repository at this point in the history
Fix the info command for tar GZ & BZ2 compressed PHARs and make the info command able to inspect PHARs with no extension.

Closes #99
  • Loading branch information
theofidry authored Apr 10, 2018
1 parent 5743108 commit 6592bdc
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 44 deletions.
1 change: 0 additions & 1 deletion .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ $config = PhpCsFixer\Config::create()
'no_useless_else' => true,
'no_useless_return' => true,
'no_unused_imports' => true,
'ordered_class_elements' => true,
'ordered_imports' => true,
'php_unit_test_class_requires_covers' => true,
'phpdoc_add_missing_param_annotation' => true,
Expand Down
Empty file added fixtures/info/foo
Empty file.
Binary file added fixtures/info/new-simple-phar.zip
Binary file not shown.
Binary file added fixtures/info/simple-phar
Binary file not shown.
Binary file added fixtures/info/simple-phar.tar.bz2
Binary file not shown.
Binary file added fixtures/info/simple-phar.tar.gz
Binary file not shown.
152 changes: 111 additions & 41 deletions src/Console/Command/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@

namespace KevinGH\Box\Console\Command;

use DateTimeImmutable;
use DirectoryIterator;
use Phar;
use PharData;
use PharFileInfo;
use RecursiveIteratorIterator;
use Symfony\Component\Console\Command\Command;
Expand All @@ -24,17 +26,23 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Throwable;
use UnexpectedValueException;
use function array_fill_keys;
use function array_filter;
use function array_reduce;
use function array_sum;
use function count;
use function end;
use function is_array;
use function iterator_to_array;
use function KevinGH\Box\FileSystem\copy;
use function KevinGH\Box\FileSystem\remove;
use function KevinGH\Box\formatted_filesize;
use function key;
use function realpath;
use function sprintf;
use function sys_get_temp_dir;

final class Info extends Command
{
Expand Down Expand Up @@ -64,29 +72,6 @@ final class Info extends Command
Phar::GZ => 'GZ',
];

/**
* @override
*/
public function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$io->writeln('');

if (null === ($file = $input->getArgument(self::PHAR_ARG))) {
return $this->executeShowGlobalInfo($output, $io);
}

$phar = new Phar($file);

return $this->executeShowPharInfo(
$phar,
$input->getOption(self::LIST_OPT),
'indent' === $input->getOption(self::MODE_OPT),
$output,
$io
);
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -136,7 +121,74 @@ protected function configure(): void
);
}

private function executeShowGlobalInfo(OutputInterface $output, SymfonyStyle $io): int
/**
* @override
*/
public function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$io->writeln('');

if (null === ($file = $input->getArgument(self::PHAR_ARG))) {
return $this->showGlobalInfo($output, $io);
}

$file = realpath($file);

if (false === $file) {
$io->error(
sprintf(
'The file "%s" could not be found.',
$input->getArgument(self::PHAR_ARG)
)
);

return 1;
}

if ('' === pathinfo($file, PATHINFO_EXTENSION)) {
// It is likely to be a PHAR without extension
copy($file, $tmpFile = sys_get_temp_dir().'/'.(new DateTimeImmutable())->getTimestamp().$file.'.phar');

try {
return $this->showInfo($tmpFile, $file, $input, $output, $io);
} finally {
remove($tmpFile);
}
}

return $this->showInfo($file, $file, $input, $output, $io);
}

public function showInfo(string $file, string $originalFile, InputInterface $input, OutputInterface $output, SymfonyStyle $io): int
{
try {
try {
$phar = new Phar($file);
} catch (UnexpectedValueException $exception) {
$phar = new PharData($file);
}

return $this->showPharInfo(
$phar,
$input->getOption(self::LIST_OPT),
'indent' === $input->getOption(self::MODE_OPT),
$output,
$io
);
} catch (Throwable $throwable) {
$io->error(
sprintf(
'Could not read the file "%s".',
$originalFile
)
);

return 1;
}
}

private function showGlobalInfo(OutputInterface $output, SymfonyStyle $io): int
{
$this->render(
$output,
Expand All @@ -153,7 +205,10 @@ private function executeShowGlobalInfo(OutputInterface $output, SymfonyStyle $io
return 0;
}

private function executeShowPharInfo(Phar $phar, bool $content, bool $indent, OutputInterface $output, SymfonyStyle $io): int
/**
* @param Phar|PharData $phar
*/
private function showPharInfo($phar, bool $content, bool $indent, OutputInterface $output, SymfonyStyle $io): int
{
$signature = $phar->getSignature();

Expand All @@ -177,12 +232,16 @@ private function executeShowPharInfo(Phar $phar, bool $content, bool $indent, Ou
return 0;
}

private function showPharGlobalInfo(Phar $phar, SymfonyStyle $io, $signature): void
/**
* @param Phar|PharData $phar
* @param mixed $signature
*/
private function showPharGlobalInfo($phar, SymfonyStyle $io, $signature): void
{
$io->writeln(
sprintf(
'<comment>API Version:</comment> %s',
$phar->getVersion()
'' !== $phar->getVersion() ? $phar->getVersion() : 'No information found'
)
);
$io->writeln('');
Expand Down Expand Up @@ -225,19 +284,21 @@ private function showPharGlobalInfo(Phar $phar, SymfonyStyle $io, $signature): v
}
$io->writeln('');

$io->writeln(
sprintf(
'<comment>Signature:</comment> %s',
$signature['hash_type']
)
);
$io->writeln(
sprintf(
'<comment>Signature Hash:</comment> %s',
$signature['hash']
)
);
$io->writeln('');
if (false !== $signature) {
$io->writeln(
sprintf(
'<comment>Signature:</comment> %s',
$signature['hash_type']
)
);
$io->writeln(
sprintf(
'<comment>Signature Hash:</comment> %s',
$signature['hash']
)
);
$io->writeln('');
}

$metadata = var_export($phar->getMetadata(), true);

Expand Down Expand Up @@ -344,13 +405,22 @@ private function renderContents(
}
}

private function retrieveCompressionCount(Phar $phar): array
/**
* @param Phar|PharData $phar
*/
private function retrieveCompressionCount($phar): array
{
$count = array_fill_keys(
self::ALGORITHMS,
0
);

if ($phar instanceof PharData) {
$count[self::ALGORITHMS[$phar->isCompressed()]] = 1;

return $count;
}

$countFile = function (array $count, PharFileInfo $file) {
if (false === $file->isCompressed()) {
++$count['None'];
Expand Down
Loading

0 comments on commit 6592bdc

Please sign in to comment.