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