1 | <?php |
||||
2 | |||||
3 | namespace Phile\Core; |
||||
4 | |||||
5 | /** |
||||
6 | * Config class |
||||
7 | * |
||||
8 | * @author PhileCMS |
||||
9 | * @link https://philecms.github.io |
||||
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 | 49 | public function __construct(array $values = []) |
|||
19 | { |
||||
20 | 49 | Registry::set('Phile_Settings', []); |
|||
21 | 49 | $this->set($values); |
|||
22 | } |
||||
23 | |||||
24 | /** |
||||
25 | * Getter for configuration values |
||||
26 | * |
||||
27 | * @param string $key single key |
||||
28 | * @return mixed |
||||
29 | */ |
||||
30 | 47 | public function get(string $key) |
|||
31 | { |
||||
32 | /** @var array $config */ |
||||
33 | 47 | $config = Registry::get('Phile_Settings'); |
|||
34 | |||||
35 | 47 | if (array_key_exists($key, $config)) { |
|||
36 | 46 | return $config[$key]; |
|||
37 | } |
||||
38 | |||||
39 | 35 | $constant = strtoupper($key); |
|||
40 | 35 | if (defined($constant)) { |
|||
41 | 35 | return constant($constant); |
|||
42 | } |
||||
43 | |||||
44 | 34 | return null; |
|||
45 | } |
||||
46 | |||||
47 | /** |
||||
48 | * Checks if configuration $key exists |
||||
49 | * |
||||
50 | * @param string $key configuration-key |
||||
51 | * @return boolean |
||||
52 | */ |
||||
53 | 35 | public function has(string $key): bool |
|||
54 | { |
||||
55 | 35 | $config = Registry::get('Phile_Settings'); |
|||
56 | 35 | return array_key_exists($key, $config); |
|||
57 | } |
||||
58 | |||||
59 | /** |
||||
60 | * Return configuration as PHP-array |
||||
61 | * |
||||
62 | * @return array |
||||
63 | */ |
||||
64 | 40 | public function toArray() |
|||
65 | { |
||||
66 | 40 | return Registry::get('Phile_Settings'); |
|||
67 | } |
||||
68 | |||||
69 | /** |
||||
70 | * Setter for configuration values |
||||
71 | * |
||||
72 | * @param string|array $key set single key value; sell all if array |
||||
73 | * @param mixed $value |
||||
74 | * @return void |
||||
75 | */ |
||||
76 | 49 | public function set($key, $value = null): void |
|||
77 | { |
||||
78 | 49 | $config = Registry::get('Phile_Settings'); |
|||
79 | |||||
80 | 49 | if ($this->isLocked) { |
|||
81 | 1 | throw new \LogicException( |
|||
82 | 1 | sprintf('Phile-configuration is locked. Can\' set key "%s"', (string)$key), |
|||
83 | 1518440759 |
||||
84 | ); |
||||
85 | } |
||||
86 | |||||
87 | 49 | if ($value === null && is_array($key)) { |
|||
88 | 49 | $config = $key; |
|||
89 | } else { |
||||
90 | 34 | $config[$key] = $value; |
|||
91 | } |
||||
92 | |||||
93 | 49 | Registry::set('Phile_Settings', $config); |
|||
94 | } |
||||
95 | |||||
96 | /** |
||||
97 | * Recursively merges a configuration over the existing configuration |
||||
98 | * |
||||
99 | * @param array $values configuration to merge |
||||
100 | * @return void |
||||
101 | */ |
||||
102 | 34 | public function merge(array $values): void |
|||
103 | { |
||||
104 | 34 | $old = $this->toArray(); |
|||
105 | 34 | $new = array_replace_recursive($old, $values); |
|||
106 | 34 | $this->set($new); |
|||
107 | } |
||||
108 | |||||
109 | /** |
||||
110 | * Creates an array of template variables derived from the configuration |
||||
111 | * |
||||
112 | * @return array |
||||
113 | */ |
||||
114 | 5 | public function getTemplateVars(): array |
|||
115 | { |
||||
116 | return [ |
||||
117 | 5 | 'base_dir' => rtrim($this->get('root_dir'), '/'), |
|||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
118 | 5 | 'base_url' => $this->get('base_url'), |
|||
119 | 5 | 'config' => $this->toArray(), |
|||
120 | 5 | 'content_dir' => $this->get('content_dir'), |
|||
121 | 5 | 'content_url' => $this->get('base_url') . '/' . basename($this->get('content_dir')), |
|||
0 ignored issues
–
show
It seems like
$this->get('content_dir') can also be of type null ; however, parameter $path of basename() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
122 | 5 | 'site_title' => $this->get('site_title'), |
|||
123 | 5 | 'theme_dir' => $this->get('themes_dir') . $this->get('theme'), |
|||
124 | 5 | 'theme_url' => $this->get('base_url') . '/' |
|||
125 | 5 | . basename($this->get('themes_dir')) . '/' . $this->get('theme'), |
|||
126 | ]; |
||||
127 | } |
||||
128 | |||||
129 | /** |
||||
130 | * Locks configuration into read-only mode |
||||
131 | * |
||||
132 | * @return void |
||||
133 | */ |
||||
134 | 9 | public function lock(): void |
|||
135 | { |
||||
136 | 9 | $this->isLocked = true; |
|||
137 | } |
||||
138 | } |
||||
139 |