|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lincable; |
|
4
|
|
|
|
|
5
|
|
|
use Lincable\Concerns\BuildClassnames; |
|
6
|
|
|
use Illuminate\Contracts\Config\Repository; |
|
7
|
|
|
use Lincable\Exceptions\ConfModelNotFoundException; |
|
8
|
|
|
|
|
9
|
|
|
class UrlConf implements Repository |
|
10
|
|
|
{ |
|
11
|
|
|
use BuildClassnames; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* The url model configuration. |
|
15
|
|
|
* |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $configuration = []; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The base namespace for the models. |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $modelsNamespace; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Create a new class instance. |
|
29
|
|
|
* |
|
30
|
|
|
* @param array $conf |
|
31
|
|
|
* @return void |
|
32
|
|
|
*/ |
|
33
|
58 |
|
public function __construct(string $modelsNamespace) |
|
34
|
|
|
{ |
|
35
|
58 |
|
$this->modelsNamespace = $modelsNamespace; |
|
36
|
58 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Determine if the given configuration value exists. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $key |
|
42
|
|
|
* @return bool |
|
43
|
|
|
*/ |
|
44
|
43 |
|
public function has($key) |
|
45
|
|
|
{ |
|
46
|
43 |
|
return isset($this->configuration[$key]); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get the specified configuration value. |
|
51
|
|
|
* |
|
52
|
|
|
* @param array|string $key |
|
53
|
|
|
* @param mixed $default |
|
54
|
|
|
* @return mixed |
|
55
|
|
|
*/ |
|
56
|
39 |
|
public function get($key, $default = null) |
|
57
|
|
|
{ |
|
58
|
39 |
|
return $this->has($key) ? $this->configuration[$key] : $default; |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get all of the configuration items for the application. |
|
63
|
|
|
* |
|
64
|
|
|
* @return array |
|
65
|
|
|
*/ |
|
66
|
46 |
|
public function all() |
|
67
|
|
|
{ |
|
68
|
46 |
|
return $this->configuration; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Set a given configuration value. |
|
73
|
|
|
* |
|
74
|
|
|
* @param array|string $key |
|
75
|
|
|
* @param mixed $value |
|
76
|
|
|
* @return void |
|
77
|
|
|
*/ |
|
78
|
3 |
|
public function set($key, $value = null) |
|
79
|
|
|
{ |
|
80
|
3 |
|
if (is_array($key)) { |
|
81
|
|
|
|
|
82
|
|
|
// Handle an array insert value. |
|
83
|
1 |
|
foreach ($key as $k => $v) { |
|
84
|
1 |
|
$this->set($k, $v); |
|
85
|
|
|
} |
|
86
|
|
|
} else { |
|
87
|
|
|
|
|
88
|
|
|
// Get model class name from key. |
|
89
|
3 |
|
$key = $this->getModelFromKey($key); |
|
90
|
|
|
|
|
91
|
|
|
// Set the configuration. |
|
92
|
3 |
|
$this->configuration[$key] = $value; |
|
93
|
|
|
} |
|
94
|
3 |
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Prepend a value onto an array configuration value. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $key |
|
100
|
|
|
* @param mixed $value |
|
101
|
|
|
* @return void |
|
102
|
|
|
*/ |
|
103
|
|
|
public function prepend($key, $value) |
|
104
|
|
|
{ |
|
105
|
|
|
$this->push($key, $value); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Push a value onto an array configuration value. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $key |
|
112
|
|
|
* @param mixed $value |
|
113
|
|
|
* @return void |
|
114
|
|
|
*/ |
|
115
|
46 |
|
public function push($key, $value) |
|
116
|
|
|
{ |
|
117
|
46 |
|
$this->configuration = array_add( |
|
118
|
46 |
|
$this->all(), |
|
119
|
46 |
|
$this->getModelFromKey($key), |
|
120
|
44 |
|
$value |
|
121
|
|
|
); |
|
122
|
44 |
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Set the models namespace. |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $namespace |
|
128
|
|
|
* @return this |
|
129
|
|
|
*/ |
|
130
|
|
|
public function setModelsNamespace(string $namespce) |
|
131
|
|
|
{ |
|
132
|
|
|
$this->modelsNamespace = $namespce; |
|
133
|
|
|
|
|
134
|
|
|
return $this; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Return the model namespace from key. |
|
139
|
|
|
* |
|
140
|
|
|
* @param string $key |
|
141
|
|
|
* @return string |
|
142
|
|
|
*/ |
|
143
|
46 |
|
protected function getModelFromKey(string $key) |
|
144
|
|
|
{ |
|
145
|
46 |
|
$model = $key; |
|
146
|
|
|
|
|
147
|
46 |
|
if (str_contains($key, '.') || ! str_contains($key, '\\')) { |
|
148
|
|
|
|
|
149
|
|
|
// Build the class namespace using the base model namespace |
|
150
|
|
|
// and the parts of the key splitted by dots. |
|
151
|
18 |
|
$model = $this->buildNamespace(array_merge( |
|
152
|
18 |
|
[$this->modelsNamespace], |
|
153
|
18 |
|
array_map('ucfirst', explode('.', $key)) |
|
154
|
|
|
)); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
46 |
|
if (class_exists($model)) { |
|
158
|
44 |
|
return $model; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
2 |
|
throw new ConfModelNotFoundException("Could not find model [{$key}]. We tried to load from [{$model}]."); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|