-
Notifications
You must be signed in to change notification settings - Fork 611
/
Application.php
194 lines (172 loc) · 5.77 KB
/
Application.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\console;
use Craft;
use craft\base\ApplicationTrait;
use craft\console\controllers\HelpController;
use craft\db\Query;
use craft\db\Table;
use craft\errors\MissingComponentException;
use craft\helpers\App;
use craft\helpers\Console;
use craft\queue\QueueLogBehavior;
use IntlDateFormatter;
use IntlException;
use Throwable;
use yii\base\Component;
use yii\base\InvalidConfigException;
use yii\base\Response as BaseResponse;
use yii\console\controllers\CacheController;
use yii\console\controllers\MigrateController;
use yii\console\Response;
/**
* Craft Console Application class
*
* An instance of the Console Application class is globally accessible to console requests in Craft via [[\Craft::$app|`Craft::$app`]].
*
* @property Request $request The request component
* @property User $user The user component
* @method Request getRequest() Returns the request component.
* @method Response getResponse() Returns the response component.
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0.0
*/
class Application extends \yii\console\Application
{
use ApplicationTrait;
/**
* @inheritdoc
*/
public function init(): void
{
$this->state = self::STATE_INIT;
$this->_preInit();
parent::init();
$this->_postInit();
}
/**
* @inheritdoc
*/
public function bootstrap(): void
{
// Ensure that the request component has been instantiated
if (!$this->has('request', true)) {
$this->getRequest();
}
parent::bootstrap();
}
/**
* @inheritdoc
*/
public function runAction($route, $params = []): int|BaseResponse|null
{
if ($this->_requireInfoTable($route, $params) && !$this->getIsInstalled(true)) {
// Is the connection valid at least?
if (!$this->getIsDbConnectionValid()) {
Console::outputWarning('Craft can’t connect to the database. Check your connection settings.');
} else {
$infoTable = $this->getDb()->getSchema()->getRawTableName(Table::INFO);
// Figure out the exception that is getting thrown
$e = null;
try {
(new Query())->from([Table::INFO])->where(['id' => 1])->one();
} catch (Throwable $e) {
$e = $e->getPrevious() ?? $e;
}
Console::outputWarning("Craft can’t fetch the `$infoTable` table row." . ($e ? PHP_EOL . 'Exception: ' . $e->getMessage() : ''), false);
}
}
return parent::runAction($route, $params);
}
/**
* @inheritdoc
*/
public function setTimeZone($value): void
{
parent::setTimeZone($value);
if ($value !== 'UTC') {
// Make sure that ICU supports this timezone
try {
/** @noinspection PhpExpressionResultUnusedInspection */
/** @phpstan-ignore-next-line */
new IntlDateFormatter($this->language, IntlDateFormatter::NONE, IntlDateFormatter::NONE);
} catch (IntlException) {
Craft::warning("Time zone “{$value}” does not appear to be supported by ICU: " . intl_get_error_message());
parent::setTimeZone('UTC');
}
}
}
/**
* @inheritdoc
*/
public function handleRequest($request)
{
// Disable read/write splitting for all console requests
$this->getDb()->enableReplicas = false;
return parent::handleRequest($request);
}
/**
* Returns the configuration of the built-in commands.
*
* @return array The configuration of the built-in commands.
*/
public function coreCommands(): array
{
return [
'help' => HelpController::class,
'migrate' => MigrateController::class,
'cache' => CacheController::class,
];
}
/**
* @throws MissingComponentException
*/
public function getSession()
{
throw new MissingComponentException('Session does not exist in a console request.');
}
/**
* Returns the user component.
*
* @return User
*/
public function getUser(): User
{
/** @noinspection PhpIncompatibleReturnTypeInspection */
return $this->get('user');
}
/**
* Returns the component instance with the specified ID.
*
* @param string $id component ID (e.g. `db`).
* @param bool $throwException whether to throw an exception if `$id` is not registered with the locator before.
* @return object|null the component of the specified ID. If `$throwException` is false and `$id`
* is not registered before, null will be returned.
* @throws InvalidConfigException if `$id` refers to a nonexistent component ID
* @see has()
* @see set()
*/
public function get($id, $throwException = true): ?object
{
// Is this the first time the queue component is requested?
$isFirstQueue = $id === 'queue' && !$this->has($id, true);
$component = parent::get($id, $throwException);
if ($isFirstQueue && $component instanceof Component) {
$component->attachBehavior('queueLogger', QueueLogBehavior::class);
}
return $component;
}
private function _requireInfoTable(string $route, array &$params): bool
{
$skipCheck = App::env('CRAFT_NO_DB') ?? false;
if ($skipCheck || isset($params['help'])) {
return false;
}
[$firstSeg] = explode('/', $route, 2);
return !in_array($firstSeg, ['install', 'setup', 'db', 'help'], true);
}
}