|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Php\Pie\Command; |
| 6 | + |
| 7 | +use Composer\Package\Link; |
| 8 | +use OutOfRangeException; |
| 9 | +use Php\Pie\ComposerIntegration\PieComposerFactory; |
| 10 | +use Php\Pie\ComposerIntegration\PieComposerRequest; |
| 11 | +use Php\Pie\ExtensionName; |
| 12 | +use Php\Pie\ExtensionType; |
| 13 | +use Php\Pie\Installing\InstallForPhpProject\FindMatchingPackages; |
| 14 | +use Php\Pie\Installing\InstallForPhpProject\FindRootPackage; |
| 15 | +use Php\Pie\Installing\InstallForPhpProject\InstallSelectedPackage; |
| 16 | +use Psr\Container\ContainerInterface; |
| 17 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 18 | +use Symfony\Component\Console\Command\Command; |
| 19 | +use Symfony\Component\Console\Helper\QuestionHelper; |
| 20 | +use Symfony\Component\Console\Input\InputInterface; |
| 21 | +use Symfony\Component\Console\Output\ConsoleOutputInterface; |
| 22 | +use Symfony\Component\Console\Output\NullOutput; |
| 23 | +use Symfony\Component\Console\Output\OutputInterface; |
| 24 | +use Symfony\Component\Console\Question\ChoiceQuestion; |
| 25 | +use Throwable; |
| 26 | + |
| 27 | +use function array_filter; |
| 28 | +use function array_keys; |
| 29 | +use function array_map; |
| 30 | +use function array_merge; |
| 31 | +use function array_walk; |
| 32 | +use function assert; |
| 33 | +use function getcwd; |
| 34 | +use function in_array; |
| 35 | +use function sprintf; |
| 36 | +use function str_starts_with; |
| 37 | +use function strlen; |
| 38 | +use function strpos; |
| 39 | +use function substr; |
| 40 | + |
| 41 | +use const PHP_EOL; |
| 42 | + |
| 43 | +#[AsCommand( |
| 44 | + name: 'install-extensions-for-project', |
| 45 | + description: 'Check a project for its extension dependencies, and offers to install them', |
| 46 | +)] |
| 47 | +final class InstallExtensionsForProjectCommand extends Command |
| 48 | +{ |
| 49 | + public function __construct( |
| 50 | + private readonly FindRootPackage $findRootPackage, |
| 51 | + private readonly FindMatchingPackages $findMatchingPackages, |
| 52 | + private readonly InstallSelectedPackage $installSelectedPackage, |
| 53 | + private readonly ContainerInterface $container, |
| 54 | + ) { |
| 55 | + parent::__construct(); |
| 56 | + } |
| 57 | + |
| 58 | + public function configure(): void |
| 59 | + { |
| 60 | + parent::configure(); |
| 61 | + |
| 62 | + CommandHelper::configurePhpConfigOptions($this); |
| 63 | + } |
| 64 | + |
| 65 | + public function execute(InputInterface $input, OutputInterface $output): int |
| 66 | + { |
| 67 | + $helper = $this->getHelper('question'); |
| 68 | + assert($helper instanceof QuestionHelper); |
| 69 | + |
| 70 | + $targetPlatform = CommandHelper::determineTargetPlatformFromInputs($input, $output); |
| 71 | + |
| 72 | + $rootPackage = $this->findRootPackage->forCwd($input, $output); |
| 73 | + |
| 74 | + if (ExtensionType::isValid($rootPackage->getType())) { |
| 75 | + $output->writeln('<error>This composer.json is for an extension, installing missing packages is not supported.</error>'); |
| 76 | + |
| 77 | + return Command::INVALID; |
| 78 | + } |
| 79 | + |
| 80 | + $output->writeln(sprintf( |
| 81 | + 'Checking extensions for your project <info>%s</info> (path: %s)', |
| 82 | + $rootPackage->getPrettyName(), |
| 83 | + getcwd(), |
| 84 | + )); |
| 85 | + |
| 86 | + $rootPackageExtensionsRequired = array_filter( |
| 87 | + array_merge($rootPackage->getRequires(), $rootPackage->getDevRequires()), |
| 88 | + static function (Link $link) { |
| 89 | + $linkTarget = $link->getTarget(); |
| 90 | + if (! str_starts_with($linkTarget, 'ext-')) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + return ExtensionName::isValidExtensionName(substr($linkTarget, strlen('ext-'))); |
| 95 | + }, |
| 96 | + ); |
| 97 | + |
| 98 | + $pieComposer = PieComposerFactory::createPieComposer( |
| 99 | + $this->container, |
| 100 | + PieComposerRequest::noOperation( |
| 101 | + new NullOutput(), |
| 102 | + $targetPlatform, |
| 103 | + ), |
| 104 | + ); |
| 105 | + |
| 106 | + $phpEnabledExtensions = array_keys($targetPlatform->phpBinaryPath->extensions()); |
| 107 | + |
| 108 | + $anyErrorsHappened = false; |
| 109 | + |
| 110 | + array_walk( |
| 111 | + $rootPackageExtensionsRequired, |
| 112 | + function (Link $link) use ($pieComposer, $phpEnabledExtensions, $input, $output, $helper, &$anyErrorsHappened): void { |
| 113 | + $extension = ExtensionName::normaliseFromString($link->getTarget()); |
| 114 | + |
| 115 | + if (in_array($extension->name(), $phpEnabledExtensions)) { |
| 116 | + $output->writeln(sprintf( |
| 117 | + '%s: <info>%s</info> ✅ Already installed', |
| 118 | + $link->getDescription(), |
| 119 | + $extension->name(), |
| 120 | + )); |
| 121 | + |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + $output->writeln(sprintf( |
| 126 | + '%s: <comment>%s</comment> ⚠️ Missing', |
| 127 | + $link->getDescription(), |
| 128 | + $extension->name(), |
| 129 | + )); |
| 130 | + |
| 131 | + try { |
| 132 | + $matches = $this->findMatchingPackages->for($pieComposer, $extension); |
| 133 | + } catch (OutOfRangeException) { |
| 134 | + $anyErrorsHappened = true; |
| 135 | + |
| 136 | + $message = sprintf( |
| 137 | + '<error>No packages were found for %s</error>', |
| 138 | + $extension->nameWithExtPrefix(), |
| 139 | + ); |
| 140 | + |
| 141 | + if ($output instanceof ConsoleOutputInterface) { |
| 142 | + $output->getErrorOutput()->writeln($message); |
| 143 | + |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + $output->writeln($message); |
| 148 | + |
| 149 | + return; |
| 150 | + } |
| 151 | + |
| 152 | + $choiceQuestion = new ChoiceQuestion( |
| 153 | + "\nThe following packages may be suitable, which would you like to install: ", |
| 154 | + array_merge( |
| 155 | + ['None'], |
| 156 | + array_map( |
| 157 | + static function (array $match): string { |
| 158 | + return sprintf('%s: %s', $match['name'], $match['description'] ?? 'no description available'); |
| 159 | + }, |
| 160 | + $matches, |
| 161 | + ), |
| 162 | + ), |
| 163 | + 0, |
| 164 | + ); |
| 165 | + |
| 166 | + $selectedPackageAnswer = (string) $helper->ask($input, $output, $choiceQuestion); |
| 167 | + |
| 168 | + if ($selectedPackageAnswer === 'None') { |
| 169 | + $output->writeln('Okay I won\'t install anything for ' . $extension->name()); |
| 170 | + |
| 171 | + return; |
| 172 | + } |
| 173 | + |
| 174 | + try { |
| 175 | + $this->installSelectedPackage->withPieCli( |
| 176 | + substr($selectedPackageAnswer, 0, (int) strpos($selectedPackageAnswer, ':')), |
| 177 | + $input, |
| 178 | + $output, |
| 179 | + ); |
| 180 | + } catch (Throwable $t) { |
| 181 | + $anyErrorsHappened = true; |
| 182 | + |
| 183 | + $message = '<error>' . $t->getMessage() . '</error>'; |
| 184 | + |
| 185 | + if ($output instanceof ConsoleOutputInterface) { |
| 186 | + $output->getErrorOutput()->writeln($message); |
| 187 | + |
| 188 | + return; |
| 189 | + } |
| 190 | + |
| 191 | + $output->writeln($message); |
| 192 | + } |
| 193 | + }, |
| 194 | + ); |
| 195 | + |
| 196 | + $output->writeln(PHP_EOL . 'Finished checking extensions.'); |
| 197 | + |
| 198 | + /** |
| 199 | + * @psalm-suppress TypeDoesNotContainType |
| 200 | + * @psalm-suppress RedundantCondition |
| 201 | + */ |
| 202 | + return $anyErrorsHappened ? self::FAILURE : self::SUCCESS; |
| 203 | + } |
| 204 | +} |
0 commit comments