php-spark/src/Commands/DevCommand.php

78 lines
2.4 KiB
PHP

<?php
namespace Spark\Commands;
use Symfony\Component\Console\Attribute\AsCommand;
use Spark\Commands\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Helper\QuestionHelper;
use Spark\DevMode\DevMode;
#[AsCommand(name:'dev', description:'Enter development mode')]
class DevCommand extends Command
{
protected function configure()
{
$this->addOption("init", null, InputOption::VALUE_NONE, "Initialize a new devmode.json file");
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$env = $this->getEnvironment();
if ($input->getOption("init")) {
$conffile = $env->getConfigDirectory()."/devmode.json";
if (file_exists($conffile)) {
$output->writeln("<error>You have already created a devmode.json file</>");
return Command::FAILURE;
}
$this->initConfig($conffile, $input, $output);
return Command::SUCCESS;
}
$devmode = new DevMode($env);
$devmode->run();
return Command::SUCCESS;
}
private function initConfig(string $file, InputInterface $input, OutputInterface $output)
{
$qh = new QuestionHelper();
$titleQuestion = new Question("Window title []: ", null);
$title = $qh->ask($input, $output, $titleQuestion);
$commandQuestion = new Question(" Command []: ", null);
$startup = [];
$output->writeln("Startup commands. Enter an empty line to continue.");
while ($command = $qh->ask($input, $output, $commandQuestion)) {
$startup[] = $command;
}
$shutdown = [];
$output->writeln("Shutdown commands. Enter an empty line to continue.");
while ($command = $qh->ask($input, $output, $commandQuestion)) {
$shutdown[] = $command;
}
$json = [
'devmode' => [
]
];
if ($title)
$json['devmode']['title'] = $title;
$json['devmode']['startup'] = $startup;
$json['devmode']['shutdown'] = $shutdown;
$out = json_encode($json, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
file_put_contents($file, $out);
}
}