Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bin/pie
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Php\Pie;

use Php\Pie\Command\BuildCommand;
use Php\Pie\Command\DownloadCommand;
use Php\Pie\Command\InfoCommand;
use Php\Pie\Command\InstallCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
Expand All @@ -25,6 +26,7 @@ $application->setCommandLoader(new ContainerCommandLoader(
'download' => DownloadCommand::class,
'build' => BuildCommand::class,
'install' => InstallCommand::class,
'info' => InfoCommand::class,
]
));
$application->run($container->get(InputInterface::class), $container->get(OutputInterface::class));
25 changes: 19 additions & 6 deletions src/Command/CommandHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@

use const PHP_VERSION;

/** @psalm-type RequestedNameAndVersionPair = array{name: non-empty-string, version: non-empty-string|null} */
/**
* @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks
*
* @psalm-type RequestedNameAndVersionPair = array{name: non-empty-string, version: non-empty-string|null}
*/
final class CommandHelper
{
private const ARG_REQUESTED_PACKAGE_AND_VERSION = 'requested-package-and-version';
Expand Down Expand Up @@ -146,18 +150,27 @@ public static function requestedNameAndVersionPair(InputInterface $input): array
}

/** @param RequestedNameAndVersionPair $requestedNameAndVersionPair */
public static function downloadPackage(
public static function resolvePackage(
DependencyResolver $dependencyResolver,
TargetPlatform $targetPlatform,
array $requestedNameAndVersionPair,
DownloadAndExtract $downloadAndExtract,
OutputInterface $output,
): DownloadedPackage {
$package = ($dependencyResolver)(
): Package {
return ($dependencyResolver)(
$targetPlatform,
$requestedNameAndVersionPair['name'],
$requestedNameAndVersionPair['version'],
);
}

/** @param RequestedNameAndVersionPair $requestedNameAndVersionPair */
public static function downloadPackage(
DependencyResolver $dependencyResolver,
TargetPlatform $targetPlatform,
array $requestedNameAndVersionPair,
DownloadAndExtract $downloadAndExtract,
OutputInterface $output,
): DownloadedPackage {
$package = self::resolvePackage($dependencyResolver, $targetPlatform, $requestedNameAndVersionPair);

$output->writeln(sprintf('<info>Found package:</info> %s which provides <info>%s</info>', $package->prettyNameAndVersion(), $package->extensionName->nameWithExtPrefix()));

Expand Down
68 changes: 68 additions & 0 deletions src/Command/InfoCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace Php\Pie\Command;

use Php\Pie\DependencyResolver\DependencyResolver;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

use function count;
use function sprintf;

#[AsCommand(
name: 'info',
description: 'Show metadata about a given extension.',
)]
final class InfoCommand extends Command
{
public function __construct(
private readonly DependencyResolver $dependencyResolver,
) {
parent::__construct();
}

public function configure(): void
{
parent::configure();

CommandHelper::configureOptions($this);
}

public function execute(InputInterface $input, OutputInterface $output): int
{
CommandHelper::validateInput($input, $this);

$targetPlatform = CommandHelper::determineTargetPlatformFromInputs($input, $output);

$requestedNameAndVersionPair = CommandHelper::requestedNameAndVersionPair($input);

$package = CommandHelper::resolvePackage(
$this->dependencyResolver,
$targetPlatform,
$requestedNameAndVersionPair,
);

$output->writeln(sprintf('<info>Found package:</info> %s which provides <info>%s</info>', $package->prettyNameAndVersion(), $package->extensionName->nameWithExtPrefix()));

$output->writeln(sprintf('Extension name: %s', $package->extensionName->name()));
$output->writeln(sprintf('Extension type: %s (%s)', $package->extensionType->value, $package->extensionType->name));
$output->writeln(sprintf('Composer package name: %s', $package->name));
$output->writeln(sprintf('Version: %s', $package->version));
$output->writeln(sprintf('Download URL: %s', $package->downloadUrl ?? '(not specified)'));

if (count($package->configureOptions)) {
$output->writeln('Configure options:');
foreach ($package->configureOptions as $configureOption) {
$output->writeln(sprintf(' --%s%s (%s)', $configureOption->name, $configureOption->needsValue ? '=?' : '', $configureOption->description));
}
} else {
$output->writeln('No configure options are specified.');
}

return Command::SUCCESS;
}
}
2 changes: 2 additions & 0 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Php\Pie\Building\WindowsBuild;
use Php\Pie\Command\BuildCommand;
use Php\Pie\Command\DownloadCommand;
use Php\Pie\Command\InfoCommand;
use Php\Pie\Command\InstallCommand;
use Php\Pie\DependencyResolver\DependencyResolver;
use Php\Pie\DependencyResolver\ResolveDependencyWithComposer;
Expand Down Expand Up @@ -52,6 +53,7 @@ public static function factory(): ContainerInterface
$container->singleton(DownloadCommand::class);
$container->singleton(BuildCommand::class);
$container->singleton(InstallCommand::class);
$container->singleton(InfoCommand::class);

$container->singleton(IOInterface::class, static function (ContainerInterface $container): IOInterface {
return new ConsoleIO(
Expand Down
3 changes: 0 additions & 3 deletions src/DependencyResolver/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
*/
final class Package
{
public const TYPE_PHP_MODULE = 'php-ext';
public const TYPE_ZEND_EXTENSION = 'php-ext-zend';

/** @param list<ConfigureOption> $configureOptions */
public function __construct(
public readonly ExtensionType $extensionType,
Expand Down
45 changes: 45 additions & 0 deletions test/integration/Command/InfoCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Php\PieIntegrationTest\Command;

use Php\Pie\Command\InfoCommand;
use Php\Pie\Container;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;

use const PHP_VERSION;
use const PHP_VERSION_ID;

#[CoversClass(InfoCommand::class)]
final class InfoCommandTest extends TestCase
{
private CommandTester $commandTester;

public function setUp(): void
{
$this->commandTester = new CommandTester(Container::factory()->get(InfoCommand::class));
}

public function testInfoCommandDisplaysInformation(): void
{
if (PHP_VERSION_ID < 80300 || PHP_VERSION_ID >= 80400) {
self::markTestSkipped('This test can only run on PHP 8.3 - you are running ' . PHP_VERSION);
}

$this->commandTester->execute(['requested-package-and-version' => 'asgrim/example-pie-extension:dev-main#9b5e6c80a1e05556e4e6824f0c112a4992cee001']);

$this->commandTester->assertCommandIsSuccessful();

$outputString = $this->commandTester->getDisplay();
self::assertStringContainsString('Extension name: example_pie_extension', $outputString);
self::assertStringContainsString('Extension type: php-ext (PhpModule)', $outputString);
self::assertStringContainsString('Composer package name: asgrim/example-pie-extension', $outputString);
self::assertStringContainsString('Version: dev-main', $outputString);
self::assertStringContainsString('Download URL: https://api.github.com/repos/asgrim/example-pie-extension/zipball/9b5e6c80a1e05556e4e6824f0c112a4992cee001', $outputString);
self::assertStringContainsString('--enable-example-pie-extension (whether to enable example-pie-extension support)', $outputString);
self::assertStringContainsString('--with-hello-name=? (Name ot use when saying hello)', $outputString);
}
}