[FEATURE] Introduce native support for Symfony Console
[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 * @var string
34 */
35 protected $entryPointPath = 'typo3/sysext/core/bin/';
36
37 /**
38 * All available request handlers that can deal with a CLI Request
39 * @var array
40 */
41 protected $availableRequestHandlers = array(
42 \TYPO3\CMS\Core\Console\CommandRequestHandler::class,
43 \TYPO3\CMS\Backend\Console\CliRequestHandler::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_BE | TYPO3_REQUESTTYPE_CLI)
58 ->baseSetup($this->entryPointPath);
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 * @return void
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 * @return void
96 */
97 protected function checkEnvironmentOrDie()
98 {
99 if (php_sapi_name() !== 'cli') {
100 die('Not called from a command line interface (e.g. a shell or scheduler).' . LF);
101 }
102 }
103 }