1<?php
2
3declare(strict_types=1);
4
5/*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18namespace TYPO3\CMS\Extensionmanager\Command;
19
20use Psr\EventDispatcher\EventDispatcherInterface;
21use Symfony\Component\Console\Command\Command;
22use Symfony\Component\Console\Input\InputInterface;
23use Symfony\Component\Console\Input\InputOption;
24use Symfony\Component\Console\Output\OutputInterface;
25use Symfony\Component\Console\Style\SymfonyStyle;
26use TYPO3\CMS\Core\Core\Bootstrap;
27use TYPO3\CMS\Core\Package\Event\PackagesMayHaveChangedEvent;
28use TYPO3\CMS\Core\Package\PackageManager;
29use TYPO3\CMS\Extensionmanager\Utility\InstallUtility;
30
31/**
32 * Command for setting up all extensions via CLI.
33 */
34class SetupExtensionsCommand extends Command
35{
36    /**
37     * @var EventDispatcherInterface
38     */
39    private $eventDispatcher;
40
41    /**
42     * @var InstallUtility
43     */
44    private $installUtility;
45
46    /**
47     * @var PackageManager
48     */
49    private PackageManager $packageManager;
50
51    public function __construct(
52        EventDispatcherInterface $eventDispatcher,
53        InstallUtility $installUtility,
54        PackageManager $packageManager
55    ) {
56        $this->eventDispatcher = $eventDispatcher;
57        $this->installUtility = $installUtility;
58        $this->packageManager = $packageManager;
59        parent::__construct();
60    }
61
62    /**
63     * Defines the allowed options for this command
64     */
65    protected function configure()
66    {
67        $this
68            ->setDescription('Set up extensions')
69            ->setHelp('The given extension keys must be recognized by TYPO3, or will be ignored otherwise')
70            ->addOption(
71                'extension',
72                '-e',
73                InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
74                'Only set up extensions with given key'
75            );
76    }
77
78    /**
79     * Sets up one or all extensions
80     *
81     * @inheritdoc
82     */
83    protected function execute(InputInterface $input, OutputInterface $output)
84    {
85        Bootstrap::initializeBackendAuthentication();
86        $this->eventDispatcher->dispatch(new PackagesMayHaveChangedEvent());
87
88        $io = new SymfonyStyle($input, $output);
89        $extensionKeys = $input->getOption('extension');
90        $extensionKeysToSetUp = array_keys($this->packageManager->getActivePackages());
91        if (!empty($extensionKeys)) {
92            $extensionKeysToSetUp = array_filter(
93                $extensionKeysToSetUp,
94                static function ($extKey) use ($extensionKeys) {
95                    return in_array($extKey, $extensionKeys, true);
96                }
97            );
98        }
99        $this->installUtility->updateDatabase();
100        foreach ($extensionKeysToSetUp as $extensionKey) {
101            $this->installUtility->processExtensionSetup($extensionKey);
102        }
103        if (empty($extensionKeysToSetUp)) {
104            $io->error('Given extensions "' . implode(', ', $extensionKeys) . '" not found in the system.');
105            return 1;
106        }
107        $io->success('Extension(s) "' . implode(', ', $extensionKeysToSetUp) . '" successfully set up.');
108
109        return 0;
110    }
111}
112