1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* @author PhileCMS |
4
|
|
|
* @link https://philecms.com |
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
|
|
|
public static function loadConfiguration($file, Config $config) |
25
|
|
|
{ |
26
|
|
|
// function isolates context of loaded files |
27
|
29 |
|
$load = function ($file) { |
28
|
29 |
|
return include $file; |
29
|
29 |
|
}; |
30
|
29 |
|
$config->merge($load($file)); |
31
|
29 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Creates and protects folder and path $directory |
35
|
|
|
*/ |
36
|
1 |
|
public static function setupFolder($directory, Config $config) |
37
|
|
|
{ |
38
|
1 |
|
if (empty($directory) || strpos($directory, $config->get('root_dir')) !== 0) { |
39
|
|
|
return; |
40
|
|
|
} |
41
|
1 |
|
if (!file_exists($directory)) { |
42
|
1 |
|
mkdir($directory, 0775, true); |
43
|
|
|
} |
44
|
1 |
|
$htaccessPath = "$directory.htaccess"; |
45
|
1 |
|
if (!file_exists($htaccessPath)) { |
46
|
1 |
|
$htaccessContent = "order deny,allow\ndeny from all\nallow from 127.0.0.1"; |
47
|
1 |
|
file_put_contents($htaccessPath, $htaccessContent); |
48
|
|
|
} |
49
|
1 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Loads all plug-ins |
53
|
|
|
* |
54
|
|
|
* @throws Exception\PluginException |
55
|
|
|
*/ |
56
|
29 |
|
public static function loadPlugins(Event $eventBus, Config $config) |
57
|
|
|
{ |
58
|
29 |
|
$pluginsToLoad = $config->get('plugins'); |
59
|
|
|
|
60
|
29 |
|
$loader = new PluginRepository($config->get('plugins_dir')); |
61
|
29 |
|
$plugins = $loader->loadAll($pluginsToLoad); |
62
|
29 |
|
$errors = $loader->getLoadErrors(); |
63
|
|
|
|
64
|
29 |
|
$eventBus->trigger('plugins_loaded', ['plugins' => $plugins]); |
65
|
|
|
|
66
|
|
|
// Throw after 'plugins_loaded' so that error handler service is set. |
67
|
|
|
// Even with setupErrorHandler() not run yet, the global app try/catch |
68
|
|
|
// block uses the handler if present. |
69
|
29 |
|
if (count($errors) > 0) { |
70
|
|
|
throw new PluginException($errors[0]['message'], $errors[0]['code']); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// settings include initialized plugin-configs now |
74
|
29 |
|
$eventBus->trigger( |
75
|
29 |
|
'config_loaded', |
76
|
29 |
|
['config' => $config->toArray(), 'class' => $config] |
77
|
|
|
); |
78
|
29 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Initializes error handling |
82
|
|
|
*/ |
83
|
1 |
|
public static function setupErrorHandler(Config $config) |
84
|
|
|
{ |
85
|
1 |
|
$cliMode = $config->get('phile_cli_mode'); |
86
|
1 |
|
if ($cliMode || !ServiceLocator::hasService('Phile_ErrorHandler')) { |
87
|
|
|
return; |
88
|
|
|
} |
89
|
1 |
|
$errorHandler = ServiceLocator::getService('Phile_ErrorHandler'); |
90
|
1 |
|
set_error_handler([$errorHandler, 'handleError']); |
91
|
1 |
|
set_exception_handler([$errorHandler, 'handleException']); |
92
|
1 |
|
register_shutdown_function([$errorHandler, 'handleShutdown']); |
93
|
1 |
|
} |
94
|
|
|
} |
95
|
|
|
|