-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathModule.php
237 lines (195 loc) · 7.32 KB
/
Module.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
namespace craft\cloud;
use Craft;
use craft\base\Event;
use craft\base\Model;
use craft\cloud\fs\AssetsFs;
use craft\cloud\fs\StorageFs;
use craft\cloud\fs\TmpFs;
use craft\cloud\twig\TwigExtension;
use craft\cloud\web\assets\uploader\UploaderAsset;
use craft\cloud\web\ResponseEventHandler;
use craft\console\Application as ConsoleApplication;
use craft\debug\Module as DebugModule;
use craft\elements\Asset;
use craft\events\DefineRulesEvent;
use craft\events\RegisterComponentTypesEvent;
use craft\events\RegisterTemplateRootsEvent;
use craft\fs\Temp;
use craft\helpers\App;
use craft\helpers\ConfigHelper;
use craft\imagetransforms\FallbackTransformer;
use craft\imagetransforms\ImageTransformer as CraftImageTransformer;
use craft\services\Fs as FsService;
use craft\services\ImageTransforms;
use craft\web\Application as WebApplication;
use craft\web\View;
use Illuminate\Support\Collection;
use yii\base\InvalidConfigException;
/**
* @property ?string $id When auto-bootstrapped as an extension, this can be `null`.
*/
class Module extends \yii\base\Module implements \yii\base\BootstrapInterface
{
private Config $_config;
/**
* @throws InvalidConfigException
* @param WebApplication|ConsoleApplication $app
* @inheritDoc
*/
public function bootstrap($app): void
{
$this->id = $this->id ?? 'cloud';
// Set instance early so our dependencies can use it
self::setInstance($this);
$this->controllerNamespace = $app->getRequest()->getIsConsoleRequest()
? 'craft\\cloud\\cli\\controllers'
: 'craft\\cloud\\controllers';
$this->registerGlobalEventHandlers();
$this->validateConfig();
// Required for controllers to be found
$app->setModule($this->id, $this);
$app->getView()->registerTwigExtension(new TwigExtension());
Craft::setAlias('@artifactBaseUrl', Helper::artifactUrl());
if (Helper::isCraftCloud()) {
$this->bootstrapCloud($app);
}
if ($this->getConfig()->useAssetCdn) {
$app->getImages()->supportedImageFormats = ImageTransformer::SUPPORTED_IMAGE_FORMATS;
/**
* Currently this is the only reasonable way to change the default transformer
*/
Craft::$container->set(
CraftImageTransformer::class,
ImageTransformer::class,
);
// TODO: this is to ensure PHP never transforms. Test this.
Craft::$container->set(
FallbackTransformer::class,
ImageTransformer::class,
);
if ($app->getRequest()->getIsCpRequest()) {
$app->getView()->registerAssetBundle(UploaderAsset::class);
}
}
}
public function getConfig(): Config
{
if (isset($this->_config)) {
return $this->_config;
}
$fileConfig = Craft::$app->getConfig()->getConfigFromFile($this->id);
/** @var Config $config */
$config = is_array($fileConfig)
? Craft::createObject(['class' => Config::class] + $fileConfig)
: $fileConfig;
$this->_config = Craft::configure($config, App::envConfig(Config::class, 'CRAFT_CLOUD_'));
return $this->_config;
}
protected function bootstrapCloud(ConsoleApplication|WebApplication $app): void
{
// Set Craft memory limit to just below PHP's limit
$this->setMemoryLimit(
ini_get('memory_limit'),
$app->getErrorHandler()->memoryReserveSize,
);
Craft::$container->set(
Temp::class,
TmpFs::class,
);
/**
* We have to use DI here (can't use setModule), as
* \craft\web\Application::debugBootstrap will be called after and override it.
*/
Craft::$container->set(
DebugModule::class,
[
'class' => DebugModule::class,
'fs' => Craft::createObject(StorageFs::class),
'dataPath' => 'debug',
],
);
$this->setComponents([
'staticCache' => StaticCache::class,
]);
$this->registerCloudEventHandlers();
if ($app instanceof WebApplication) {
Craft::setAlias('@web', $app->getRequest()->getHostInfo());
$app->getRequest()->secureHeaders = Collection::make($app->getRequest()->secureHeaders)
->reject(fn(string $header) => $header === 'X-Forwarded-Host')
->all();
// Important this gets called last so multi-value headers aren't prematurely joined
(new ResponseEventHandler())->handle();
}
}
protected function registerGlobalEventHandlers(): void
{
Event::on(
ImageTransforms::class,
ImageTransforms::EVENT_REGISTER_IMAGE_TRANSFORMERS,
static function(RegisterComponentTypesEvent $event) {
$event->types[] = ImageTransformer::class;
}
);
Event::on(
FsService::class,
FsService::EVENT_REGISTER_FILESYSTEM_TYPES,
static function(RegisterComponentTypesEvent $event) {
$event->types[] = AssetsFs::class;
}
);
Event::on(
View::class,
View::EVENT_REGISTER_CP_TEMPLATE_ROOTS,
function(RegisterTemplateRootsEvent $e) {
$e->roots[$this->id] = sprintf('%s/templates', $this->getBasePath());
}
);
}
protected function registerCloudEventHandlers(): void
{
$this->getStaticCache()->registerEventHandlers();
Event::on(
Asset::class,
Model::EVENT_DEFINE_RULES,
function(DefineRulesEvent $e) {
$e->rules = $this->removeAttributeFromRules($e->rules, 'tempFilePath');
}
);
}
protected function validateConfig(): void
{
$config = $this->getConfig();
if (!$config->validate()) {
$firstErrors = $config->getFirstErrors();
throw new InvalidConfigException(reset($firstErrors) ?: '');
}
}
public function getStaticCache(): StaticCache
{
return $this->get('staticCache');
}
private function removeAttributeFromRule(array $rule, string $attributeToRemove): array
{
$attributes = Collection::wrap($rule[0])
->reject(fn($attribute) => $attribute === $attributeToRemove);
// We may end up with a rule with an empty array of attributes.
// We still need to keep that rule around so any potential
// scenarios get defined from the 'on' key.
$rule[0] = $attributes->all();
return $rule;
}
private function removeAttributeFromRules(array $rules, string $attributeToRemove): array
{
return Collection::make($rules)
->map(fn($rule) => $this->removeAttributeFromRule($rule, $attributeToRemove))
->all();
}
private function setMemoryLimit(int|string $limit, int|string $offset = 0): int|float
{
$memoryLimit = ConfigHelper::sizeInBytes($limit) - ConfigHelper::sizeInBytes($offset);
Craft::$app->getConfig()->getGeneral()->phpMaxMemoryLimit((string) $memoryLimit);
Craft::info("phpMaxMemoryLimit set to $memoryLimit");
return $memoryLimit;
}
}