Completed
Push — master ( 4bf7f4...98cf5d )
by
unknown
22:36
created

CommandApplication::populateAvailableCommands()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 5
rs 10
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
16
namespace TYPO3\CMS\Core\Console;
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;
22
use TYPO3\CMS\Core\Authentication\CommandLineUserAuthentication;
23
use TYPO3\CMS\Core\Context\Context;
24
use TYPO3\CMS\Core\Context\DateTimeAspect;
25
use TYPO3\CMS\Core\Context\UserAspect;
26
use TYPO3\CMS\Core\Context\VisibilityAspect;
27
use TYPO3\CMS\Core\Context\WorkspaceAspect;
28
use TYPO3\CMS\Core\Core\ApplicationInterface;
29
use TYPO3\CMS\Core\Core\Bootstrap;
30
use TYPO3\CMS\Core\Core\Environment;
31
use TYPO3\CMS\Core\Information\Typo3Version;
32
use TYPO3\CMS\Core\Localization\LanguageService;
33
34
/**
35
 * Entry point for the TYPO3 Command Line for Commands
36
 * In addition to a simple Symfony Command, this also sets up a CLI user
37
 */
38
class CommandApplication implements ApplicationInterface
39
{
40
    /**
41
     * @var Context
42
     */
43
    protected $context;
44
45
    /**
46
     * @var CommandRegistry
47
     */
48
    protected $commandRegistry;
49
50
    /**
51
     * Instance of the symfony application
52
     * @var Application
53
     */
54
    protected $application;
55
56
    public function __construct(Context $context, CommandRegistry $commandRegistry)
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(),
64
            Environment::getContext()
65
        ));
66
        $this->application->setAutoExit(false);
67
        $this->application->setCommandLoader($commandRegistry);
68
    }
69
70
    /**
71
     * Run the Symfony Console application in this TYPO3 application
72
     *
73
     * @param callable $execute
74
     */
75
    public function run(callable $execute = null)
76
    {
77
        $this->initializeContext();
78
79
        $input = new ArgvInput();
80
        $output = new ConsoleOutput();
81
82
        Bootstrap::loadExtTables();
83
        // create the BE_USER object (not logged in yet)
84
        Bootstrap::initializeBackendUser(CommandLineUserAuthentication::class);
85
        $GLOBALS['LANG'] = LanguageService::createFromUserPreferences($GLOBALS['BE_USER']);
86
        // Make sure output is not buffered, so command-line output and interaction can take place
87
        ob_clean();
88
89
        $exitCode = $this->application->run($input, $output);
90
91
        if ($execute !== null) {
92
            call_user_func($execute);
93
        }
94
95
        exit($exitCode);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
96
    }
97
98
    /**
99
     * Check the script is called from a cli environment.
100
     */
101
    protected function checkEnvironmentOrDie(): void
102
    {
103
        if (PHP_SAPI !== 'cli') {
104
            die('Not called from a command line interface (e.g. a shell or scheduler).' . LF);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
105
        }
106
    }
107
108
    /**
109
     * Initializes the Context used for accessing data and finding out the current state of the application
110
     */
111
    protected function initializeContext(): void
112
    {
113
        $this->context->setAspect('date', new DateTimeAspect(new \DateTimeImmutable('@' . $GLOBALS['EXEC_TIME'])));
114
        $this->context->setAspect('visibility', new VisibilityAspect(true, true));
115
        $this->context->setAspect('workspace', new WorkspaceAspect(0));
116
        $this->context->setAspect('backend.user', new UserAspect(null));
117
    }
118
}
119