-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.php
executable file
·116 lines (86 loc) · 3.3 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
<?php
use vxPHP\Application\Config\DotEnvReader;
use vxPHP\Application\Exception\ConfigException;
use vxWeb\Config\Parser\CustomVxweb;
// enable error reporting (might be disabled at end of bootstrap)
ini_set('display_errors', true);
error_reporting(E_ALL & ~(E_STRICT|E_DEPRECATED));
if(!file_exists($rootPath . '/vendor/autoload.php')) {
die ('No autoloader found. Please run "composer install".');
}
require_once $rootPath . '/vendor/autoload.php';
// populate $_ENV
if (file_exists($rootPath . '/.env')) {
(new DotEnvReader($rootPath . '/.env'))->read();
}
$iniPath = $rootPath . DIRECTORY_SEPARATOR . 'ini' . DIRECTORY_SEPARATOR;
$configFilename = $iniPath . 'site.ini.xml';
$cachedConfigPath = $iniPath . '.cache' . DIRECTORY_SEPARATOR;
$cachedConfigFilename = $cachedConfigPath . 'app_config';
// read configuration and cache if necessary
if(!file_exists($configFilename)) {
die ('No site.ini.xml in ' . $iniPath . ' found.');
}
if(file_exists($cachedConfigFilename)) {
/*
* check whether cached file is outdated
*
* checks .env file in root path
* checks all XML files in the ini folder tree (whether they are relevant or not)
* does not consider any included XML files outside the ini folder
*/
$cachedFileTimestamp = filemtime($cachedConfigFilename);
$cachedIsValid = true;
if (file_exists($rootPath . '/.env') && filemtime($rootPath . '/.env') > $cachedFileTimestamp) {
$cachedIsValid = false;
}
else {
foreach (
new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$iniPath,
FilesystemIterator::SKIP_DOTS
),
RecursiveIteratorIterator::CHILD_FIRST
) as $f) {
if ($f->getMTime() > $cachedFileTimestamp && strtolower($f->getExtension()) === 'xml') {
$cachedIsValid = false;
break;
}
}
}
if($cachedIsValid) {
$config = unserialize(file_get_contents($cachedConfigFilename), ['allowed_classes' => true]);
}
}
// cached config was either not found or is outdated
if(!isset($config)) {
// create cache path if no cached config was found and path does not exist
if(!isset($cachedFileTimestamp) && !file_exists($cachedConfigPath) && !mkdir($cachedConfigPath, 0777, true) && !is_dir($cachedConfigPath)) {
die ('Cannot create directory ' . $cachedConfigPath);
}
// create config instance
try {
$config = new vxPHP\Application\Config($configFilename, ['parsers' => ['custom_vxweb' => CustomVxweb::class]]);
}
catch(ConfigException $e) {
die('<h1>Cannot create Config instance</h1><pre>' . $e->getMessage() . '</pre>');
}
// write file to cache
if(false === file_put_contents($cachedConfigFilename, serialize($config))) {
die ('<h1>Cannot create serialized config file</h1>' . $cachedConfigFilename);
}
}
// initialize application
$application = vxPHP\Application\Application::getInstance($config);
// set path information
$application
->setRootPath ($rootPath)
->setAbsoluteAssetsPath ($assetsPath)
->setRelativeAssetsPath (dirname($_SERVER['SCRIPT_NAME']))
->registerPlugins ()
;
// set debugging and error reporting level depending on environment
if(!$application::runsLocally()) {
vxPHP\Debug\Debug::enable(E_ALL, false);
}