[TASK] Instantiate Context through dependency injection
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Console / CommandRequestHandler.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Console;
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
18 use Symfony\Component\Console\Application;
19 use Symfony\Component\Console\Command\Command;
20 use Symfony\Component\Console\Input\InputInterface;
21 use Symfony\Component\Console\Output\ConsoleOutput;
22 use TYPO3\CMS\Core\Authentication\CommandLineUserAuthentication;
23 use TYPO3\CMS\Core\Core\Bootstrap;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26 /**
27 * Command Line Interface Request Handler dealing with registered commands.
28 */
29 class CommandRequestHandler implements RequestHandlerInterface
30 {
31 /**
32 * Instance of the symfony application
33 * @var Application
34 */
35 protected $application;
36
37 /**
38 * Constructor initializing the symfony application
39 */
40 public function __construct()
41 {
42 $this->application = new Application('TYPO3 CMS', TYPO3_version);
43 }
44
45 /**
46 * Handles any commandline request
47 *
48 * @param InputInterface $input
49 */
50 public function handleRequest(InputInterface $input)
51 {
52 $output = new ConsoleOutput();
53
54 Bootstrap::loadExtTables();
55 // create the BE_USER object (not logged in yet)
56 Bootstrap::initializeBackendUser(CommandLineUserAuthentication::class);
57 Bootstrap::initializeLanguageObject();
58 // Make sure output is not buffered, so command-line output and interaction can take place
59 ob_clean();
60
61 $this->populateAvailableCommands();
62
63 $exitCode = $this->application->run($input, $output);
64 exit($exitCode);
65 }
66
67 /**
68 * This request handler can handle any CLI request
69 *
70 * @param InputInterface $input
71 * @return bool Always TRUE
72 */
73 public function canHandleRequest(InputInterface $input): bool
74 {
75 return true;
76 }
77
78 /**
79 * Returns the priority - how eager the handler is to actually handle the request.
80 *
81 * @return int The priority of the request handler.
82 */
83 public function getPriority(): int
84 {
85 return 50;
86 }
87
88 /**
89 * Put all available commands inside the application
90 * @throws \TYPO3\CMS\Core\Console\CommandNameAlreadyInUseException
91 */
92 protected function populateAvailableCommands()
93 {
94 $commands = GeneralUtility::makeInstance(CommandRegistry::class);
95
96 foreach ($commands as $commandName => $command) {
97 /** @var Command $command */
98 $this->application->add($command);
99 }
100 }
101 }