|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Leaf\Console; |
| 4 | + |
| 5 | +use Symfony\Component\Console\Command\Command; |
| 6 | +use Symfony\Component\Console\Input\InputArgument; |
| 7 | +use Symfony\Component\Console\Input\InputInterface; |
| 8 | +use Symfony\Component\Console\Output\OutputInterface; |
| 9 | +use Symfony\Component\Process\Process; |
| 10 | + |
| 11 | +class InstallCommand extends Command |
| 12 | +{ |
| 13 | + protected static $defaultName = 'install'; |
| 14 | + |
| 15 | + protected function configure() |
| 16 | + { |
| 17 | + $this |
| 18 | + ->setHelp("Install a new package") |
| 19 | + ->setDescription("Add a new package to your leaf app") |
| 20 | + ->addArgument("package", InputArgument::REQUIRED, "package to install") |
| 21 | + ->addArgument("version", InputArgument::OPTIONAL, "version to install"); |
| 22 | + } |
| 23 | + |
| 24 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 25 | + { |
| 26 | + $package = $input->getArgument("package"); |
| 27 | + $version = $input->getArgument("version") ?? null; |
| 28 | + |
| 29 | + $output->writeln("<info>Installing $package...</info>"); |
| 30 | + // $output->write(shell_exec("composer require $package")); |
| 31 | + $composer = $this->findComposer(); |
| 32 | + $process = Process::fromShellCommandline("$composer require $package $version", null, null, null, null); |
| 33 | + |
| 34 | + $process->run(function ($type, $line) use ($output) { |
| 35 | + $output->write($line); |
| 36 | + }); |
| 37 | + |
| 38 | + if ($process->isSuccessful()) { |
| 39 | + $output->writeln("<comment>$package installed successfully!</comment>"); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Get the composer command for the environment. |
| 45 | + * |
| 46 | + * @return string |
| 47 | + */ |
| 48 | + protected function findComposer() |
| 49 | + { |
| 50 | + $composerPath = getcwd() . '/composer.phar'; |
| 51 | + |
| 52 | + if (file_exists($composerPath)) { |
| 53 | + return '"' . PHP_BINARY . '" ' . $composerPath; |
| 54 | + } |
| 55 | + |
| 56 | + return 'composer'; |
| 57 | + } |
| 58 | +} |
0 commit comments