‪TYPO3CMS  10.4
CommandApplication.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Symfony\Component\Console\Application;
19 use Symfony\Component\Console\Command\Command;
20 use Symfony\Component\Console\Input\ArgvInput;
21 use Symfony\Component\Console\Output\ConsoleOutput;
33 
39 {
43  protected ‪$context;
44 
48  protected ‪$commandRegistry;
49 
54  protected ‪$application;
55 
57  {
58  $this->context = ‪$context;
59  $this->commandRegistry = ‪$commandRegistry;
60  $this->‪checkEnvironmentOrDie();
61  $this->application = new Application('TYPO3 CMS', sprintf(
62  '%s (Application Context: <comment>%s</comment>)',
63  (new ‪Typo3Version())->getVersion(),
65  ));
66  $this->application->setAutoExit(false);
67  $this->application->setCommandLoader(‪$commandRegistry);
68  }
69 
75  public function ‪run(callable $execute = null)
76  {
77  $this->‪initializeContext();
78 
79  $input = new ArgvInput();
80  ‪$output = new ConsoleOutput();
81 
83  // create the BE_USER object (not logged in yet)
84  ‪Bootstrap::initializeBackendUser(CommandLineUserAuthentication::class);
86  // Make sure output is not buffered, so command-line output and interaction can take place
87  ob_clean();
88 
90 
91  $exitCode = $this->application->run($input, ‪$output);
92 
93  if ($execute !== null) {
94  call_user_func($execute);
95  }
96 
97  exit($exitCode);
98  }
99 
103  protected function ‪checkEnvironmentOrDie(): void
104  {
105  if (PHP_SAPI !== 'cli') {
106  die('Not called from a command line interface (e.g. a shell or scheduler).' . LF);
107  }
108  }
109 
113  protected function ‪initializeContext(): void
114  {
115  $this->context->setAspect('date', new ‪DateTimeAspect(new \DateTimeImmutable('@' . ‪$GLOBALS['EXEC_TIME'])));
116  $this->context->setAspect('visibility', new ‪VisibilityAspect(true, true));
117  $this->context->setAspect('workspace', new ‪WorkspaceAspect(0));
118  $this->context->setAspect('backend.user', new ‪UserAspect(null));
119  }
120 
126  protected function ‪populateAvailableCommands(): void
127  {
128  foreach ($this->commandRegistry->getLegacyCommands() as $commandName => $command) {
130  $this->application->add($command);
131  }
132  }
133 }
‪TYPO3\CMS\Core\Context\VisibilityAspect
Definition: VisibilityAspect.php:31
‪TYPO3\CMS\Core\Context\WorkspaceAspect
Definition: WorkspaceAspect.php:31
‪TYPO3\CMS\Core\Information\Typo3Version
Definition: Typo3Version.php:21
‪TYPO3\CMS\Core\Console\CommandApplication\__construct
‪__construct(Context $context, CommandRegistry $commandRegistry)
Definition: CommandApplication.php:53
‪TYPO3\CMS\Core\Console
Definition: CommandApplication.php:16
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Core\Console\CommandApplication
Definition: CommandApplication.php:39
‪TYPO3\CMS\Core\Console\CommandRegistry
Definition: CommandRegistry.php:32
‪TYPO3\CMS\Core\Core\Environment\getContext
‪static ApplicationContext getContext()
Definition: Environment.php:133
‪TYPO3\CMS\Core\Localization\LanguageService\createFromUserPreferences
‪static createFromUserPreferences(?AbstractUserAuthentication $user)
Definition: LanguageService.php:435
‪TYPO3\CMS\Core\Console\CommandApplication\run
‪run(callable $execute=null)
Definition: CommandApplication.php:72
‪TYPO3\CMS\Core\Core\ApplicationInterface
Definition: ApplicationInterface.php:24
‪TYPO3\CMS\Core\Console\CommandApplication\checkEnvironmentOrDie
‪checkEnvironmentOrDie()
Definition: CommandApplication.php:100
‪TYPO3\CMS\Core\Core\Bootstrap\initializeBackendUser
‪static initializeBackendUser($className=BackendUserAuthentication::class)
Definition: Bootstrap.php:591
‪TYPO3\CMS\Core\Console\CommandApplication\$application
‪Application $application
Definition: CommandApplication.php:51
‪TYPO3\CMS\Core\Console\CommandApplication\$commandRegistry
‪CommandRegistry $commandRegistry
Definition: CommandApplication.php:46
‪$output
‪$output
Definition: annotationChecker.php:119
‪TYPO3\CMS\Core\Console\CommandApplication\$context
‪Context $context
Definition: CommandApplication.php:42
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Core\Core\Bootstrap
Definition: Bootstrap.php:66
‪TYPO3\CMS\Core\Core\Bootstrap\loadExtTables
‪static loadExtTables(bool $allowCaching=true)
Definition: Bootstrap.php:546
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Core\Console\CommandApplication\populateAvailableCommands
‪populateAvailableCommands()
Definition: CommandApplication.php:123
‪TYPO3\CMS\Core\Console\CommandApplication\initializeContext
‪initializeContext()
Definition: CommandApplication.php:110
‪TYPO3\CMS\Core\Context\DateTimeAspect
Definition: DateTimeAspect.php:35
‪TYPO3\CMS\Core\Context\UserAspect
Definition: UserAspect.php:38
‪TYPO3\CMS\Core\Authentication\CommandLineUserAuthentication
Definition: CommandLineUserAuthentication.php:30