2025-04-08 23:00:21 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Loom\Spinner\Command;
|
|
|
|
|
2025-04-12 15:14:28 +01:00
|
|
|
use Loom\Spinner\Classes\Collection\FilePathCollection;
|
2025-04-13 10:43:30 +01:00
|
|
|
use Loom\Spinner\Classes\Config\Config;
|
2025-04-12 15:14:28 +01:00
|
|
|
use Loom\Spinner\Classes\File\SpinnerFilePath;
|
|
|
|
use Loom\Spinner\Classes\OS\System;
|
|
|
|
use Loom\Spinner\Command\Interface\ConsoleCommandInterface;
|
2025-04-13 10:43:30 +01:00
|
|
|
use Loom\Utility\FilePath\FilePath;
|
2025-04-08 23:00:21 +01:00
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
2025-04-12 17:28:57 +01:00
|
|
|
use Symfony\Component\Yaml\Yaml;
|
2025-04-08 23:00:21 +01:00
|
|
|
|
2025-04-12 15:14:28 +01:00
|
|
|
class AbstractSpinnerCommand extends Command implements ConsoleCommandInterface
|
2025-04-08 23:00:21 +01:00
|
|
|
{
|
2025-04-13 10:43:30 +01:00
|
|
|
protected Config $config;
|
|
|
|
protected SymfonyStyle $style;
|
|
|
|
protected System $system;
|
2025-04-12 10:47:55 +01:00
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->system = new System();
|
2025-04-13 12:59:44 +01:00
|
|
|
$this->config = new Config();
|
2025-04-12 10:47:55 +01:00
|
|
|
|
|
|
|
parent::__construct();
|
|
|
|
}
|
2025-04-08 23:00:21 +01:00
|
|
|
|
2025-04-12 15:14:28 +01:00
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
|
|
{
|
|
|
|
$this->setStyle($input, $output);
|
|
|
|
|
2025-04-12 17:28:57 +01:00
|
|
|
$this->style->title('Loom Spinner');
|
|
|
|
|
2025-04-12 15:14:28 +01:00
|
|
|
if (!$this->system->isDockerEngineRunning()) {
|
|
|
|
$this->style->error('It looks like the Docker Engine is not running. Please start it and try again.');
|
|
|
|
|
|
|
|
return Command::FAILURE;
|
|
|
|
}
|
|
|
|
|
2025-04-13 10:43:30 +01:00
|
|
|
if (!$this->validatePathArgument($input)) {
|
|
|
|
return Command::FAILURE;
|
2025-04-12 15:14:28 +01:00
|
|
|
}
|
|
|
|
|
2025-04-13 10:43:30 +01:00
|
|
|
$this->validateNameArgument($input);
|
2025-04-12 17:28:57 +01:00
|
|
|
|
2025-04-12 15:14:28 +01:00
|
|
|
return Command::SUCCESS;
|
|
|
|
}
|
|
|
|
|
2025-04-12 17:28:57 +01:00
|
|
|
protected function buildDockerComposeCommand(string $command, bool $daemon = true): string
|
|
|
|
{
|
|
|
|
return sprintf(
|
2025-04-12 19:09:40 +01:00
|
|
|
'cd %s && docker-compose --env-file=%s %s%s',
|
2025-04-13 12:59:44 +01:00
|
|
|
$this->config->getFilePaths()->get('projectData')->getAbsolutePath(),
|
|
|
|
$this->config->getFilePaths()->get('projectEnv')->getAbsolutePath(),
|
2025-04-12 17:28:57 +01:00
|
|
|
$command,
|
|
|
|
$daemon ? ' -d' : ''
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2025-04-13 10:43:30 +01:00
|
|
|
private function validatePathArgument(InputInterface $input): bool
|
|
|
|
{
|
|
|
|
if ($input->hasArgument('path')) {
|
|
|
|
$projectDirectory = new FilePath($input->getArgument('path'));
|
|
|
|
|
|
|
|
if (!$projectDirectory->exists() || !$projectDirectory->isDirectory()) {
|
|
|
|
$this->style->error('The provided path is not a valid directory.');
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2025-04-13 12:59:44 +01:00
|
|
|
$this->config->addFilePath($projectDirectory, 'project');
|
2025-04-13 10:43:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function validateNameArgument(InputInterface $input): void
|
|
|
|
{
|
|
|
|
if ($input->hasArgument('name')) {
|
2025-04-13 12:59:44 +01:00
|
|
|
$this->config->addFilePath(
|
2025-04-13 10:43:30 +01:00
|
|
|
new SpinnerFilePath(sprintf('data/environments/%s', $input->getArgument('name'))),
|
|
|
|
'projectData'
|
|
|
|
);
|
2025-04-13 12:59:44 +01:00
|
|
|
$this->config->addFilePath(
|
2025-04-13 10:43:30 +01:00
|
|
|
new SpinnerFilePath(sprintf('data/environments/%s/.env', $input->getArgument('name'))),
|
|
|
|
'projectEnv'
|
|
|
|
);
|
2025-04-13 12:59:44 +01:00
|
|
|
$this->config->addFilePath(
|
2025-04-13 10:43:30 +01:00
|
|
|
new SpinnerFilePath(sprintf('data/environments/%s/docker-compose.yml', $input->getArgument('name'))),
|
|
|
|
'projectDockerCompose'
|
|
|
|
);
|
2025-04-13 12:59:44 +01:00
|
|
|
$this->config->addFilePath(
|
|
|
|
new SpinnerFilePath(sprintf('data/environments/%s/php-fpm/Dockerfile', $input->getArgument('name'))),
|
|
|
|
'projectPhpFpmDockerfile'
|
|
|
|
);
|
|
|
|
$this->config->addFilePath(
|
2025-04-13 20:07:48 +01:00
|
|
|
new SpinnerFilePath(sprintf('data/environments/%s/nginx/Dockerfile', $input->getArgument('name'))),
|
|
|
|
'projectNginxDockerfile'
|
2025-04-13 12:59:44 +01:00
|
|
|
);
|
|
|
|
$this->config->addFilePath(
|
|
|
|
new FilePath($this->config->getFilePaths()->get('project')->getAbsolutePath() . DIRECTORY_SEPARATOR . 'spinner.yaml'),
|
2025-04-13 10:43:30 +01:00
|
|
|
'projectCustomConfig'
|
|
|
|
);
|
|
|
|
}
|
2025-04-12 17:28:57 +01:00
|
|
|
}
|
|
|
|
|
2025-04-12 15:14:28 +01:00
|
|
|
private function setStyle(InputInterface $input, OutputInterface $output): void
|
2025-04-08 23:00:21 +01:00
|
|
|
{
|
|
|
|
$this->style = new SymfonyStyle($input, $output);
|
|
|
|
}
|
|
|
|
}
|