-
-
Notifications
You must be signed in to change notification settings - Fork 522
/
bootstrap.php
298 lines (231 loc) · 11.1 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
<?php
/**
* This file is part of the Cockpit project.
*
* (c) Artur Heinze - 🅰🅶🅴🅽🆃🅴🅹🅾, http://agentejo.com
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Cockpit start time
*/
define('COCKPIT_START_TIME', microtime(true));
if (!defined('COCKPIT_CLI')) {
define('COCKPIT_CLI', PHP_SAPI == 'cli');
}
// Autoload vendor libs
include(__DIR__.'/lib/vendor/autoload.php');
// include core classes for better performance
if (!class_exists('Lime\\App')) {
include(__DIR__.'/lib/Lime/App.php');
include(__DIR__.'/lib/LimeExtra/App.php');
include(__DIR__.'/lib/LimeExtra/Controller.php');
}
/*
* Autoload from lib folder (PSR-0)
*/
spl_autoload_register(function($class){
$class_path = __DIR__.'/lib/'.str_replace('\\', '/', $class).'.php';
if(file_exists($class_path)) include_once($class_path);
});
// load .env file if exists
DotEnv::load(__DIR__);
// check for custom defines
if (file_exists(__DIR__.'/defines.php')) {
include(__DIR__.'/defines.php');
}
/*
* Collect needed paths
*/
$COCKPIT_DIR = str_replace(DIRECTORY_SEPARATOR, '/', __DIR__);
$COCKPIT_DOCS_ROOT = str_replace(DIRECTORY_SEPARATOR, '/', isset($_SERVER['DOCUMENT_ROOT']) ? realpath($_SERVER['DOCUMENT_ROOT']) : dirname(__DIR__));
# make sure that $_SERVER['DOCUMENT_ROOT'] is set correctly
if (strpos($COCKPIT_DIR, $COCKPIT_DOCS_ROOT)!==0 && isset($_SERVER['SCRIPT_NAME'])) {
$COCKPIT_DOCS_ROOT = str_replace(dirname(str_replace(DIRECTORY_SEPARATOR, '/', $_SERVER['SCRIPT_NAME'])), '', $COCKPIT_DIR);
}
$COCKPIT_BASE = trim(str_replace($COCKPIT_DOCS_ROOT, '', $COCKPIT_DIR), "/");
$COCKPIT_BASE_URL = strlen($COCKPIT_BASE) ? "/{$COCKPIT_BASE}": $COCKPIT_BASE;
$COCKPIT_BASE_ROUTE = $COCKPIT_BASE_URL;
/*
* SYSTEM DEFINES
*/
if (!defined('COCKPIT_DIR')) define('COCKPIT_DIR' , $COCKPIT_DIR);
if (!defined('COCKPIT_ADMIN')) define('COCKPIT_ADMIN' , 0);
if (!defined('COCKPIT_DOCS_ROOT')) define('COCKPIT_DOCS_ROOT' , $COCKPIT_DOCS_ROOT);
if (!defined('COCKPIT_ENV_ROOT')) define('COCKPIT_ENV_ROOT' , COCKPIT_DIR);
if (!defined('COCKPIT_BASE_URL')) define('COCKPIT_BASE_URL' , $COCKPIT_BASE_URL);
if (!defined('COCKPIT_API_REQUEST')) define('COCKPIT_API_REQUEST' , COCKPIT_ADMIN && strpos($_SERVER['REQUEST_URI'], COCKPIT_BASE_URL.'/api/')!==false ? 1:0);
if (!defined('COCKPIT_SITE_DIR')) define('COCKPIT_SITE_DIR' , COCKPIT_ENV_ROOT == COCKPIT_DIR ? ($COCKPIT_DIR == COCKPIT_DOCS_ROOT ? COCKPIT_DIR : dirname(COCKPIT_DIR)) : COCKPIT_ENV_ROOT);
if (!defined('COCKPIT_CONFIG_DIR')) define('COCKPIT_CONFIG_DIR' , COCKPIT_ENV_ROOT.'/config');
if (!defined('COCKPIT_BASE_ROUTE')) define('COCKPIT_BASE_ROUTE' , $COCKPIT_BASE_ROUTE);
if (!defined('COCKPIT_STORAGE_FOLDER')) define('COCKPIT_STORAGE_FOLDER' , COCKPIT_ENV_ROOT.'/storage');
if (!defined('COCKPIT_ADMIN_CP')) define('COCKPIT_ADMIN_CP' , COCKPIT_ADMIN && !COCKPIT_API_REQUEST ? 1 : 0);
if (!defined('COCKPIT_PUBLIC_STORAGE_FOLDER')) define('COCKPIT_PUBLIC_STORAGE_FOLDER' , COCKPIT_ENV_ROOT.'/storage');
if (!defined('COCKPIT_CONFIG_PATH')) {
$_configpath = COCKPIT_CONFIG_DIR.'/config.'.(file_exists(COCKPIT_CONFIG_DIR.'/config.php') ? 'php':'yaml');
define('COCKPIT_CONFIG_PATH', $_configpath);
}
function cockpit($module = null) {
static $app;
if (!$app) {
$customConfig = [];
// load custom config
if (file_exists(COCKPIT_CONFIG_PATH)) {
$customConfig = preg_match('/\.yaml$/', COCKPIT_CONFIG_PATH) ? Spyc::YAMLLoad(COCKPIT_CONFIG_PATH) : include(COCKPIT_CONFIG_PATH);
}
// load config
$config = array_replace_recursive([
'debug' => preg_match('/(localhost|::1|\.local)$/', @$_SERVER['SERVER_NAME']),
'app.name' => 'Cockpit',
'base_url' => COCKPIT_BASE_URL,
'base_route' => COCKPIT_BASE_ROUTE,
'docs_root' => COCKPIT_DOCS_ROOT,
'session.name' => md5(COCKPIT_ENV_ROOT),
'session.init' => (COCKPIT_ADMIN && !COCKPIT_API_REQUEST) ? true : false,
'sec-key' => 'c3b40c4c-db44-s5h7-a814-b4931a15e5e1',
'i18n' => 'en',
'database' => ['server' => 'mongolite://'.(COCKPIT_STORAGE_FOLDER.'/data'), 'options' => ['db' => 'cockpitdb'], 'driverOptions' => [] ],
'memory' => ['server' => 'redislite://'.(COCKPIT_STORAGE_FOLDER.'/data/cockpit.memory.sqlite'), 'options' => [] ],
'paths' => [
'#root' => COCKPIT_DIR,
'#storage' => COCKPIT_STORAGE_FOLDER,
'#pstorage' => COCKPIT_PUBLIC_STORAGE_FOLDER,
'#data' => COCKPIT_STORAGE_FOLDER.'/data',
'#cache' => COCKPIT_STORAGE_FOLDER.'/cache',
'#tmp' => COCKPIT_STORAGE_FOLDER.'/tmp',
'#thumbs' => COCKPIT_PUBLIC_STORAGE_FOLDER.'/thumbs',
'#uploads' => COCKPIT_PUBLIC_STORAGE_FOLDER.'/uploads',
'#modules' => COCKPIT_DIR.'/modules',
'#addons' => COCKPIT_ENV_ROOT.'/addons',
'#config' => COCKPIT_CONFIG_DIR,
'assets' => COCKPIT_DIR.'/assets',
'site' => COCKPIT_SITE_DIR
],
'filestorage' => [],
], is_array($customConfig) ? $customConfig : []);
// make sure Cockpit module is not disabled
if (isset($config['modules.disabled']) && in_array('Cockpit', $config['modules.disabled'])) {
array_splice($config['modules.disabled'], array_search('Cockpit', $config['modules.disabled']), 1);
}
$app = new LimeExtra\App($config);
$app['config'] = $config;
// register paths
foreach ($config['paths'] as $key => $path) {
$app->path($key, $path);
}
// nosql storage
$app->service('storage', function() use($config) {
$client = new MongoHybrid\Client($config['database']['server'], $config['database']['options'], $config['database']['driverOptions']);
return $client;
});
// file storage
$app->service('filestorage', function() use($config, $app) {
$storages = array_replace_recursive([
'root' => [
'adapter' => 'League\Flysystem\Adapter\Local',
'args' => [$app->path('#root:')],
'mount' => true,
'url' => $app->pathToUrl('#root:', true)
],
'site' => [
'adapter' => 'League\Flysystem\Adapter\Local',
'args' => [$app->path('site:')],
'mount' => true,
'url' => $app->pathToUrl('site:', true)
],
'tmp' => [
'adapter' => 'League\Flysystem\Adapter\Local',
'args' => [$app->path('#tmp:')],
'mount' => true,
'url' => $app->pathToUrl('#tmp:', true)
],
'thumbs' => [
'adapter' => 'League\Flysystem\Adapter\Local',
'args' => [$app->path('#thumbs:')],
'mount' => true,
'url' => $app->pathToUrl('#thumbs:', true)
],
'uploads' => [
'adapter' => 'League\Flysystem\Adapter\Local',
'args' => [$app->path('#uploads:')],
'mount' => true,
'url' => $app->pathToUrl('#uploads:', true)
],
'assets' => [
'adapter' => 'League\Flysystem\Adapter\Local',
'args' => [$app->path('#uploads:')],
'mount' => true,
'url' => $app->pathToUrl('#uploads:', true)
]
], $config['filestorage']);
$app->trigger('cockpit.filestorages.init', [&$storages]);
$filestorage = new FileStorage($storages);
return $filestorage;
});
// key-value storage
$app->service('memory', function() use($config) {
$client = new SimpleStorage\Client($config['memory']['server'], $config['memory']['options']);
return $client;
});
// mailer service
$app->service('mailer', function() use($app, $config){
$options = isset($config['mailer']) ? $config['mailer']:[];
if (is_string($options)) {
parse_str($options, $options);
}
$mailer = new \Mailer($options['transport'] ?? 'mail', $options);
return $mailer;
});
// set cache path
$tmppath = $app->path('#tmp:');
$app('cache')->setCachePath($tmppath);
$app->renderer->setCachePath($tmppath);
// i18n
$app('i18n')->locale = $config['i18n'] ?? 'en';
// handle exceptions
if (COCKPIT_ADMIN) {
set_exception_handler(function($exception) use($app) {
$error = [
'message' => $exception->getMessage(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
];
if ($app['debug']) {
$body = $app->request->is('ajax') || COCKPIT_API_REQUEST ? json_encode(['error' => $error['message'], 'file' => $error['file'], 'line' => $error['line']]) : $app->render('cockpit:views/errors/500-debug.php', ['error' => $error]);
} else {
$body = $app->request->is('ajax') || COCKPIT_API_REQUEST ? '{"error": "500", "message": "system error"}' : $app->view('cockpit:views/errors/500.php');
}
$app->trigger('error', [$error, $exception]);
header('HTTP/1.0 500 Internal Server Error');
echo $body;
if (function_exists('cockpit_error_handler')) {
cockpit_error_handler($error);
}
});
}
$modulesPaths = array_merge([
COCKPIT_DIR.'/modules', # core
COCKPIT_DIR.'/addons' # addons
], $config['loadmodules'] ?? []);
if (COCKPIT_ENV_ROOT !== COCKPIT_DIR) {
$modulesPaths[] = COCKPIT_ENV_ROOT.'/addons';
}
// load modules
$app->loadModules($modulesPaths);
// load config global bootstrap file
if ($custombootfile = $app->path('#config:bootstrap.php')) {
include($custombootfile);
}
$app->trigger('cockpit.bootstrap');
}
// shorthand modules method call e.g. cockpit('regions:render', 'test');
if (func_num_args() > 1) {
$arguments = func_get_args();
list($module, $method) = explode(':', $arguments[0]);
array_splice($arguments, 0, 1);
return call_user_func_array([$app->module($module), $method], $arguments);
}
return $module ? $app->module($module) : $app;
}
$cockpit = cockpit();