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;
|
2025-04-12 15:14:28 +01:00
|
|
|
protected FilePathCollection $filePaths;
|
|
|
|
protected string $rootDirectory;
|
2025-04-13 10:43:30 +01:00
|
|
|
protected SymfonyStyle $style;
|
|
|
|
protected System $system;
|
2025-04-12 10:47:55 +01:00
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
2025-04-12 15:14:28 +01:00
|
|
|
$this->rootDirectory = dirname(__DIR__, 2);
|
2025-04-12 10:47:55 +01:00
|
|
|
$this->system = new System();
|
2025-04-12 15:14:28 +01:00
|
|
|
$this->setFilePaths();
|
2025-04-13 10:43:30 +01:00
|
|
|
$this->config = new Config($this->filePaths);
|
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
|
|
|
/**
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
protected function getDefaultConfig()
|
|
|
|
{
|
|
|
|
if (!$this->filePaths->get('defaultSpinnerConfig')?->exists()) {
|
|
|
|
throw new \Exception('Default spinner configuration file not found.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return Yaml::parseFile($this->filePaths->get('defaultSpinnerConfig')->getAbsolutePath())['options'] ?? null;
|
|
|
|
}
|
|
|
|
|
|
|
|
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',
|
|
|
|
$this->filePaths->get('projectData')->getAbsolutePath(),
|
|
|
|
$this->filePaths->get('projectEnv')->getAbsolutePath(),
|
2025-04-12 17:28:57 +01:00
|
|
|
$command,
|
|
|
|
$daemon ? ' -d' : ''
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2025-04-13 10:43:30 +01:00
|
|
|
protected function getEnvironmentOption(string $service, string $option): mixed
|
2025-04-12 17:28:57 +01:00
|
|
|
{
|
2025-04-13 10:43:30 +01:00
|
|
|
if ($this->filePaths->get('projectCustomConfig')?->exists()) {
|
|
|
|
$config = Yaml::parseFile(
|
|
|
|
$this->filePaths->get('projectCustomConfig')->getAbsolutePath()
|
|
|
|
)['options']['environment'] ?? null;
|
|
|
|
|
|
|
|
if ($config) {
|
|
|
|
if (isset($config[$service][$option])) {
|
|
|
|
return $config[$service][$option];
|
|
|
|
}
|
|
|
|
}
|
2025-04-12 17:28:57 +01:00
|
|
|
}
|
|
|
|
|
2025-04-13 10:43:30 +01:00
|
|
|
return $this->getDefaultConfig()['environment'][$service][$option] ?? null;
|
|
|
|
}
|
2025-04-12 17:28:57 +01:00
|
|
|
|
2025-04-13 10:43:30 +01:00
|
|
|
/**
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
protected function isNodeEnabled(InputInterface $input): bool
|
|
|
|
{
|
|
|
|
if ($input->getOption('node-disabled')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getEnvironmentOption('node', 'enabled');
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->filePaths->add($projectDirectory, 'project');
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function validateNameArgument(InputInterface $input): void
|
|
|
|
{
|
|
|
|
if ($input->hasArgument('name')) {
|
|
|
|
$this->filePaths->add(
|
|
|
|
new SpinnerFilePath(sprintf('data/environments/%s', $input->getArgument('name'))),
|
|
|
|
'projectData'
|
|
|
|
);
|
|
|
|
$this->filePaths->add(
|
|
|
|
new SpinnerFilePath(sprintf('data/environments/%s/.env', $input->getArgument('name'))),
|
|
|
|
'projectEnv'
|
|
|
|
);
|
|
|
|
$this->filePaths->add(
|
|
|
|
new SpinnerFilePath(sprintf('data/environments/%s/docker-compose.yml', $input->getArgument('name'))),
|
|
|
|
'projectDockerCompose'
|
|
|
|
);
|
|
|
|
$this->filePaths->add(
|
|
|
|
new FilePath($this->filePaths->get('project')->getAbsolutePath() . DIRECTORY_SEPARATOR . 'spinner.yaml'),
|
|
|
|
'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);
|
|
|
|
}
|
2025-04-12 15:14:28 +01:00
|
|
|
|
|
|
|
private function setFilePaths(): void
|
|
|
|
{
|
|
|
|
$this->filePaths = new FilePathCollection([
|
|
|
|
'config' => new SpinnerFilePath('config'),
|
2025-04-12 17:28:57 +01:00
|
|
|
'defaultSpinnerConfig' => new SpinnerFilePath('config/spinner.yaml'),
|
|
|
|
'envTemplate' => new SpinnerFilePath('config/.template.env'),
|
|
|
|
'data' => new SpinnerFilePath('data'),
|
2025-04-12 19:09:40 +01:00
|
|
|
'phpYamlTemplate' => new SpinnerFilePath('config/php.yaml'),
|
|
|
|
'phpFpmDataDirectory' => new SpinnerFilePath('config/php-fpm'),
|
2025-04-13 10:43:30 +01:00
|
|
|
'phpFpmDockerfileTemplate' => new SpinnerFilePath('config/php-fpm/Dockerfile'),
|
|
|
|
'nodeDockerfileTemplate' => new SpinnerFilePath('config/php-fpm/Node.Dockerfile'),
|
2025-04-12 15:14:28 +01:00
|
|
|
]);
|
|
|
|
}
|
2025-04-08 23:00:21 +01:00
|
|
|
}
|