[FEATURE] Add symfony dependency injection for core and extbase
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Command / UpgradeWizardListCommand.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Install\Command;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use Symfony\Component\Console\Command\Command;
20 use Symfony\Component\Console\Input\InputInterface;
21 use Symfony\Component\Console\Input\InputOption;
22 use Symfony\Component\Console\Output\OutputInterface;
23 use Symfony\Component\Console\Style\SymfonyStyle;
24 use TYPO3\CMS\Core\Authentication\CommandLineUserAuthentication;
25 use TYPO3\CMS\Core\Core\Bootstrap;
26 use TYPO3\CMS\Core\Utility\GeneralUtility;
27 use TYPO3\CMS\Install\Service\LateBootService;
28 use TYPO3\CMS\Install\Service\UpgradeWizardsService;
29 use TYPO3\CMS\Install\Updates\ChattyInterface;
30 use TYPO3\CMS\Install\Updates\UpgradeWizardInterface;
31
32 /**
33 * Upgrade wizard command for listing wizards
34 *
35 * @internal
36 */
37 class UpgradeWizardListCommand extends Command
38 {
39 /**
40 * @var UpgradeWizardsService
41 */
42 private $upgradeWizardsService;
43
44 /**
45 * @var OutputInterface|\Symfony\Component\Console\Style\StyleInterface
46 */
47 private $output;
48
49 /**
50 * @var InputInterface
51 */
52 private $input;
53
54 /**
55 * Bootstrap running of upgradeWizards
56 */
57 protected function bootstrap(): void
58 {
59 GeneralUtility::makeInstance(LateBootService::class)->loadExtLocalconfDatabaseAndExtTables();
60 Bootstrap::initializeBackendUser(CommandLineUserAuthentication::class);
61 Bootstrap::initializeBackendAuthentication();
62 }
63
64 /**
65 * Configure the command by defining the name, options and arguments
66 */
67 protected function configure()
68 {
69 $this->setDescription('List available upgrade wizards.')
70 ->addOption(
71 'all',
72 'a',
73 InputOption::VALUE_NONE,
74 'Include wizards already done.'
75 );
76 }
77
78 /**
79 * List available upgrade wizards. If -all is given, already done wizards are listed, too.
80 *
81 * @param InputInterface $input
82 * @param \Symfony\Component\Console\Output\OutputInterface $output
83 * @return int
84 */
85 protected function execute(InputInterface $input, OutputInterface $output)
86 {
87 $this->output = new SymfonyStyle($input, $output);
88 $this->input = $input;
89 $this->bootstrap();
90 $this->upgradeWizardsService = new UpgradeWizardsService();
91
92 $result = 0;
93 $wizards = [];
94 $all = $input->getOption('all');
95 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update'] as $identifier => $wizardToExecute) {
96 $upgradeWizard = $this->getWizard($wizardToExecute, $identifier, (bool)$all);
97 if ($upgradeWizard !== null) {
98 $wizardInfo = [
99 'identifier' => $upgradeWizard->getIdentifier(),
100 'title' => $upgradeWizard->getTitle(),
101 'description' => wordwrap($upgradeWizard->getDescription()),
102 ];
103 if ($all === true) {
104 $wizardInfo['status'] = $this->upgradeWizardsService->isWizardDone($identifier) ? 'DONE' : 'AVAILABLE';
105 }
106 $wizards[] = $wizardInfo;
107 }
108 }
109 if (empty($wizards)) {
110 $this->output->success('No wizards available.');
111 } else {
112 if ($all === true) {
113 $this->output->table(['Identifier', 'Title', 'Description', 'Status'], $wizards);
114 } else {
115 $this->output->table(['Identifier', 'Title', 'Description'], $wizards);
116 }
117 }
118 return $result;
119 }
120
121 /**
122 * Get Wizard instance by class name and identifier
123 * Returns null if wizard is already done
124 *
125 * @param string $className
126 * @param string $identifier
127 * @param bool $all
128 * @return \TYPO3\CMS\Install\Updates\UpgradeWizardInterface|null
129 */
130 protected function getWizard(string $className, string $identifier, $all = false): ?UpgradeWizardInterface
131 {
132 // already done
133 if (!$all && $this->upgradeWizardsService->isWizardDone($identifier)) {
134 return null;
135 }
136
137 $wizardInstance = GeneralUtility::makeInstance($className);
138 if ($wizardInstance instanceof ChattyInterface) {
139 $wizardInstance->setOutput($this->output);
140 }
141
142 if (!($wizardInstance instanceof UpgradeWizardInterface)) {
143 return null;
144 }
145
146 return !$all ? $wizardInstance->updateNecessary() ? $wizardInstance : null : $wizardInstance;
147 }
148 }