2025-04-08 23:00:21 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Loom\Spinner\Command;
|
|
|
|
|
2025-04-13 13:11:35 +01:00
|
|
|
use Loom\Spinner\Classes\File\DockerComposeFileBuilder;
|
2025-04-13 20:07:48 +01:00
|
|
|
use Loom\Spinner\Classes\File\NginxDockerFileBuilder;
|
2025-04-13 12:59:44 +01:00
|
|
|
use Loom\Spinner\Classes\File\PHPDockerFileBuilder;
|
2025-04-13 10:43:30 +01:00
|
|
|
use Loom\Spinner\Classes\File\SpinnerFilePath;
|
2025-04-12 19:09:40 +01:00
|
|
|
use Loom\Spinner\Classes\OS\PortGenerator;
|
2025-04-08 23:00:21 +01:00
|
|
|
use Symfony\Component\Console\Attribute\AsCommand;
|
|
|
|
use Symfony\Component\Console\Command\Command;
|
2025-04-12 10:47:55 +01:00
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
2025-04-08 23:00:21 +01:00
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
2025-04-12 17:28:57 +01:00
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
2025-04-08 23:00:21 +01:00
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
2025-04-12 17:28:57 +01:00
|
|
|
#[AsCommand(name: 'spin:up', description: 'Spin up a new development environment')]
|
2025-04-08 23:00:21 +01:00
|
|
|
class SpinCommand extends AbstractSpinnerCommand
|
|
|
|
{
|
2025-04-12 19:09:40 +01:00
|
|
|
private PortGenerator $portGenerator;
|
|
|
|
private array $ports;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->portGenerator = new PortGenerator();
|
|
|
|
$this->ports = [
|
|
|
|
'php' => $this->portGenerator->generateRandomPort(),
|
2025-04-13 20:07:48 +01:00
|
|
|
'nginx' => $this->portGenerator->generateRandomPort(),
|
2025-04-12 19:09:40 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
2025-04-12 17:28:57 +01:00
|
|
|
/**
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2025-04-12 10:47:55 +01:00
|
|
|
protected function configure(): void
|
|
|
|
{
|
|
|
|
$this
|
|
|
|
->addArgument('name', InputArgument::REQUIRED, 'The name for your Docker container.')
|
2025-04-12 17:28:57 +01:00
|
|
|
->addArgument('path', InputArgument::REQUIRED, 'The absolute path to your projects root directory.')
|
2025-04-13 12:59:44 +01:00
|
|
|
->addOption(
|
|
|
|
'php',
|
|
|
|
null,
|
|
|
|
InputOption::VALUE_OPTIONAL,
|
|
|
|
'The PHP version to use (e.g., 8.0).'
|
|
|
|
)
|
|
|
|
->addOption(
|
2025-04-13 20:07:48 +01:00
|
|
|
'disable-node',
|
|
|
|
null,
|
|
|
|
InputOption::VALUE_NONE,
|
|
|
|
'Set this flag to disable Node.js for your environment.'
|
|
|
|
)
|
|
|
|
->addOption(
|
|
|
|
'disable-server',
|
2025-04-13 12:59:44 +01:00
|
|
|
null,
|
|
|
|
InputOption::VALUE_NONE,
|
2025-04-13 23:51:02 +01:00
|
|
|
'Set this flag to not include a web server for your environment.'
|
2025-04-13 12:59:44 +01:00
|
|
|
)
|
2025-04-13 23:33:55 +01:00
|
|
|
->addOption(
|
|
|
|
'disable-xdebug',
|
|
|
|
null,
|
|
|
|
InputOption::VALUE_NONE,
|
|
|
|
'Set this flag to disable XDebug for your environment.'
|
|
|
|
)
|
2025-04-13 12:59:44 +01:00
|
|
|
->addOption('node', null, InputOption::VALUE_OPTIONAL, 'The Node.js version to use (e.g. 20).');
|
2025-04-12 10:47:55 +01:00
|
|
|
}
|
|
|
|
|
2025-04-13 10:43:30 +01:00
|
|
|
/**
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2025-04-08 23:00:21 +01:00
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
|
|
{
|
2025-04-12 17:28:57 +01:00
|
|
|
if (parent::execute($input, $output)) {
|
2025-04-12 10:47:55 +01:00
|
|
|
return Command::FAILURE;
|
|
|
|
}
|
|
|
|
|
2025-04-13 10:43:30 +01:00
|
|
|
if ($this->projectDataExists()) {
|
2025-04-12 17:28:57 +01:00
|
|
|
return Command::SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->style->success("Spinning up a new development environment...");
|
2025-04-14 00:13:30 +01:00
|
|
|
|
2025-04-13 12:59:44 +01:00
|
|
|
$this->style->text('Creating project data...');
|
2025-04-12 19:09:40 +01:00
|
|
|
$this->createProjectData($input);
|
2025-04-14 00:13:30 +01:00
|
|
|
$this->style->success('Project data created.');
|
2025-04-12 19:09:40 +01:00
|
|
|
|
2025-04-14 00:13:30 +01:00
|
|
|
$this->style->text('Building Docker images...');
|
2025-04-13 20:07:48 +01:00
|
|
|
$command = $this->buildDockerComposeCommand(sprintf('-p %s up', $input->getArgument('name')));
|
|
|
|
|
|
|
|
passthru($command);
|
2025-04-12 19:09:40 +01:00
|
|
|
|
|
|
|
return Command::SUCCESS;
|
|
|
|
}
|
|
|
|
|
2025-04-13 10:43:30 +01:00
|
|
|
protected function projectDataExists(): bool
|
2025-04-12 19:09:40 +01:00
|
|
|
{
|
2025-04-13 12:59:44 +01:00
|
|
|
if ($this->config->getFilePaths()->get('projectData')->exists()) {
|
2025-04-14 02:52:39 +01:00
|
|
|
$this->style->warning('Project already exists. Skipping new build.');
|
2025-04-13 10:43:30 +01:00
|
|
|
|
2025-04-13 12:59:44 +01:00
|
|
|
return true;
|
2025-04-13 10:43:30 +01:00
|
|
|
}
|
2025-04-12 17:28:57 +01:00
|
|
|
|
2025-04-13 12:59:44 +01:00
|
|
|
return false;
|
2025-04-13 10:43:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
private function createProjectData(InputInterface $input): void
|
|
|
|
{
|
|
|
|
$this->createProjectDataDirectory();
|
2025-04-12 19:09:40 +01:00
|
|
|
$this->createEnvironmentFile($input);
|
2025-04-13 13:11:35 +01:00
|
|
|
$this->buildDockerComposeFile($input);
|
2025-04-13 20:07:48 +01:00
|
|
|
$this->buildDockerfiles($input);
|
2025-04-13 10:43:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
private function createProjectDataDirectory(): void
|
|
|
|
{
|
2025-04-13 12:59:44 +01:00
|
|
|
$projectData = $this->config->getFilePaths()->get('projectData');
|
2025-04-13 10:43:30 +01:00
|
|
|
|
|
|
|
if (!$projectData instanceof SpinnerFilePath) {
|
|
|
|
throw new \Exception('Invalid project data directory provided.');
|
|
|
|
}
|
|
|
|
|
|
|
|
mkdir($projectData->getProvidedPath(), 0777, true);
|
2025-04-12 19:09:40 +01:00
|
|
|
}
|
|
|
|
|
2025-04-13 10:43:30 +01:00
|
|
|
/**
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2025-04-12 19:09:40 +01:00
|
|
|
private function createEnvironmentFile(InputInterface $input): void
|
|
|
|
{
|
2025-04-13 12:59:44 +01:00
|
|
|
$projectEnv = $this->config->getFilePaths()->get('projectEnv');
|
2025-04-13 10:43:30 +01:00
|
|
|
|
|
|
|
if (!$projectEnv instanceof SpinnerFilePath) {
|
|
|
|
throw new \Exception('Invalid project environment file provided.');
|
|
|
|
}
|
|
|
|
|
2025-04-12 17:28:57 +01:00
|
|
|
file_put_contents(
|
2025-04-13 10:43:30 +01:00
|
|
|
$projectEnv->getProvidedPath(),
|
2025-04-12 17:28:57 +01:00
|
|
|
sprintf(
|
2025-04-13 12:59:44 +01:00
|
|
|
file_get_contents($this->config->getFilePaths()->get('envTemplate')->getAbsolutePath()),
|
|
|
|
$this->config->getFilePaths()->get('project')->getAbsolutePath(),
|
2025-04-12 17:28:57 +01:00
|
|
|
$input->getArgument('name'),
|
2025-04-13 13:35:39 +01:00
|
|
|
$this->config->getPhpVersion($input),
|
2025-04-12 19:09:40 +01:00
|
|
|
$this->getPort('php'),
|
2025-04-13 20:07:48 +01:00
|
|
|
$this->getPort('nginx'),
|
2025-04-12 17:28:57 +01:00
|
|
|
)
|
|
|
|
);
|
2025-04-12 19:09:40 +01:00
|
|
|
}
|
2025-04-12 17:28:57 +01:00
|
|
|
|
2025-04-13 10:43:30 +01:00
|
|
|
/**
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2025-04-13 13:11:35 +01:00
|
|
|
private function buildDockerComposeFile(InputInterface $input): void
|
|
|
|
{
|
|
|
|
$this->createProjectPhpFpmDirectory();
|
|
|
|
|
2025-04-13 20:07:48 +01:00
|
|
|
if ($this->config->isServerEnabled($input)) {
|
|
|
|
$this->createProjectNginxDirectory();
|
|
|
|
(new NginxDockerFileBuilder($this->config))->build($input)->save();
|
|
|
|
}
|
|
|
|
|
2025-04-13 13:11:35 +01:00
|
|
|
(new DockerComposeFileBuilder($this->config))->build($input)->save();
|
|
|
|
}
|
|
|
|
|
2025-04-13 20:07:48 +01:00
|
|
|
/**
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
private function buildDockerfiles(InputInterface $input): void
|
|
|
|
{
|
|
|
|
(new PHPDockerFileBuilder($this->config))->build($input)->save();
|
|
|
|
}
|
|
|
|
|
2025-04-13 13:11:35 +01:00
|
|
|
/**
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
private function createProjectPhpFpmDirectory(): void
|
2025-04-12 19:09:40 +01:00
|
|
|
{
|
2025-04-13 12:59:44 +01:00
|
|
|
$projectData = $this->config->getFilePaths()->get('projectData');
|
2025-04-13 10:43:30 +01:00
|
|
|
|
2025-04-13 13:11:35 +01:00
|
|
|
if (!$projectData instanceof SpinnerFilePath) {
|
2025-04-13 10:43:30 +01:00
|
|
|
throw new \Exception('Invalid project data directory provided.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file_exists($projectData->getProvidedPath() . '/php-fpm')) {
|
|
|
|
mkdir($projectData->getProvidedPath() . '/php-fpm', 0777, true);
|
2025-04-12 19:09:40 +01:00
|
|
|
}
|
2025-04-13 10:43:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2025-04-13 20:07:48 +01:00
|
|
|
private function createProjectNginxDirectory(): void
|
2025-04-13 10:43:30 +01:00
|
|
|
{
|
2025-04-13 20:07:48 +01:00
|
|
|
$projectData = $this->config->getFilePaths()->get('projectData');
|
|
|
|
|
|
|
|
if (!$projectData instanceof SpinnerFilePath) {
|
|
|
|
throw new \Exception('Invalid project data directory provided.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file_exists($projectData->getProvidedPath() . '/nginx')) {
|
|
|
|
mkdir($projectData->getProvidedPath() . '/nginx', 0777, true);
|
|
|
|
}
|
2025-04-12 19:09:40 +01:00
|
|
|
}
|
|
|
|
|
2025-04-13 13:35:39 +01:00
|
|
|
private function getPort(string $service): ?int
|
2025-04-12 19:09:40 +01:00
|
|
|
{
|
2025-04-13 13:35:39 +01:00
|
|
|
return $this->ports[$service];
|
2025-04-08 23:00:21 +01:00
|
|
|
}
|
|
|
|
}
|