This repository has been archived on 2025-08-17. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
versa/src/Console/Command/InstallCommand.php
Oliver Davies 52d933bc5e Ensure explode is passed a string and not null
Passing `null` to `explode()` is deprecated.
2024-02-23 16:15:41 +00:00

64 lines
2 KiB
PHP

<?php
namespace App\Console\Command;
use App\Action\DetermineProjectLanguage;
use App\Enum\ProjectLanguage;
use App\Process\Process;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
final class InstallCommand extends AbstractCommand
{
public function execute(InputInterface $input, OutputInterface $output): int
{
$extraArgs = $input->getOption('extra-args');
$workingDir = $input->getOption('working-dir');
$language = $input->getOption('language') ?? (new DetermineProjectLanguage(
filesystem: $this->filesystem,
workingDir: $workingDir,
))->getLanguage();
$filesystem = new Filesystem();
// TODO: Composer in Docker Compose?
$process = Process::create(
command: $this->getCommand(
filesystem: $filesystem,
language: $language,
workingDir: $workingDir,
),
extraArgs: explode(separator: ' ', string: strval($extraArgs)),
workingDir: $workingDir,
);
$process->setTimeout(null);
$process->run();
return Command::SUCCESS;
}
/**
* @param Filesystem $filesystem
* @param non-empty-string $language
* @param non-empty-string $workingDir
* @return non-empty-array<int, non-empty-string>
*/
private function getCommand(Filesystem $filesystem, string $language, string $workingDir): array
{
if ($language === ProjectLanguage::JavaScript->value) {
if ($filesystem->exists($workingDir.'/yarn.lock')) {
return ['yarn'];
} elseif ($filesystem->exists($workingDir.'/pnpm-lock.yaml')) {
return ['pnpm', 'install'];
} else {
return ['npm', 'install'];
}
}
return ['composer', 'install'];
}
}