Passed
Branch master (748c89)
by Schlaefer
02:28
created

Bootstrap   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 69.44%

Importance

Changes 0
Metric Value
dl 0
loc 75
ccs 25
cts 36
cp 0.6944
rs 10
c 0
b 0
f 0
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setupErrorHandler() 0 11 3
B setupFolder() 0 12 5
A loadPlugins() 0 21 2
A loadConfiguration() 0 7 1
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 28
        $load = function ($file) {
28 28
            return include $file;
29 28
        };
30 28
        $config->merge($load($file));
31 28
    }
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 28
    public static function loadPlugins(Event $eventBus, Config $config)
57
    {
58 28
        $pluginsToLoad = $config->get('plugins');
59
60 28
        $loader = new PluginRepository($config->get('plugins_dir'));
61 28
        $plugins = $loader->loadAll($pluginsToLoad);
62 28
        $errors = $loader->getLoadErrors();
63
64 28
        $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 28
        if (count($errors) > 0) {
70
            throw new PluginException($errors[0]['message'], $errors[0]['code']);
71
        }
72
73
        // settings include initialized plugin-configs now
74 28
        $eventBus->trigger(
75 28
            'config_loaded',
76 28
            ['config' => $config->toArray(), 'class' => $config]
77
        );
78 28
    }
79
80
    /**
81
     * Initializes error handling
82
     */
83
    public static function setupErrorHandler(Config $config)
84
    {
85
        $cliMode = $config->get('phile_cli_mode');
86
        if ($cliMode || !ServiceLocator::hasService('Phile_ErrorHandler')) {
87
            return;
88
        }
89
        $errorHandler = ServiceLocator::getService('Phile_ErrorHandler');
90
        set_error_handler([$errorHandler, 'handleError']);
91
        set_exception_handler([$errorHandler, 'handleException']);
92
        register_shutdown_function([$errorHandler, 'handleShutdown']);
93
        ini_set('display_errors', $config->get('display_errors'));
94
    }
95
}
96