forked from vanilla/vanilla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.php
408 lines (327 loc) · 11.6 KB
/
bootstrap.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<?php
use Garden\Container\Container;
use Garden\Container\Reference;
use Vanilla\Addon;
use Vanilla\InjectableInterface;
if (!defined('APPLICATION')) exit();
/**
* Bootstrap.
*
* @copyright 2009-2018 Vanilla Forums Inc.
* @license GPL-2.0-only
* @package Core
* @since 2.0
*/
// Guard against broken cache files.
if (!class_exists('Gdn')) {
// Throwing an exception here would result in a white screen for the user.
// This error usually indicates the .ini files in /cache are out of date and should be deleted.
exit("Class Gdn not found.");
}
// Set up the dependency injection container.
$dic = new Container();
Gdn::setContainer($dic);
$dic->setInstance('Garden\Container\Container', $dic)
->rule('Interop\Container\ContainerInterface')
->setAliasOf('Garden\Container\Container')
->rule(InjectableInterface::class)
->addCall('setDependencies')
// Cache
->rule('Gdn_Cache')
->setShared(true)
->setFactory(['Gdn_Cache', 'initialize'])
->addAlias('Cache')
// Configuration
->rule('Gdn_Configuration')
->setShared(true)
->addAlias('Config')
// AddonManager
->rule(Vanilla\AddonManager::class)
->setShared(true)
->setConstructorArgs([
[
Addon::TYPE_ADDON => ['/applications', '/plugins'],
Addon::TYPE_THEME => '/themes',
Addon::TYPE_LOCALE => '/locales'
],
PATH_CACHE
])
->addAlias('AddonManager')
->addCall('registerAutoloader')
// ApplicationManager
->rule('Gdn_ApplicationManager')
->setShared(true)
->addAlias('ApplicationManager')
->rule(Garden\Web\Cookie::class)
->setShared(true)
->addAlias('Cookie')
// PluginManager
->rule('Gdn_PluginManager')
->setShared(true)
->addAlias('PluginManager')
->rule(SsoUtils::class)
->setShared(true)
// ThemeManager
->rule('Gdn_ThemeManager')
->setShared(true)
->addAlias('ThemeManager')
// Logger
->rule(\Vanilla\Logger::class)
->setShared(true)
->addAlias(\Psr\Log\LoggerInterface::class)
->rule(\Psr\Log\LoggerAwareInterface::class)
->addCall('setLogger')
// EventManager
->rule(\Garden\EventManager::class)
->setShared(true)
// Locale
->rule('Gdn_Locale')
->setShared(true)
->setConstructorArgs([new Reference(['Gdn_Configuration', 'Garden.Locale'])])
->addAlias('Locale')
// Request
->rule('Gdn_Request')
->setShared(true)
->addCall('fromEnvironment')
->addAlias('Request')
->addAlias(\Garden\Web\RequestInterface::class)
// Database.
->rule('Gdn_Database')
->setShared(true)
->setConstructorArgs([new Reference(['Gdn_Configuration', 'Database'])])
->addAlias('Database')
->rule('Gdn_DatabaseStructure')
->setClass('Gdn_MySQLStructure')
->setShared(true)
->addAlias(Gdn::AliasDatabaseStructure)
->addAlias('MySQLStructure')
->rule('Gdn_SQLDriver')
->setClass('Gdn_MySQLDriver')
->setShared(true)
->addAlias('Gdn_MySQLDriver')
->addAlias('MySQLDriver')
->addAlias(Gdn::AliasSqlDriver)
->rule('Identity')
->setClass('Gdn_CookieIdentity')
->setShared(true)
->rule('Gdn_Session')
->setShared(true)
->addAlias('Session')
->rule(Gdn::AliasAuthenticator)
->setClass('Gdn_Auth')
->setShared(true)
->rule('Gdn_Router')
->addAlias(Gdn::AliasRouter)
->setShared(true)
->rule('Gdn_Dispatcher')
->setShared(true)
->addAlias(Gdn::AliasDispatcher)
->rule(\Garden\Web\Dispatcher::class)
->setShared(true)
->addCall('addRoute', ['route' => new Reference('@api-v2-route'), 'api-v2'])
->addCall('addRoute', ['route' => new \Garden\Container\Callback(function () {
return new \Garden\Web\PreflightRoute('/api/v2', true);
})])
->addCall('setAllowedOrigins', ['isTrustedDomain'])
->addCall('addMiddleware', [new Reference('@smart-id-middleware')])
->addCall('addMiddleware', [new Reference(\Vanilla\Web\CacheControlMiddleware::class)])
->rule('@smart-id-middleware')
->setClass(\Vanilla\Web\SmartIDMiddleware::class)
->setConstructorArgs(['/api/v2/'])
->addCall('addSmartID', ['CategoryID', 'categories', ['name', 'urlcode'], 'Category'])
->addCall('addSmartID', ['RoleID', 'roles', ['name'], 'Role'])
->addCall('addSmartID', ['UserID', 'users', '*', new Reference('@user-smart-id-resolver')])
->rule('@user-smart-id-resolver')
->setFactory(function (Container $dic) {
/* @var \Vanilla\Web\UserSmartIDResolver $uid */
$uid = $dic->get(\Vanilla\Web\UserSmartIDResolver::class);
$uid->setEmailEnabled(!$dic->get(Gdn_Configuration::class)->get('Garden.Registration.NoEmail'))
->setViewEmail($dic->get(\Gdn_Session::class)->checkPermission('Garden.PersonalInfo.View'));
return $uid;
})
->rule('@api-v2-route')
->setClass(\Garden\Web\ResourceRoute::class)
->setConstructorArgs(['/api/v2/', '*\\%sApiController'])
->addCall('setMeta', ['CONTENT_TYPE', 'application/json; charset=utf-8'])
->rule('@view-application/json')
->setClass(\Vanilla\Web\JsonView::class)
->setShared(true)
->rule(\Garden\ClassLocator::class)
->setClass(\Vanilla\VanillaClassLocator::class)
->rule('Gdn_Model')
->setShared(true)
->rule(Gdn_Validation::class)
->addCall('addRule', ['BodyFormat', new Reference(\Vanilla\BodyFormatValidator::class)])
->rule(\Vanilla\Models\AuthenticatorModel::class)
->setShared(true)
->addCall('registerAuthenticatorClass', [\Vanilla\Authenticator\PasswordAuthenticator::class])
->rule(SearchModel::class)
->setShared(true)
->rule('Gdn_IPlugin')
->setShared(true)
->rule(Gdn_Plugin::class)
->setShared(true)
->addCall('setAddonFromManager')
->rule('Gdn_Slice')
->setShared(true)
->addAlias('Slice')
->rule('Gdn_Statistics')
->addAlias('Statistics')
->setShared(true)
->rule('Gdn_Regarding')
->setShared(true)
->rule('BBCodeFormatter')
->setClass('BBCode')
->setShared(true)
->rule('HtmlFormatter')
->setClass(VanillaHtmlFormatter::class)
->addAlias(VanillaHtmlFormatter::class)
->setShared(true)
->rule(\Vanilla\Formatting\Quill\Renderer::class)
->setShared(true)
->rule(\Vanilla\Formatting\Quill\Parser::class)
->addCall('addCoreBlotsAndFormats')
->setShared(true)
->rule('Smarty')
->setShared(true)
->rule('WebLinking')
->setClass(\Vanilla\Web\WebLinking::class)
->setShared(true)
->rule('ViewHandler.tpl')
->setClass('Gdn_Smarty')
->setShared(true)
->rule('Gdn_Form')
->addAlias('Form')
->rule(Vanilla\Formatting\Embeds\EmbedManager::class)
->addCall('addCoreEmbeds')
->setShared(true)
->rule(Vanilla\PageScraper::class)
->addCall('registerMetadataParser', [new Reference(Vanilla\Metadata\Parser\OpenGraphParser::class)])
->addCall('registerMetadataParser', [new Reference(Vanilla\Metadata\Parser\JsonLDParser::class)])
->setShared(true)
;
// Run through the bootstrap with dependencies.
$dic->call(function (
Container $dic,
Gdn_Configuration $config,
\Vanilla\AddonManager $addonManager,
\Garden\EventManager $eventManager,
Gdn_Request $request // remove later
) {
// Load default baseline Garden configurations.
$config->load(PATH_CONF.'/config-defaults.php');
// Load installation-specific configuration so that we know what apps are enabled.
$config->load($config->defaultPath(), 'Configuration', true);
/**
* Bootstrap Early
*
* A lot of the framework is loaded now, most importantly the core autoloader,
* default config and the general and error functions. More control is possible
* here, but some things have already been loaded and are immutable.
*/
if (file_exists(PATH_CONF.'/bootstrap.early.php')) {
require_once PATH_CONF.'/bootstrap.early.php';
}
$config->caching(true);
debug($config->get('Debug', false));
setHandlers();
/**
* Installer Redirect
*
* If Garden is not yet installed, force the request to /dashboard/setup and
* begin installation.
*/
if ($config->get('Garden.Installed', false) === false && strpos($request->path(), 'setup') === false) {
safeHeader('Location: '.$request->url('dashboard/setup', true));
exit();
}
/**
* Extension Managers
*
* Now load the Addon, Application, Theme and Plugin managers into the Factory, and
* process the application-specific configuration defaults.
*/
// Start the addons, plugins, and applications.
$addonManager->startAddonsByKey(c('EnabledPlugins'), Addon::TYPE_ADDON);
$addonManager->startAddonsByKey(c('EnabledApplications'), Addon::TYPE_ADDON);
$addonManager->startAddonsByKey(array_keys(c('EnabledLocales', [])), Addon::TYPE_LOCALE);
$currentTheme = c('Garden.Theme', Gdn_ThemeManager::DEFAULT_DESKTOP_THEME);
if (isMobile()) {
$currentTheme = c('Garden.MobileTheme', Gdn_ThemeManager::DEFAULT_MOBILE_THEME);
}
$addonManager->startAddonsByKey([$currentTheme], Addon::TYPE_THEME);
// Load the configurations for enabled addons.
foreach ($addonManager->getEnabled() as $addon) {
/* @var Addon $addon */
if ($configPath = $addon->getSpecial('config')) {
$config->load($addon->path($configPath));
}
}
// Re-apply loaded user settings.
$config->overlayDynamic();
/**
* Bootstrap Late
*
* All configurations are loaded, as well as the Application, Plugin and Theme
* managers.
*/
if (file_exists(PATH_CONF.'/bootstrap.late.php')) {
require_once PATH_CONF.'/bootstrap.late.php';
}
if ($config->get('Debug')) {
debug(true);
}
Gdn_Cache::trace(debug()); // remove later
/**
* Extension Startup
*
* Allow installed addons to execute startup and bootstrap procedures that they may have, here.
*/
// Bootstrapping.
foreach ($addonManager->getEnabled() as $addon) {
/* @var Addon $addon */
if ($bootstrapPath = $addon->getSpecial('bootstrap')) {
$bootstrapPath = $addon->path($bootstrapPath);
include_once $bootstrapPath;
}
}
// Plugins startup
$addonManager->bindAllEvents($eventManager);
if ($eventManager->hasHandler('gdn_pluginManager_afterStart')) {
$eventManager->fire('gdn_pluginManager_afterStart', $dic->get(Gdn_PluginManager::class));
}
// Now that all of the events have been bound, fire an event that allows plugins to modify the container.
$eventManager->fire('container_init', $dic);
});
// Send out cookie headers.
register_shutdown_function(function() use ($dic) {
$dic->call(function(Garden\Web\Cookie $cookie) {
$cookie->flush();
});
});
/**
* Locales
*
* Install any custom locales provided by applications and plugins, and set up
* the locale management system.
*/
// Load the Garden locale system.
$dic->get('Gdn_Locale');
require_once PATH_LIBRARY_CORE.'/functions.validation.php';
// Start Authenticators
$dic->get('Authenticator')->startAuthenticator();
/**
* Bootstrap After
*
* After the bootstrap has finished loading, this hook allows developers a last
* chance to customize Garden's runtime environment before the actual request
* is handled.
*/
if (file_exists(PATH_ROOT.'/conf/bootstrap.after.php')) {
require_once PATH_ROOT.'/conf/bootstrap.after.php';
}
// Include "Render" functions now - this way pluggables and custom confs can override them.
require_once PATH_LIBRARY_CORE.'/functions.render.php';
if (!defined('CLIENT_NAME')) {
define('CLIENT_NAME', 'vanilla');
}