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
|
39 |
|
public function __construct(array $values = []) |
19
|
|
|
{ |
20
|
39 |
|
Registry::set('Phile_Settings', []); |
21
|
39 |
|
$this->set($values); |
22
|
39 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Getter for configuration values |
26
|
|
|
* |
27
|
|
|
* @param string $key single key |
28
|
|
|
* @return mixed |
29
|
|
|
*/ |
30
|
37 |
|
public function get($key = null) |
31
|
|
|
{ |
32
|
37 |
|
$config = Registry::get('Phile_Settings'); |
33
|
|
|
|
34
|
37 |
|
if (array_key_exists($key, $config)) { |
35
|
36 |
|
return $config[$key]; |
36
|
|
|
} |
37
|
|
|
|
38
|
27 |
|
$constant = strtoupper($key); |
39
|
27 |
|
if (defined($constant)) { |
40
|
27 |
|
return constant($constant); |
41
|
|
|
} |
42
|
|
|
|
43
|
25 |
|
return null; |
44
|
|
|
} |
45
|
|
|
|
46
|
27 |
|
public function has($key) |
47
|
|
|
{ |
48
|
27 |
|
$config = Registry::get('Phile_Settings'); |
49
|
27 |
|
return array_key_exists($key, $config); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Return configuration as PHP-array |
54
|
|
|
* |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
31 |
|
public function toArray() |
58
|
|
|
{ |
59
|
31 |
|
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
|
40 |
|
public function set($key, $value = null) |
69
|
|
|
{ |
70
|
40 |
|
$config = Registry::get('Phile_Settings'); |
71
|
|
|
|
72
|
40 |
|
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
|
40 |
|
if ($value === null && is_array($key)) { |
80
|
40 |
|
$config = $key; |
81
|
|
|
} else { |
82
|
26 |
|
$config[$key] = $value; |
83
|
|
|
} |
84
|
|
|
|
85
|
40 |
|
Registry::set('Phile_Settings', $config); |
86
|
40 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Recursively merges a configuration over the existing configuration |
90
|
|
|
* |
91
|
|
|
* @param array $values configuration to merge |
92
|
|
|
*/ |
93
|
26 |
|
public function merge(array $values) |
94
|
|
|
{ |
95
|
26 |
|
$old = $this->toArray(); |
96
|
26 |
|
$new = array_replace_recursive($old, $values); |
97
|
26 |
|
$this->set($new); |
98
|
26 |
|
} |
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
|
|
|
|