-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.php
152 lines (137 loc) · 6.13 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
<?php
if (!class_exists('\\Composer\\Autoload\\ClassLoader', false)) {
if (file_exists(__DIR__ . '/../../autoload.php')) {
require_once __DIR__ . '/../../autoload.php';
} else {
// Note: We cannot report errors for cli as some scripts will need to run before the composer files are installed
if (PHP_SAPI != 'cli') {
header('Content-Type: text/html; charset=utf-8');
header('503 Service Unavailable');
if (!isset($GLOBALS['STARTER_BOOTSTRAP_DEV']) || !$GLOBALS['STARTER_BOOTSTRAP_DEV']) {
echo "<h1>503 Service Unavailable</h1>";
} else {
echo "<h1>Composer autoload required</h1>\n";
echo "<p>The system could not be properly bootstrapped, install Composer first. See README.md for details.</p>\n";
}
exit();
}
}
}
if ((isset($GLOBALS['STARTER_DEBUG_TRACE']) ? $GLOBALS['STARTER_DEBUG_TRACE'] : false) &&
function_exists('xdebug_stop_trace')
) {
xdebug_start_trace(
isset($GLOBALS['STARTER_DEBUG_TRACE_FILE']) ? $GLOBALS['STARTER_DEBUG_TRACE_FILE'] : "debug-trace",
isset($GLOBALS['STARTER_DEBUG_TRACE_OPTIONS']) ? $GLOBALS['STARTER_DEBUG_TRACE_OPTIONS'] : 0
);
$GLOBALS['STARTER_DEBUG_TRACE_STARTED'] = true;
}
// If the bootstrap process needs to be debugged for errors set $GLOBALS['STARTER_BASE_DEBUG'] to true
// This will install the Whoops error handler as early as possible
$errorHandler = null;
// Automatically turn on error handling for startup code if 'dev' is part of STARTER_CONFIGS
// Unless STARTER_BASE_DEBUG is explicitly set
if (!isset($GLOBALS['STARTER_BASE_DEBUG']) && isset($GLOBALS['STARTER_CONFIGS']) && is_array($GLOBALS['STARTER_CONFIGS']) && in_array('dev', $GLOBALS['STARTER_CONFIGS'])) {
$GLOBALS['STARTER_BASE_DEBUG'] = true;
}
if (isset($GLOBALS['STARTER_BASE_DEBUG']) && $GLOBALS['STARTER_BASE_DEBUG']) {
if (class_exists('\\Whoops\\Run')) {
$errorHandler = new \Whoops\Run;
$errorHandler->pushHandler(new \Whoops\Handler\PrettyPageHandler);
// Additional handler for plain-text but will only activate for CLI
$textHandler = new \Whoops\Handler\PlainTextHandler;
$hasWhoops2 = interface_exists('\\Whoops\\RunInterface');
if ($hasWhoops2) {
if (PHP_SAPI !== 'cli') {
$textHandler->loggerOnly(true);
}
} else {
$textHandler->outputOnlyIfCommandLine(true);
}
$errorHandler->pushHandler($textHandler);
$errorHandler->register();
// Turn on all types of errors excepts deprecations, this should help with
// catching any problems during startup
// Once BaseApp is initialized it will be changed according what the application
// is currently configured as.
error_reporting(~E_DEPRECATED);
}
}
// We need the www-root and app-root set for the rest of the code work properly
if (!isset($_ENV['WWW_ROOT'])) {
$_ENV['WWW_ROOT'] = realpath(__DIR__ . "/../../../");
}
// If Dotenv can be loaded we use that to support a .env file
if (file_exists($_ENV['WWW_ROOT'] . '/.env') && class_exists('\\Dotenv')) {
// Load values from .env if it exists
try {
$dotenv = new \Dotenv();
$dotenv->load($_ENV["WWW_ROOT"]);
} catch (\Exception $e) {
// Ignore error if there is no .env file, we do not require it
}
}
if (!isset($_ENV['APP_ROOT'])) {
$_ENV['APP_ROOT'] = $_ENV['WWW_ROOT'] . '/' . (isset($GLOBALS['STARTER_APP_PATH']) ? $GLOBALS['STARTER_APP_PATH'] : 'extension/site');
}
// Load the cache as soon as possible to reduce the amount of code to execute
if (isset($GLOBALS['STARTER_APP_CACHE']) ? $GLOBALS['STARTER_APP_CACHE'] : true) {
$wwwPath = $_ENV['WWW_ROOT'];
if (isset($_ENV['BUILD_PATH'])) {
$buildPath = $_ENV['BUILD_PATH'] . '/bootstrap';
} else {
$buildPath = isset($GLOBALS['STARTER_BOOTSTRAP_BUILD']) ? $GLOBALS['STARTER_BOOTSTRAP_BUILD'] : 'build/bootstrap';
}
if (getenv('FRAMEWORK')) {
$frameworks = explode(",", getenv('FRAMEWORK'));
} elseif (isset($GLOBALS['STARTER_FRAMEWORK'])) {
$frameworks = $GLOBALS['STARTER_FRAMEWORK'];
if (!is_array($frameworks)) {
$frameworks = array($frameworks);
}
} else {
$frameworks = array('ezp');
}
$frameworkIds = implode("_", $frameworks);
$configPath = "$wwwPath/$buildPath/config_$frameworkIds.json";
$bootstrapPath = "$wwwPath/$buildPath/bootstrap_$frameworkIds.php";
if (file_exists($configPath) && file_exists($bootstrapPath)) {
$settings = null;
$jsonData = @file_get_contents($configPath);
if ($jsonData) {
$settings = json_decode($jsonData, true);
}
$GLOBALS['STARTER_ERROR_INSTANCE'] = $errorHandler;
// Load the cached application
$GLOBALS['STARTER_APP'] = $app = require $bootstrapPath;
}
}
// Fallback to dynamically setting up the application
if (!isset($GLOBALS['STARTER_APP'])) {
$GLOBALS['STARTER_APP'] = $app = \Aplia\Bootstrap\Base::createApp(array(
'startupErrorHandler' => $errorHandler,
));
// Configure the app unless STARTER_BASE_CONFIGURE tells us not to
if (isset($GLOBALS['STARTER_BASE_CONFIGURE']) ? $GLOBALS['STARTER_BASE_CONFIGURE'] : true) {
$app->configure(\Aplia\Bootstrap\Base::fetchConfigNames());
$app->postConfigure();
if (isset($GLOBALS['STARTER_BASE_INIT']) ? $GLOBALS['STARTER_BASE_INIT'] : true) {
$app->init();
}
}
}
if (isset($GLOBALS['STARTER_BASE_DUMP_CONFIG']) && $GLOBALS['STARTER_BASE_DUMP_CONFIG']) {
$jsonOpts = version_compare(PHP_VERSION, '5.4.0') >= 0 ? (JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : 0;
if (PHP_SAPI == 'cli') {
echo json_encode($GLOBALS['STARTER_APP'], $jsonOpts), "\n";
} else {
echo "<pre>", json_encode($GLOBALS['STARTER_APP'], $jsonOpts), "</pre>";
}
exit;
}
if ((isset($GLOBALS['STARTER_DEBUG_TRACE_STARTED']) ? $GLOBALS['STARTER_DEBUG_TRACE_STARTED'] : false) &&
function_exists('xdebug_stop_trace')
) {
xdebug_stop_trace();
}
return $GLOBALS['STARTER_APP'];