-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathSettingsConfigRepository.php
More file actions
107 lines (88 loc) · 2.61 KB
/
Copy pathSettingsConfigRepository.php
File metadata and controls
107 lines (88 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
namespace ProcessMaker\Repositories;
use Illuminate\Config\Repository;
use Illuminate\Support\Arr;
use ProcessMaker\Models\Setting;
class SettingsConfigRepository extends Repository
{
/**
* Determine if the given configuration value exists.
*
* @param string $key
* @return bool
*/
public function has($key)
{
if (Arr::has($this->items, $key)) {
return true;
}
return $this->getFromSettings($key) ? true : false;
}
/**
* Get the specified configuration value.
*
* @param array|string $key
* @param mixed $default
* @return mixed
*/
public function get($key, $default = null)
{
if (is_array($key)) {
return $this->getMany($key);
}
if ($key === 'session.lifetime') {
$settingValue = $this->getFromSettings($key);
return $settingValue ?? $default;
}
if (Arr::has($this->items, $key)) {
return Arr::get($this->items, $key);
}
return $this->getFromSettings($key) ?? $default;
}
/**
* Get many configuration values.
*
* @param array<string|int,mixed> $keys
* @return array<string,mixed>
*/
public function getMany($keys)
{
$config = [];
foreach ($keys as $key => $default) {
if (is_numeric($key)) {
[$key, $default] = [$default, null];
}
if (Arr::has($this->items, $key)) {
$config[$key] = Arr::get($this->items, $key);
} elseif ($setting = $this->getFromSettings($key)) {
$config[$key] = $setting;
} else {
$config[$key] = $default;
}
}
return $config;
}
private function getFromSettings($key)
{
if (!Setting::readyToUseSettingsDatabase()) {
return null;
}
$setting = Setting::byKey($key);
if ($setting !== null) {
Arr::set($this->items, $key, $setting->config);
return $setting->config;
}
// If the key is a dot notation, we can try to get the first part
// and then use the dot notation to get the value if it's an array.
$parts = explode('.', $key);
if (count($parts) > 1) {
$firstKey = array_shift($parts);
$setting = Setting::byKey($firstKey);
if ($setting && $setting->format === 'array') {
$subPath = implode('.', $parts);
return Arr::get($setting->config, $subPath);
}
}
return null;
}
}