1dd9c9e459d1584c3476e598e24c114493bbea2f
[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 \TYPO3\CMS\Backend\Console\CliRequestHandler::class
45 ];
46
47 /**
48 * Constructor setting up legacy constants and register available Request Handlers
49 *
50 * @param \Composer\Autoload\ClassLoader $classLoader an instance of the class loader
51 */
52 public function __construct($classLoader)
53 {
54 $this->checkEnvironmentOrDie();
55 $this->defineLegacyConstants();
56 $this->bootstrap = Bootstrap::getInstance()
57 ->initializeClassLoader($classLoader)
58 ->setRequestType(TYPO3_REQUESTTYPE_CLI)
59 ->baseSetup($this->entryPointLevel);
60
61 foreach ($this->availableRequestHandlers as $requestHandler) {
62 $this->bootstrap->registerRequestHandlerImplementation($requestHandler);
63 }
64
65 $this->bootstrap->configure();
66 }
67
68 /**
69 * Run the Symfony Console application in this TYPO3 application
70 *
71 * @param callable $execute
72 */
73 public function run(callable $execute = null)
74 {
75 $this->bootstrap->handleRequest(new ArgvInput());
76
77 if ($execute !== null) {
78 call_user_func($execute);
79 }
80
81 $this->bootstrap->shutdown();
82 }
83
84 /**
85 * Define constants and variables
86 */
87 protected function defineLegacyConstants()
88 {
89 define('TYPO3_MODE', 'BE');
90 }
91
92 /**
93 * Check the script is called from a cli environment.
94 */
95 protected function checkEnvironmentOrDie()
96 {
97 if (php_sapi_name() !== 'cli') {
98 die('Not called from a command line interface (e.g. a shell or scheduler).' . LF);
99 }
100 }
101 }