Passed
Push — develop ( 91c1d2...446210 )
by Schlaefer
41s
created

Bootstrap::setupErrorHandler()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 2
nop 1
crap 12
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\Registry;
13
use Phile\Core\ServiceLocator;
14
use Phile\Exception\PluginException;
15
use Phile\Plugin\PluginRepository;
16
17
/**
18
 * Bootstrap class
19
 */
20
class Bootstrap
21
{
22
    /**
23
     * Loads $file into $configuration
24
     */
25
    public static function loadConfiguration($file, Config $config)
26
    {
27
        // function isolates context of loaded files
28 26
        $load = function ($file) {
29 26
            return include $file;
30 26
        };
31 26
        $config->merge($load($file));
32 26
    }
33
34
    /**
35
     * Creates and protects folder and path $directory
36
     */
37 1
    public static function setupFolder($directory, Config $config)
38
    {
39 1
        if (empty($directory) || strpos($directory, $config->get('root_dir')) !== 0) {
40
            return;
41
        }
42 1
        if (!file_exists($directory)) {
43 1
            mkdir($directory, 0775, true);
44
        }
45 1
        $htaccessPath = "$directory.htaccess";
46 1
        if (!file_exists($htaccessPath)) {
47 1
            $htaccessContent = "order deny,allow\ndeny from all\nallow from 127.0.0.1";
48 1
            file_put_contents($htaccessPath, $htaccessContent);
49
        }
50 1
    }
51
52
    /**
53
     * Loads all plug-ins
54
     *
55
     * @throws Exception\PluginException
56
     */
57 26
    public static function loadPlugins(Event $eventBus, Config $config)
58
    {
59 26
        $pluginsToLoad = $config->get('plugins');
60
61 26
        $loader = new PluginRepository($config->get('plugins_dir'));
62 26
        $plugins = $loader->loadAll($pluginsToLoad);
63 26
        $errors = $loader->getLoadErrors();
64
65 26
        $eventBus->trigger('plugins_loaded', ['plugins' => $plugins]);
66
67
        // throw after 'plugins_loaded'
68 26
        if (count($errors) > 0) {
69
            throw new PluginException($errors[0]['message'], $errors[0]['code']);
70
        }
71
72
        // settings include initialized plugin-configs now
73 26
        $eventBus->trigger(
74 26
            'config_loaded',
75 26
            ['config' => $config->toArray(), 'class' => $config]
76
        );
77 26
    }
78
79
    /**
80
     * Initializes error handling
81
     */
82
    public static function setupErrorHandler(Config $config)
83
    {
84
        $cliMode = $config->get('phile_cli_mode');
85
        if (!$cliMode && ServiceLocator::hasService('Phile_ErrorHandler')) {
86
            $errorHandler = ServiceLocator::getService('Phile_ErrorHandler');
87
            set_error_handler([$errorHandler, 'handleError']);
88
            register_shutdown_function([$errorHandler, 'handleShutdown']);
89
            ini_set('display_errors', $config->get('display_errors'));
90
        }
91
    }
92
}
93