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
42 changes: 34 additions & 8 deletions src/Command/BuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
use Php\Pie\ComposerIntegration\PieComposerRequest;
use Php\Pie\ComposerIntegration\PieOperation;
use Php\Pie\DependencyResolver\DependencyResolver;
use Php\Pie\DependencyResolver\InvalidPackageName;
use Php\Pie\DependencyResolver\UnableToResolveRequirement;
use Php\Pie\Installing\InstallForPhpProject\FindMatchingPackages;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand All @@ -28,6 +31,7 @@ public function __construct(
private readonly ContainerInterface $container,
private readonly DependencyResolver $dependencyResolver,
private readonly ComposerIntegrationHandler $composerIntegrationHandler,
private readonly FindMatchingPackages $findMatchingPackages,
) {
parent::__construct();
}
Expand All @@ -41,8 +45,19 @@ public function configure(): void

public function execute(InputInterface $input, OutputInterface $output): int
{
$targetPlatform = CommandHelper::determineTargetPlatformFromInputs($input, $output);
$requestedNameAndVersion = CommandHelper::requestedNameAndVersionPair($input);
$targetPlatform = CommandHelper::determineTargetPlatformFromInputs($input, $output);
try {
$requestedNameAndVersion = CommandHelper::requestedNameAndVersionPair($input);
} catch (InvalidPackageName $invalidPackageName) {
return CommandHelper::handlePackageNotFound(
$invalidPackageName,
$this->findMatchingPackages,
$output,
$targetPlatform,
$this->container,
);
}

$forceInstallPackageVersion = CommandHelper::determineForceInstallingPackageVersion($input);

$composer = PieComposerFactory::createPieComposer(
Expand All @@ -58,12 +73,23 @@ public function execute(InputInterface $input, OutputInterface $output): int
),
);

$package = ($this->dependencyResolver)(
$composer,
$targetPlatform,
$requestedNameAndVersion,
$forceInstallPackageVersion,
);
try {
$package = ($this->dependencyResolver)(
$composer,
$targetPlatform,
$requestedNameAndVersion,
$forceInstallPackageVersion,
);
} catch (UnableToResolveRequirement $unableToResolveRequirement) {
return CommandHelper::handlePackageNotFound(
$unableToResolveRequirement,
$this->findMatchingPackages,
$output,
$targetPlatform,
$this->container,
);
}

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

// Now we know what package we have, we can validate the configure options for the command and re-create the
Expand Down
101 changes: 101 additions & 0 deletions src/Command/CommandHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,45 @@
namespace Php\Pie\Command;

use Composer\Composer;
use Composer\Package\CompletePackageInterface;
use Composer\Package\Version\VersionParser;
use Composer\Repository\ComposerRepository;
use Composer\Repository\PathRepository;
use Composer\Repository\VcsRepository;
use Composer\Util\Platform;
use InvalidArgumentException;
use OutOfRangeException;
use Php\Pie\ComposerIntegration\PieComposerFactory;
use Php\Pie\ComposerIntegration\PieComposerRequest;
use Php\Pie\DependencyResolver\InvalidPackageName;
use Php\Pie\DependencyResolver\Package;
use Php\Pie\DependencyResolver\RequestedPackageAndVersion;
use Php\Pie\DependencyResolver\UnableToResolveRequirement;
use Php\Pie\Installing\InstallForPhpProject\FindMatchingPackages;
use Php\Pie\Platform as PiePlatform;
use Php\Pie\Platform\OperatingSystem;
use Php\Pie\Platform\TargetPhp\PhpBinaryPath;
use Php\Pie\Platform\TargetPhp\PhpizePath;
use Php\Pie\Platform\TargetPlatform;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;
use Webmozart\Assert\Assert;

use function array_key_exists;
use function array_map;
use function count;
use function is_array;
use function is_string;
use function reset;
use function sprintf;
use function str_starts_with;
use function strtolower;
use function substr;
use function trim;

use const PHP_VERSION;
Expand Down Expand Up @@ -326,4 +339,92 @@ public static function listRepositories(Composer $composer, OutputInterface $out
));
}
}

public static function handlePackageNotFound(
InvalidPackageName|UnableToResolveRequirement $exception,
FindMatchingPackages $findMatchingPackages,
OutputInterface $output,
TargetPlatform $targetPlatform,
ContainerInterface $container,
): int {
$pieComposer = PieComposerFactory::createPieComposer(
$container,
PieComposerRequest::noOperation(
$output,
$targetPlatform,
),
);

$requestedPackageName = $exception->requestedPackageAndVersion->package;
if (str_starts_with($requestedPackageName, 'ext-')) {
$requestedPackageName = substr($requestedPackageName, 4);
}

$output->writeln('');
$output->writeln(sprintf('<error>Could not install package: %s</error>', $requestedPackageName));
$output->writeln($exception->getMessage());

try {
$matches = array_map(
static function (array $match) use ($output, $pieComposer): array {
$composerMatchingPackage = $pieComposer->getRepositoryManager()->findPackage($match['name'], '*');

// Attempts to augment the Composer packages found with the PIE extension name
if ($composerMatchingPackage instanceof CompletePackageInterface) {
try {
$match['extension-name'] = Package
::fromComposerCompletePackage($composerMatchingPackage)
->extensionName()
->name();
} catch (Throwable $t) {
$output->writeln(
sprintf(
'Tried looking up extension name for %s, but failed: %s',
$match['name'],
$t->getMessage(),
),
OutputInterface::VERBOSITY_VERY_VERBOSE,
);
}
}

return $match;
},
$findMatchingPackages->for($pieComposer, $requestedPackageName),
);

if (count($matches)) {
$output->writeln('');
if (count($matches) === 1) {
$output->writeln('<info>Did you mean this?</info>');
} else {
$output->writeln('<info>Did you mean one of these?</info>');
}

array_map(
static function (array $match) use ($output): void {
$output->writeln(sprintf(
' - %s%s: %s',
$match['name'],
array_key_exists('extension-name', $match) && is_string($match['extension-name'])
? ' (provides extension: ' . $match['extension-name'] . ')'
: '',
$match['description'] ?? 'no description available',
));
},
$matches,
);
}
} catch (OutOfRangeException) {
$output->writeln(
sprintf(
'Tried searching for "%s", but nothing was found.',
$requestedPackageName,
),
OutputInterface::VERBOSITY_VERBOSE,
);
}

return 1;
}
}
42 changes: 34 additions & 8 deletions src/Command/DownloadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
use Php\Pie\ComposerIntegration\PieComposerRequest;
use Php\Pie\ComposerIntegration\PieOperation;
use Php\Pie\DependencyResolver\DependencyResolver;
use Php\Pie\DependencyResolver\InvalidPackageName;
use Php\Pie\DependencyResolver\UnableToResolveRequirement;
use Php\Pie\Installing\InstallForPhpProject\FindMatchingPackages;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand All @@ -28,6 +31,7 @@ public function __construct(
private readonly ContainerInterface $container,
private readonly DependencyResolver $dependencyResolver,
private readonly ComposerIntegrationHandler $composerIntegrationHandler,
private readonly FindMatchingPackages $findMatchingPackages,
) {
parent::__construct();
}
Expand All @@ -43,8 +47,19 @@ public function execute(InputInterface $input, OutputInterface $output): int
{
CommandHelper::validateInput($input, $this);

$targetPlatform = CommandHelper::determineTargetPlatformFromInputs($input, $output);
$requestedNameAndVersion = CommandHelper::requestedNameAndVersionPair($input);
$targetPlatform = CommandHelper::determineTargetPlatformFromInputs($input, $output);
try {
$requestedNameAndVersion = CommandHelper::requestedNameAndVersionPair($input);
} catch (InvalidPackageName $invalidPackageName) {
return CommandHelper::handlePackageNotFound(
$invalidPackageName,
$this->findMatchingPackages,
$output,
$targetPlatform,
$this->container,
);
}

$forceInstallPackageVersion = CommandHelper::determineForceInstallingPackageVersion($input);

$composer = PieComposerFactory::createPieComposer(
Expand All @@ -60,12 +75,23 @@ public function execute(InputInterface $input, OutputInterface $output): int
),
);

$package = ($this->dependencyResolver)(
$composer,
$targetPlatform,
$requestedNameAndVersion,
$forceInstallPackageVersion,
);
try {
$package = ($this->dependencyResolver)(
$composer,
$targetPlatform,
$requestedNameAndVersion,
$forceInstallPackageVersion,
);
} catch (UnableToResolveRequirement $unableToResolveRequirement) {
return CommandHelper::handlePackageNotFound(
$unableToResolveRequirement,
$this->findMatchingPackages,
$output,
$targetPlatform,
$this->container,
);
}

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

try {
Expand Down
39 changes: 32 additions & 7 deletions src/Command/InfoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use Php\Pie\ComposerIntegration\PieComposerRequest;
use Php\Pie\ComposerIntegration\PieOperation;
use Php\Pie\DependencyResolver\DependencyResolver;
use Php\Pie\DependencyResolver\InvalidPackageName;
use Php\Pie\DependencyResolver\UnableToResolveRequirement;
use Php\Pie\Installing\InstallForPhpProject\FindMatchingPackages;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand All @@ -26,6 +29,7 @@ final class InfoCommand extends Command
public function __construct(
private readonly ContainerInterface $container,
private readonly DependencyResolver $dependencyResolver,
private readonly FindMatchingPackages $findMatchingPackages,
) {
parent::__construct();
}
Expand All @@ -43,7 +47,17 @@ public function execute(InputInterface $input, OutputInterface $output): int

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

$requestedNameAndVersion = CommandHelper::requestedNameAndVersionPair($input);
try {
$requestedNameAndVersion = CommandHelper::requestedNameAndVersionPair($input);
} catch (InvalidPackageName $invalidPackageName) {
return CommandHelper::handlePackageNotFound(
$invalidPackageName,
$this->findMatchingPackages,
$output,
$targetPlatform,
$this->container,
);
}

$composer = PieComposerFactory::createPieComposer(
$this->container,
Expand All @@ -58,12 +72,23 @@ public function execute(InputInterface $input, OutputInterface $output): int
),
);

$package = ($this->dependencyResolver)(
$composer,
$targetPlatform,
$requestedNameAndVersion,
CommandHelper::determineForceInstallingPackageVersion($input),
);
try {
$package = ($this->dependencyResolver)(
$composer,
$targetPlatform,
$requestedNameAndVersion,
CommandHelper::determineForceInstallingPackageVersion($input),
);
} catch (UnableToResolveRequirement $unableToResolveRequirement) {
return CommandHelper::handlePackageNotFound(
$unableToResolveRequirement,
$this->findMatchingPackages,
$output,
$targetPlatform,
$this->container,
);
}

$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()));
Expand Down
Loading