Passed
Push — master ( 10b09c...54e166 )
by Schlaefer
04:38
created

Config::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Phile\Core;
4
5
/**
6
 * Config class
7
 *
8
 * @author  PhileCMS
9
 * @link    https://philecms.com
10
 * @license http://opensource.org/licenses/MIT
11
 * @package Phile
12
 */
13
class Config
14
{
15
    /** @var boolean configuration is writable */
16
    protected $isLocked = false;
17
    
18 40
    public function __construct(array $values = [])
19
    {
20 40
        Registry::set('Phile_Settings', []);
21 40
        $this->set($values);
22 40
    }
23
24
    /**
25
     * Getter for configuration values
26
     *
27
     * @param string $key single key
28
     * @return mixed
29
     */
30 38
    public function get($key = null)
31
    {
32 38
        $config = Registry::get('Phile_Settings');
33
34 38
        if (array_key_exists($key, $config)) {
35 37
            return $config[$key];
36
        }
37
38 28
        $constant = strtoupper($key);
39 28
        if (defined($constant)) {
40 28
            return constant($constant);
41
        }
42
43 27
        return null;
44
    }
45
46 28
    public function has($key)
47
    {
48 28
        $config = Registry::get('Phile_Settings');
49 28
        return array_key_exists($key, $config);
50
    }
51
52
    /**
53
     * Return configuration as PHP-array
54
     *
55
     * @return array
56
     */
57 33
    public function toArray()
58
    {
59 33
        return Registry::get('Phile_Settings');
60
    }
61
62
    /**
63
     * Setter for configuration values
64
     *
65
     * @param string|array $key set single key value; sell all if array
66
     * @param mixed $value
67
     */
68 41
    public function set($key, $value = null)
69
    {
70 41
        $config = Registry::get('Phile_Settings');
71
72 41
        if ($this->isLocked) {
73 1
            throw new \LogicException(
74 1
                sprintf('Phile-configuration is locked. Can\' set key "%s"', $key),
75 1
                1518440759
76
            );
77
        }
78
79 41
        if ($value === null && is_array($key)) {
80 41
            $config = $key;
81
        } else {
82 27
            $config[$key] = $value;
83
        }
84
85 41
        Registry::set('Phile_Settings', $config);
86 41
    }
87
88
    /**
89
     * Recursively merges a configuration over the existing configuration
90
     *
91
     * @param array $values configuration to merge
92
     */
93 27
    public function merge(array $values)
94
    {
95 27
        $old = $this->toArray();
96 27
        $new = array_replace_recursive($old, $values);
97 27
        $this->set($new);
98 27
    }
99
100
    /**
101
     * Creates an array of template variables derived from the configuration
102
     *
103
     * @return array
104
     */
105 3
    public function getTemplateVars()
106
    {
107
        return [
108 3
            'base_dir' => rtrim($this->get('root_dir'), '/'),
109 3
            'base_url' => $this->get('base_url'),
110 3
            'config' => $this->toArray(),
111 3
            'content_dir' => $this->get('content_dir'),
112 3
            'content_url' => $this->get('base_url') . '/' . basename($this->get('content_dir')),
113 3
            'site_title' => $this->get('site_title'),
114 3
            'theme_dir' => $this->get('themes_dir') . $this->get('theme'),
115 3
            'theme_url' => $this->get('base_url') . '/'
116 3
                . basename($this->get('themes_dir')) . '/' . $this->get('theme'),
117
        ];
118
    }
119
120
    /**
121
     * Locks configuration into read-only mode
122
     */
123 5
    public function lock()
124
    {
125 5
        $this->isLocked = true;
126 5
    }
127
}
128