dbmanager/src/Console/CreateDatabaseCommand.php

42 lines
1.1 KiB
PHP

<?php
namespace App\Console;
use App\Service\DatabaseService;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
#[AsCommand('app:db:create')]
class CreateDatabaseCommand extends Command
{
public function __construct(
private readonly DatabaseService $db,
)
{
parent::__construct();
}
protected function configure(): void
{
$this->addArgument('name', InputArgument::OPTIONAL, 'Database name');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$question = $this->getHelper('question');
if (!$name = $input->getArgument('name')) {
$name = $question->ask($input, $output, new Question('Database name: '));
}
$this->db->createDatabase($name);
$output->writeln(sprintf('Database "%s" successfully created', $name));
return Command::SUCCESS;
}
}