|
4 | 4 |
|
5 | 5 | namespace App\Command; |
6 | 6 |
|
7 | | -class UpdateCommand |
| 7 | +use App\Service\TemplateService; |
| 8 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 9 | +use Symfony\Component\Console\Command\Command; |
| 10 | +use Symfony\Component\Console\Input\ArrayInput; |
| 11 | +use Symfony\Component\Console\Input\InputInterface; |
| 12 | +use Symfony\Component\Console\Output\OutputInterface; |
| 13 | +use Symfony\Component\Console\Question\ConfirmationQuestion; |
| 14 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 15 | + |
| 16 | +#[AsCommand( |
| 17 | + name: 'app:update', |
| 18 | + description: 'Run required updates.', |
| 19 | +)] |
| 20 | +class UpdateCommand extends Command |
8 | 21 | { |
9 | | - // TODO: Test that migrations have been run. |
10 | | - // TODO: Run test of status for templates. No templates = clean install. Install all? |
11 | | - // TODO: Update existing templates. |
| 22 | + private TemplateService $templateService; |
| 23 | + |
| 24 | + public function __construct(TemplateService $templateService, ?string $name = null) |
| 25 | + { |
| 26 | + parent::__construct($name); |
| 27 | + $this->templateService = $templateService; |
| 28 | + } |
| 29 | + |
| 30 | + final protected function execute(InputInterface $input, OutputInterface $output): int |
| 31 | + { |
| 32 | + $io = new SymfonyStyle($input, $output); |
| 33 | + $isInteractive = $input->isInteractive(); |
| 34 | + |
| 35 | + $application = $this->getApplication(); |
| 36 | + |
| 37 | + if (null === $application) { |
| 38 | + $io->error('Application not initialized.'); |
| 39 | + |
| 40 | + return Command::FAILURE; |
| 41 | + } |
| 42 | + |
| 43 | + $command = new ArrayInput([ |
| 44 | + 'command' => 'doctrine:migrations:migrate', |
| 45 | + ]); |
| 46 | + $command->setInteractive($isInteractive); |
| 47 | + $result = $application->doRun($command, $output); |
| 48 | + |
| 49 | + if (0 !== $result) { |
| 50 | + $io->info('Update aborted. Migrations need to run for the system to work. Run doctrine:migrations:migrate or rerun app:update to migrate.'); |
| 51 | + |
| 52 | + return Command::FAILURE; |
| 53 | + } |
| 54 | + |
| 55 | + $allTemplates = $this->templateService->getAllTemplates(); |
| 56 | + $installedTemplates = array_filter($allTemplates, fn ($entry): bool => $entry->installed); |
| 57 | + |
| 58 | + // If no installed templates, we assume that this is a new installation and offer to install all templates. |
| 59 | + if ($isInteractive && 0 === count($installedTemplates)) { |
| 60 | + $question = new ConfirmationQuestion('No templates are installed. Install all '.count($allTemplates).'?'); |
| 61 | + $installAll = $io->askQuestion($question); |
| 62 | + |
| 63 | + if ('yes' === $installAll) { |
| 64 | + $io->info('Installing all templates...'); |
| 65 | + $command = new ArrayInput([ |
| 66 | + 'command' => 'app:templates:install', |
| 67 | + '--all' => true, |
| 68 | + ]); |
| 69 | + $application->doRun($command, $output); |
| 70 | + } |
| 71 | + } else { |
| 72 | + $io->info('Updating existing template...'); |
| 73 | + $command = new ArrayInput([ |
| 74 | + 'command' => 'app:templates:update', |
| 75 | + ]); |
| 76 | + $application->doRun($command, $output); |
| 77 | + } |
| 78 | + |
| 79 | + return Command::SUCCESS; |
| 80 | + } |
12 | 81 | } |
0 commit comments