Passed
Push — release/1.11.0 ( 94cc26 )
by Schlaefer
02:26
created

Bootstrap   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 91.3%

Importance

Changes 0
Metric Value
dl 0
loc 56
c 0
b 0
f 0
ccs 21
cts 23
cp 0.913
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setupErrorHandler() 0 10 3
B setupFolder() 0 12 5
A loadConfiguration() 0 7 1
1
<?php
2
/*
3
 * @author  PhileCMS
4
 * @link    https://philecms.github.io
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
     * @param string $file Path to config file to load
25
     * @param Config $config Phile configuration
26
     * @return void
27
     */
28
    public static function loadConfiguration($file, Config $config)
29
    {
30
        // function isolates context of loaded files
31 31
        $load = function (string $file): array {
32 31
            return include $file;
33 31
        };
34 31
        $config->merge($load($file));
35 31
    }
36
37
    /**
38
     * Creates and protects folder and path $directory.
39
     *
40
     * @param string $directory Path to $directory
41
     * @param Config $config Phile configuration
42
     * @return void
43
     */
44 1
    public static function setupFolder(string $directory, Config $config)
45
    {
46 1
        if (empty($directory) || strpos($directory, $config->get('root_dir')) !== 0) {
47
            return;
48
        }
49 1
        if (!file_exists($directory)) {
50 1
            mkdir($directory, 0775, true);
51
        }
52 1
        $htaccessPath = "$directory.htaccess";
53 1
        if (!file_exists($htaccessPath)) {
54 1
            $htaccessContent = "order deny,allow\ndeny from all\nallow from 127.0.0.1";
55 1
            file_put_contents($htaccessPath, $htaccessContent);
56
        }
57 1
    }
58
59
    /**
60
     * Initializes error handling
61
     *
62
     * @param Config $config Phile configuration
63
     * @return void
64
     */
65 1
    public static function setupErrorHandler(Config $config)
66
    {
67 1
        $cliMode = $config->get('phile_cli_mode');
68 1
        if ($cliMode || !ServiceLocator::hasService('Phile_ErrorHandler')) {
69
            return;
70
        }
71 1
        $errorHandler = ServiceLocator::getService('Phile_ErrorHandler');
72 1
        set_error_handler([$errorHandler, 'handleError']);
73 1
        set_exception_handler([$errorHandler, 'handleException']);
74 1
        register_shutdown_function([$errorHandler, 'handleShutdown']);
75 1
    }
76
}
77