[!!!][TASK] Remove all legacy CLI-related functionality
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Console / CommandApplication.php
1 <?php
2 namespace TYPO3\CMS\Core\Console;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use Symfony\Component\Console\Input\ArgvInput;
17 use TYPO3\CMS\Core\Core\ApplicationInterface;
18 use TYPO3\CMS\Core\Core\Bootstrap;
19
20 /**
21 * Entry point for the TYPO3 Command Line for Commands
22 * Does not run the RequestHandler as this already runs an Application inside an Application which
23 * is just way too much logic around simple CLI calls
24 */
25 class CommandApplication implements ApplicationInterface
26 {
27 /**
28 * @var Bootstrap
29 */
30 protected $bootstrap;
31
32 /**
33 * Number of subdirectories where the entry script is located, relative to PATH_site
34 * @var int
35 */
36 protected $entryPointLevel = 4;
37
38 /**
39 * All available request handlers that can deal with a CLI Request
40 * @var array
41 */
42 protected $availableRequestHandlers = [
43 \TYPO3\CMS\Core\Console\CommandRequestHandler::class,
44 ];
45
46 /**
47 * Constructor setting up legacy constants and register available Request Handlers
48 *
49 * @param \Composer\Autoload\ClassLoader $classLoader an instance of the class loader
50 */
51 public function __construct($classLoader)
52 {
53 $this->checkEnvironmentOrDie();
54 $this->defineLegacyConstants();
55 $this->bootstrap = Bootstrap::getInstance()
56 ->initializeClassLoader($classLoader)
57 ->setRequestType(TYPO3_REQUESTTYPE_CLI)
58 ->baseSetup($this->entryPointLevel);
59
60 foreach ($this->availableRequestHandlers as $requestHandler) {
61 $this->bootstrap->registerRequestHandlerImplementation($requestHandler);
62 }
63
64 $this->bootstrap->configure();
65 }
66
67 /**
68 * Run the Symfony Console application in this TYPO3 application
69 *
70 * @param callable $execute
71 */
72 public function run(callable $execute = null)
73 {
74 $this->bootstrap->handleRequest(new ArgvInput());
75
76 if ($execute !== null) {
77 call_user_func($execute);
78 }
79
80 $this->bootstrap->shutdown();
81 }
82
83 /**
84 * Define constants and variables
85 */
86 protected function defineLegacyConstants()
87 {
88 define('TYPO3_MODE', 'BE');
89 }
90
91 /**
92 * Check the script is called from a cli environment.
93 */
94 protected function checkEnvironmentOrDie()
95 {
96 if (php_sapi_name() !== 'cli') {
97 die('Not called from a command line interface (e.g. a shell or scheduler).' . LF);
98 }
99 }
100 }