1 | <?php |
||
22 | class Config { |
||
23 | use |
||
24 | CRUD, |
||
25 | Properties_getter, |
||
26 | Singleton; |
||
27 | const INIT_STATE_METHOD = 'init'; |
||
28 | const SYSTEM_MODULE = 'System'; |
||
29 | const SYSTEM_THEME = 'CleverStyle'; |
||
30 | /** |
||
31 | * @var Cache\Prefix |
||
32 | */ |
||
33 | protected $cache; |
||
34 | /** |
||
35 | * Most of general configuration properties |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | public $core = []; |
||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $core_internal = []; |
||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $last_language; |
||
48 | /** |
||
49 | * Configuration of databases, except the main database, parameters of which are stored in configuration file |
||
50 | * |
||
51 | * @var mixed[] |
||
52 | */ |
||
53 | public $db = []; |
||
54 | /** |
||
55 | * Configuration of storages, except the main storage, parameters of which are stored in configuration file |
||
56 | * |
||
57 | * @var mixed[] |
||
58 | */ |
||
59 | public $storage = []; |
||
60 | /** |
||
61 | * Internal structure of components parameters |
||
62 | * |
||
63 | * @var array[] |
||
64 | */ |
||
65 | public $components = []; |
||
66 | /** |
||
67 | * Array of all domains, which allowed to access the site |
||
68 | * |
||
69 | * Contains keys: |
||
70 | * * count - Total count |
||
71 | * * http - Insecure (http) domains |
||
72 | * * https - Secure (https) domains |
||
73 | * |
||
74 | * @var array |
||
75 | */ |
||
76 | public $mirrors; |
||
77 | protected $data_model = [ |
||
78 | 'domain' => 'text', |
||
79 | 'core' => 'json', |
||
80 | 'db' => 'json', |
||
81 | 'storage' => 'json', |
||
82 | 'components' => 'json' |
||
83 | ]; |
||
84 | protected $table = '[prefix]config'; |
||
85 | 6 | protected function cdb () { |
|
88 | /** |
||
89 | * Update multilingual options when language changes |
||
90 | */ |
||
91 | 72 | protected function init () { |
|
92 | 72 | Event::instance()->on( |
|
93 | 72 | 'System/Language/change/after', |
|
94 | function () { |
||
95 | 54 | $this->read_core_update_multilingual(); |
|
96 | 72 | } |
|
97 | ); |
||
98 | 72 | } |
|
99 | /** |
||
100 | * Loading of configuration, initialization of $Config, $Cache, $L and Page objects, Routing processing |
||
101 | * |
||
102 | * @throws ExitException |
||
103 | */ |
||
104 | 72 | protected function construct () { |
|
105 | 72 | $this->cache = Cache::prefix('config'); |
|
106 | 72 | Event::instance()->fire('System/Config/init/before'); |
|
107 | 72 | $this->load_configuration(); |
|
108 | 72 | Event::instance()->fire('System/Config/init/after'); |
|
109 | 72 | if (!file_exists(MODULES.'/'.$this->core['default_module'])) { |
|
110 | 2 | $this->core['default_module'] = self::SYSTEM_MODULE; |
|
111 | 2 | $this->save(); |
|
112 | } |
||
113 | 72 | } |
|
114 | /** |
||
115 | * Reloading of settings cache |
||
116 | * |
||
117 | * @throws ExitException |
||
118 | */ |
||
119 | 72 | protected function load_configuration () { |
|
120 | /** |
||
121 | * @var array[] $config |
||
122 | */ |
||
123 | 72 | $config = $this->cache->get( |
|
124 | 72 | 'source', |
|
125 | function () { |
||
126 | 4 | return $this->read(Core::instance()->domain); |
|
127 | 72 | } |
|
128 | ); |
||
129 | 72 | if (!$config) { |
|
130 | 2 | throw new ExitException('Failed to load system configuration', 500); |
|
131 | } |
||
132 | 72 | $this->core_internal = $config['core'] + Options::get_defaults(); |
|
133 | 72 | $this->core = $this->core_internal; |
|
134 | 72 | $this->db = $config['db']; |
|
135 | 72 | $this->storage = $config['storage']; |
|
136 | 72 | $this->components = $config['components']; |
|
137 | 72 | date_default_timezone_set($this->core['timezone']); |
|
138 | 72 | $this->fill_mirrors(); |
|
139 | 72 | $this->read_core_update_multilingual(true); |
|
140 | 72 | } |
|
141 | /** |
||
142 | * Is used to fill `$this->mirrors` using current configuration |
||
143 | */ |
||
144 | 72 | protected function fill_mirrors () { |
|
157 | /** |
||
158 | * @param bool $force |
||
159 | */ |
||
160 | 72 | protected function read_core_update_multilingual ($force = false) { |
|
183 | /** |
||
184 | * Get core options item |
||
185 | * |
||
186 | * @param string[]|string[][] $item |
||
187 | * |
||
188 | * @return mixed|mixed[]|null Core options items (or associative array of items) if exists or `null` otherwise (in case if `$item` is an array even one |
||
189 | * missing key will cause the whole thing to fail) |
||
190 | */ |
||
191 | 2 | public function core (...$item) { |
|
194 | /** |
||
195 | * Applying settings without saving changes into db |
||
196 | * |
||
197 | * @return bool |
||
198 | * |
||
199 | * @throws ExitException |
||
200 | */ |
||
201 | 2 | public function apply () { |
|
218 | /** |
||
219 | * Applying settings without saving changes into db |
||
220 | * |
||
221 | * @param bool $cache_not_saved_mark |
||
222 | * |
||
223 | * @return bool |
||
224 | * |
||
225 | * @throws ExitException |
||
226 | */ |
||
227 | 4 | protected function apply_internal ($cache_not_saved_mark = true) { |
|
250 | /** |
||
251 | * Saving settings |
||
252 | * |
||
253 | * @return bool |
||
254 | * |
||
255 | * @throws ExitException |
||
256 | */ |
||
257 | 4 | public function save () { |
|
279 | /** |
||
280 | * Whether configuration was applied (not saved) and can be canceled |
||
281 | * |
||
282 | * @return bool |
||
283 | */ |
||
284 | 2 | public function cancel_available () { |
|
287 | /** |
||
288 | * Canceling of applied settings |
||
289 | * |
||
290 | * @throws ExitException |
||
291 | */ |
||
292 | 2 | public function cancel () { |
|
296 | /** |
||
297 | * Get base url of current mirror including language suffix |
||
298 | * |
||
299 | * @return string |
||
300 | */ |
||
301 | 8 | public function base_url () { |
|
312 | /** |
||
313 | * Get base url of main domain |
||
314 | * |
||
315 | * @return string |
||
316 | */ |
||
317 | 20 | public function core_url () { |
|
321 | /** |
||
322 | * Get object for getting db and storage configuration of module |
||
323 | * |
||
324 | * @param string $module_name |
||
325 | * |
||
326 | * @return Config\Module_Properties |
||
327 | */ |
||
328 | 52 | public function module ($module_name) { |
|
335 | } |
||
336 |