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