Passed
Push — master ( 1282f8...92a01c )
by Schlaefer
03:31
created

Bootstrap   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 87.18%

Importance

Changes 0
Metric Value
wmc 33
lcom 2
cbo 5
dl 0
loc 184
ccs 68
cts 78
cp 0.8718
rs 9.3999
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
C initializeDefinitions() 0 17 12
A getInstance() 0 7 2
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('CONTENT_DIR') || define('CONTENT_DIR', ROOT_DIR . 'content' . DS);
89 2
        defined('CONTENT_EXT') || define('CONTENT_EXT', '.md');
90 2
        defined('LIB_DIR') || define('LIB_DIR', ROOT_DIR . 'lib' . DS);
91 2
        defined('PLUGINS_DIR') || define('PLUGINS_DIR', ROOT_DIR . 'plugins' . DS);
92 2
        defined('THEMES_DIR') || define('THEMES_DIR', ROOT_DIR . 'themes' . DS);
93 2
        defined('CACHE_DIR') || define('CACHE_DIR', LIB_DIR . 'cache' . DS);
94 2
        defined('STORAGE_DIR') || define('STORAGE_DIR', LIB_DIR . 'datastorage' . DS);
95 2
    }
96
97
    /**
98
     * initialize the autoloader
99
     */
100 17
    protected function initializeAutoloader()
101
    {
102 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...
103
        // load phile core
104 2
        spl_autoload_register(
105 17
            function ($className) {
106 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...
107 15
                if (file_exists($fileName)) {
108 13
                    include_once $fileName;
109
                }
110 17
            }
111
        );
112
        // load phile plugins
113 2
        spl_autoload_register('\Phile\Plugin\PluginRepository::autoload');
114
115 2
        include LIB_DIR . 'vendor' . DS . 'autoload.php';
116 2
    }
117
118
    /**
119
     * initialize configuration
120
     */
121 2
    protected function initializeConfiguration()
122
    {
123 2
        $defaults      = Utility::load(ROOT_DIR . 'default_config.php');
124 2
        $localSettings = Utility::load(ROOT_DIR . 'config.php');
125 2
        if (is_array($localSettings)) {
126 2
            $this->settings = array_replace_recursive($defaults, $localSettings);
127
        } else {
128
            $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...
129
        }
130
131 2
        Registry::set('Phile_Settings', $this->settings);
132 2
        date_default_timezone_set($this->settings['timezone']);
133 2
    }
134
135
    /**
136
     * auto-setup of files and folders
137
     */
138 2
    protected function initializeFilesAndFolders()
139
    {
140
        $dirs = [
141 2
        ['path' => CACHE_DIR],
142 2
        ['path' => STORAGE_DIR]
143
        ];
144 2
        $defaults = ['protected' => true];
145
146 2
        foreach ($dirs as $dir) {
147 2
            $dir += $defaults;
148 2
            $path = $dir['path'];
149 2
            if (empty($path) || strpos($path, ROOT_DIR) !== 0) {
150
                continue;
151
            }
152 2
            if (!file_exists($path)) {
153 1
                mkdir($path, 0775, true);
154
            }
155 2
            if ($dir['protected']) {
156 2
                $file = "$path.htaccess";
157 2
                if (!file_exists($file)) {
158 1
                    $content = "order deny,allow\ndeny from all\nallow from 127.0.0.1";
159 2
                    file_put_contents($file, $content);
160
                }
161
            }
162
        }
163 2
    }
164
165
    /**
166
     * initialize plugins
167
     *
168
     * @throws Exception\PluginException
169
     */
170 2
    protected function initializePlugins()
171
    {
172 2
        $loader = new PluginRepository();
173 2
        if (isset($this->settings['plugins']) && is_array($this->settings['plugins'])) {
174 2
            $this->plugins = $loader->loadAll($this->settings['plugins']);
175
        }
176
177 2
        Event::triggerEvent('plugins_loaded', ['plugins' => $this->plugins]);
178
179
        // throw not earlier to have the error-handler plugin loaded
180
        // and initialized (by 'plugins_loaded' event)
181 2
        $errors = $loader->getLoadErrors();
182 2
        if (count($errors) > 0) {
183
            throw new PluginException($errors[0]['message'], $errors[0]['code']);
184
        }
185
186
        // settings now include initialized plugin-configs
187 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...
188 2
        Event::triggerEvent('config_loaded', ['config' => $this->settings]);
189 2
    }
190
191
    /**
192
     * method to get plugins
193
     * @return array
194
     * @deprecated since 1.5 will be removed
195
     * @use 'plugins_loaded' event
196
     */
197
    public function getPlugins()
198
    {
199
        return $this->plugins;
200
    }
201
}
202