[TASK] Directly wire Application and RequestHandler
[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 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Entry point for the TYPO3 Command Line for Commands
23 * Does not run the RequestHandler as this already runs an Application inside an Application which
24 * is just way too much logic around simple CLI calls
25 */
26 class CommandApplication implements ApplicationInterface
27 {
28 /**
29 * @var Bootstrap
30 */
31 protected $bootstrap;
32
33 /**
34 * Number of subdirectories where the entry script is located, relative to PATH_site
35 * @var int
36 */
37 protected $entryPointLevel = 4;
38
39 /**
40 * Constructor setting up legacy constants and register available Request Handlers
41 *
42 * @param \Composer\Autoload\ClassLoader $classLoader an instance of the class loader
43 */
44 public function __construct($classLoader)
45 {
46 $this->checkEnvironmentOrDie();
47 $this->defineLegacyConstants();
48 $this->bootstrap = Bootstrap::getInstance()
49 ->initializeClassLoader($classLoader)
50 ->setRequestType(TYPO3_REQUESTTYPE_CLI)
51 ->baseSetup($this->entryPointLevel);
52
53 $this->bootstrap->configure();
54 }
55
56 /**
57 * Run the Symfony Console application in this TYPO3 application
58 *
59 * @param callable $execute
60 */
61 public function run(callable $execute = null)
62 {
63 $handler = GeneralUtility::makeInstance(CommandRequestHandler::class, $this->bootstrap);
64 $handler->handleRequest(new ArgvInput());
65
66 if ($execute !== null) {
67 call_user_func($execute);
68 }
69 }
70
71 /**
72 * Define constants and variables
73 */
74 protected function defineLegacyConstants()
75 {
76 define('TYPO3_MODE', 'BE');
77 }
78
79 /**
80 * Check the script is called from a cli environment.
81 */
82 protected function checkEnvironmentOrDie()
83 {
84 if (php_sapi_name() !== 'cli') {
85 die('Not called from a command line interface (e.g. a shell or scheduler).' . LF);
86 }
87 }
88 }