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\Core\Console;
19
20use Symfony\Component\Console\Application;
21use Symfony\Component\Console\Command\Command;
22use Symfony\Component\Console\Input\InputInterface;
23use Symfony\Component\Console\Output\ConsoleOutput;
24use TYPO3\CMS\Core\Authentication\CommandLineUserAuthentication;
25use TYPO3\CMS\Core\Core\Bootstrap;
26use TYPO3\CMS\Core\Core\Environment;
27use TYPO3\CMS\Core\Information\Typo3Version;
28use TYPO3\CMS\Core\Localization\LanguageService;
29use TYPO3\CMS\Core\Utility\GeneralUtility;
30
31/**
32 * Command Line Interface Request Handler dealing with registered commands.
33 *
34 * @deprecated since TYPO3 v10.1, will be removed in TYPO3 v11.0, as everything is handled by the CommandApplication directly.
35 */
36class CommandRequestHandler implements RequestHandlerInterface
37{
38    /**
39     * Instance of the symfony application
40     * @var Application
41     */
42    protected $application;
43
44    /**
45     * Constructor initializing the symfony application
46     */
47    public function __construct()
48    {
49        trigger_error('CommandRequestHandler will be removed in TYPO3 v11.0, as CLI is executed inside the CommandApplication directly.', E_USER_DEPRECATED);
50        $this->application = new Application('TYPO3 CMS', sprintf(
51            '%s (Application Context: <comment>%s</comment>)',
52            (new Typo3Version())->getVersion(),
53            Environment::getContext()
54        ));
55    }
56
57    /**
58     * Handles any commandline request
59     *
60     * @param InputInterface $input
61     */
62    public function handleRequest(InputInterface $input)
63    {
64        $output = new ConsoleOutput();
65
66        Bootstrap::loadExtTables();
67        // create the BE_USER object (not logged in yet)
68        Bootstrap::initializeBackendUser(CommandLineUserAuthentication::class);
69        $GLOBALS['LANG'] = LanguageService::createFromUserPreferences($GLOBALS['BE_USER']);
70        // Make sure output is not buffered, so command-line output and interaction can take place
71        ob_clean();
72
73        $this->populateAvailableCommands();
74
75        $exitCode = $this->application->run($input, $output);
76        exit($exitCode);
77    }
78
79    /**
80     * This request handler can handle any CLI request
81     *
82     * @param InputInterface $input
83     * @return bool Always TRUE
84     */
85    public function canHandleRequest(InputInterface $input): bool
86    {
87        return true;
88    }
89
90    /**
91     * Returns the priority - how eager the handler is to actually handle the request.
92     *
93     * @return int The priority of the request handler.
94     */
95    public function getPriority(): int
96    {
97        return 50;
98    }
99
100    /**
101     * Put all available commands inside the application
102     * @throws \TYPO3\CMS\Core\Console\CommandNameAlreadyInUseException
103     */
104    protected function populateAvailableCommands()
105    {
106        $commands = GeneralUtility::makeInstance(CommandRegistry::class);
107
108        foreach ($commands as $commandName => $command) {
109            /** @var Command $command */
110            $this->application->add($command);
111        }
112    }
113}
114