Passed
Push — develop ( e37a4b...d665c0 )
by Schlaefer
38s
created

Bootstrap   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 86.84%

Importance

Changes 0
Metric Value
wmc 31
lcom 2
cbo 5
dl 0
loc 182
ccs 66
cts 76
cp 0.8684
rs 9.8
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __clone() 0 3 1
A initializeBasics() 0 9 1
A getInstance() 0 7 2
C initializeDefinitions() 0 15 10
A initializeAutoloader() 0 17 2
A initializeConfiguration() 0 13 2
C initializeFilesAndFolders() 0 26 7
A initializePlugins() 0 20 4
A getPlugins() 0 4 1
1
<?php
2
/**
3
 * the Bootstrap of Phile
4
 */
5
namespace Phile;
6
7
use Phile\Exception\PluginException;
8
use Phile\Plugin\PluginRepository;
9
10
/**
11
 * Phile
12
 *
13
 * @author  Frank Nägler
14
 * @link    https://github.com/PhileCMS/Phile
15
 * @license http://opensource.org/licenses/MIT
16
 * @version 0.1
17
 */
18
class Bootstrap
19
{
20
    /**
21
     * @var \Phile\Bootstrap instance of Bootstrap class
22
     */
23
    static protected $instance = null;
24
25
    /**
26
     * @var array the settings array
27
     */
28
    protected $settings;
29
30
    /**
31
     * @var array the loaded plugins
32
     */
33
    protected $plugins;
34
35
    /**
36
     * the constructor
37
     * Disable direct creation of this object.
38
     */
39
    protected function __construct()
40
    {
41
    }
42
43
    /**
44
     * Disable direct cloning of this object.
45
     */
46
    protected function __clone()
47
    {
48
    }
49
50
    /**
51
     * Return instance of Bootstrap class as singleton
52
     *
53
     * @return Bootstrap
54
     */
55 2
    public static function getInstance()
56
    {
57 2
        if (is_null(static::$instance)) {
58
            self::$instance = new Bootstrap();
59
        }
60 2
        return static::$instance;
61
    }
62
63
    /**
64
     * initialize basics
65
     */
66 2
    public function initializeBasics()
67
    {
68 2
        $this->initializeDefinitions();
69 2
        $this->initializeAutoloader();
70 2
        $this->initializeConfiguration();
71 2
        $this->initializeFilesAndFolders();
72 2
        $this->initializePlugins();
73 2
        return $this;
74
    }
75
76
    /**
77
     * initialize the global definitions
78
     */
79 2
    protected function initializeDefinitions()
80
    {
81
        // for php unit testings, we need to check if constant is defined
82
        // before setting them, because there is a bug in PHPUnit which
83
        // init our bootstrap multiple times.
84 2
        defined('PHILE_VERSION') || define('PHILE_VERSION', '1.8.0');
85 2
        defined('PHILE_CLI_MODE') || define('PHILE_CLI_MODE', (php_sapi_name() === 'cli'));
86 2
        defined('DS') || define('DS', DIRECTORY_SEPARATOR);
87 2
        defined('ROOT_DIR') || define('ROOT_DIR', realpath(__DIR__ . DS . '..' . DS . '..' . DS) . DS);
88 2
        defined('LIB_DIR') || define('LIB_DIR', ROOT_DIR . 'lib' . DS);
89 2
        defined('PLUGINS_DIR') || define('PLUGINS_DIR', ROOT_DIR . 'plugins' . DS);
90 2
        defined('THEMES_DIR') || define('THEMES_DIR', ROOT_DIR . 'themes' . DS);
91 2
        defined('CACHE_DIR') || define('CACHE_DIR', LIB_DIR . 'cache' . DS);
92 2
        defined('STORAGE_DIR') || define('STORAGE_DIR', LIB_DIR . 'datastorage' . DS);
93 2
    }
94
95
    /**
96
     * initialize the autoloader
97
     */
98 17
    protected function initializeAutoloader()
99
    {
100 2
        spl_autoload_extensions(".php");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal .php does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
101
        // load phile core
102 2
        spl_autoload_register(
103 17
            function ($className) {
104 15
                $fileName = LIB_DIR . str_replace("\\", DS, $className) . '.php';
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal \\ does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
105 15
                if (file_exists($fileName)) {
106 13
                    include_once $fileName;
107
                }
108 17
            }
109
        );
110
        // load phile plugins
111 2
        spl_autoload_register('\Phile\Plugin\PluginRepository::autoload');
112
113 2
        include LIB_DIR . 'vendor' . DS . 'autoload.php';
114 2
    }
115
116
    /**
117
     * initialize configuration
118
     */
119 2
    protected function initializeConfiguration()
120
    {
121 2
        $defaults      = Utility::load(ROOT_DIR . 'default_config.php');
122 2
        $localSettings = Utility::load(ROOT_DIR . 'config.php');
123 2
        if (is_array($localSettings)) {
124 2
            $this->settings = array_replace_recursive($defaults, $localSettings);
125
        } else {
126
            $this->settings = $defaults;
0 ignored issues
show
Documentation Bug introduced by
It seems like $defaults of type * is incompatible with the declared type array of property $settings.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
127
        }
128
129 2
        Registry::set('Phile_Settings', $this->settings);
130 2
        date_default_timezone_set($this->settings['timezone']);
131 2
    }
132
133
    /**
134
     * auto-setup of files and folders
135
     */
136 2
    protected function initializeFilesAndFolders()
137
    {
138
        $dirs = [
139 2
        ['path' => CACHE_DIR],
140 2
        ['path' => STORAGE_DIR]
141
        ];
142 2
        $defaults = ['protected' => true];
143
144 2
        foreach ($dirs as $dir) {
145 2
            $dir += $defaults;
146 2
            $path = $dir['path'];
147 2
            if (empty($path) || strpos($path, ROOT_DIR) !== 0) {
148
                continue;
149
            }
150 2
            if (!file_exists($path)) {
151 1
                mkdir($path, 0775, true);
152
            }
153 2
            if ($dir['protected']) {
154 2
                $file = "$path.htaccess";
155 2
                if (!file_exists($file)) {
156 1
                    $content = "order deny,allow\ndeny from all\nallow from 127.0.0.1";
157 2
                    file_put_contents($file, $content);
158
                }
159
            }
160
        }
161 2
    }
162
163
    /**
164
     * initialize plugins
165
     *
166
     * @throws Exception\PluginException
167
     */
168 2
    protected function initializePlugins()
169
    {
170 2
        $loader = new PluginRepository();
171 2
        if (isset($this->settings['plugins']) && is_array($this->settings['plugins'])) {
172 2
            $this->plugins = $loader->loadAll($this->settings['plugins']);
173
        }
174
175 2
        Event::triggerEvent('plugins_loaded', ['plugins' => $this->plugins]);
176
177
        // throw not earlier to have the error-handler plugin loaded
178
        // and initialized (by 'plugins_loaded' event)
179 2
        $errors = $loader->getLoadErrors();
180 2
        if (count($errors) > 0) {
181
            throw new PluginException($errors[0]['message'], $errors[0]['code']);
182
        }
183
184
        // settings now include initialized plugin-configs
185 2
        $this->settings = Registry::get('Phile_Settings');
0 ignored issues
show
Documentation Bug introduced by
It seems like \Phile\Registry::get('Phile_Settings') of type * is incompatible with the declared type array of property $settings.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
186 2
        Event::triggerEvent('config_loaded', ['config' => $this->settings]);
187 2
    }
188
189
    /**
190
     * method to get plugins
191
     * @return array
192
     * @deprecated since 1.5 will be removed
193
     * @use 'plugins_loaded' event
194
     */
195
    public function getPlugins()
196
    {
197
        return $this->plugins;
198
    }
199
}
200