Completed
Push — develop ( d07230...dec153 )
by Schlaefer
05:14 queued 02:44
created

Bootstrap   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 67.57%

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 5
dl 0
loc 75
ccs 25
cts 37
cp 0.6757
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A loadConfiguration() 0 8 1
B setupFolder() 0 14 5
A loadPlugins() 0 21 2
A setupErrorHandler() 0 12 3
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 27
        $load = function ($file) {
29 27
            return include $file;
30 27
        };
31 27
        $config->merge($load($file));
32 27
    }
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 27
    public static function loadPlugins(Event $eventBus, Config $config)
58
    {
59 27
        $pluginsToLoad = $config->get('plugins');
60
61 27
        $loader = new PluginRepository($config->get('plugins_dir'));
62 27
        $plugins = $loader->loadAll($pluginsToLoad);
63 27
        $errors = $loader->getLoadErrors();
64
65 27
        $eventBus->trigger('plugins_loaded', ['plugins' => $plugins]);
66
67
        // throw after 'plugins_loaded'
68 27
        if (count($errors) > 0) {
69
            throw new PluginException($errors[0]['message'], $errors[0]['code']);
70
        }
71
72
        // settings include initialized plugin-configs now
73 27
        $eventBus->trigger(
74 27
            'config_loaded',
75 27
            ['config' => $config->toArray(), 'class' => $config]
76
        );
77 27
    }
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
            return;
87
        }
88
        $errorHandler = ServiceLocator::getService('Phile_ErrorHandler');
89
        set_error_handler([$errorHandler, 'handleError']);
90
        set_exception_handler([$errorHandler, 'handleException']);
91
        register_shutdown_function([$errorHandler, 'handleShutdown']);
92
        ini_set('display_errors', $config->get('display_errors'));
93
    }
94
}
95