1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* @author PhileCMS |
4
|
|
|
* @link https://philecms.github.io |
5
|
|
|
* @license http://opensource.org/licenses/MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Phile\Core; |
9
|
|
|
|
10
|
|
|
use Phile\Core\Config; |
11
|
|
|
use Phile\Core\Event; |
12
|
|
|
use Phile\Core\ServiceLocator; |
13
|
|
|
use Phile\Exception\PluginException; |
14
|
|
|
use Phile\Plugin\PluginRepository; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Bootstrap class |
18
|
|
|
*/ |
19
|
|
|
class Bootstrap |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Loads $file into $configuration. |
23
|
|
|
* |
24
|
|
|
* @param string $file Path to config file to load |
25
|
|
|
* @param Config $config Phile configuration |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
|
|
public static function loadConfiguration($file, Config $config) |
29
|
|
|
{ |
30
|
|
|
// function isolates context of loaded files |
31
|
31 |
|
$load = function (string $file): array { |
32
|
31 |
|
return include $file; |
33
|
31 |
|
}; |
34
|
31 |
|
$config->merge($load($file)); |
35
|
31 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Creates and protects folder and path $directory. |
39
|
|
|
* |
40
|
|
|
* @param string $directory Path to $directory |
41
|
|
|
* @param Config $config Phile configuration |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
1 |
|
public static function setupFolder(string $directory, Config $config) |
45
|
|
|
{ |
46
|
1 |
|
if (empty($directory) || strpos($directory, $config->get('root_dir')) !== 0) { |
47
|
|
|
return; |
48
|
|
|
} |
49
|
1 |
|
if (!file_exists($directory)) { |
50
|
1 |
|
mkdir($directory, 0775, true); |
51
|
|
|
} |
52
|
1 |
|
$htaccessPath = "$directory.htaccess"; |
53
|
1 |
|
if (!file_exists($htaccessPath)) { |
54
|
1 |
|
$htaccessContent = "order deny,allow\ndeny from all\nallow from 127.0.0.1"; |
55
|
1 |
|
file_put_contents($htaccessPath, $htaccessContent); |
56
|
|
|
} |
57
|
1 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Initializes error handling |
61
|
|
|
* |
62
|
|
|
* @param Config $config Phile configuration |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
1 |
|
public static function setupErrorHandler(Config $config) |
66
|
|
|
{ |
67
|
1 |
|
$cliMode = $config->get('phile_cli_mode'); |
68
|
1 |
|
if ($cliMode || !ServiceLocator::hasService('Phile_ErrorHandler')) { |
69
|
|
|
return; |
70
|
|
|
} |
71
|
1 |
|
$errorHandler = ServiceLocator::getService('Phile_ErrorHandler'); |
72
|
1 |
|
set_error_handler([$errorHandler, 'handleError']); |
73
|
1 |
|
set_exception_handler([$errorHandler, 'handleException']); |
74
|
1 |
|
register_shutdown_function([$errorHandler, 'handleShutdown']); |
75
|
1 |
|
} |
76
|
|
|
} |
77
|
|
|
|