[FEATURE] Add Contexts for storing data access modes
[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\Context\Context;
18 use TYPO3\CMS\Core\Context\DateTimeAspect;
19 use TYPO3\CMS\Core\Context\UserAspect;
20 use TYPO3\CMS\Core\Context\VisibilityAspect;
21 use TYPO3\CMS\Core\Context\WorkspaceAspect;
22 use TYPO3\CMS\Core\Core\ApplicationInterface;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25 /**
26 * Entry point for the TYPO3 Command Line for Commands
27 * Does not run the RequestHandler as this already runs an Application inside an Application which
28 * is just way too much logic around simple CLI calls
29 */
30 class CommandApplication implements ApplicationInterface
31 {
32 /**
33 */
34 public function __construct()
35 {
36 $this->checkEnvironmentOrDie();
37 }
38
39 /**
40 * Run the Symfony Console application in this TYPO3 application
41 *
42 * @param callable $execute
43 */
44 public function run(callable $execute = null)
45 {
46 $this->initializeContext();
47 $handler = GeneralUtility::makeInstance(CommandRequestHandler::class);
48 $handler->handleRequest(new ArgvInput());
49
50 if ($execute !== null) {
51 call_user_func($execute);
52 }
53 }
54
55 /**
56 * Check the script is called from a cli environment.
57 */
58 protected function checkEnvironmentOrDie()
59 {
60 if (php_sapi_name() !== 'cli') {
61 die('Not called from a command line interface (e.g. a shell or scheduler).' . LF);
62 }
63 }
64
65 /**
66 * Initializes the Context used for accessing data and finding out the current state of the application
67 * Will be moved to a DI-like concept once introduced, for now, this is a singleton
68 */
69 protected function initializeContext()
70 {
71 GeneralUtility::makeInstance(Context::class, [
72 'date' => new DateTimeAspect(new \DateTimeImmutable('@' . $GLOBALS['EXEC_TIME'])),
73 'visibility' => new VisibilityAspect(true, true),
74 'workspace' => new WorkspaceAspect(0),
75 'backend.user' => new UserAspect(null),
76 ]);
77 }
78 }