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
|
|
|
public function __construct(string $modelsNamespace) |
34
|
|
|
{ |
35
|
|
|
$this->modelsNamespace = $modelsNamespace; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Determine if the given configuration value exists. |
40
|
|
|
* |
41
|
|
|
* @param string $key |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
|
|
public function has($key) |
45
|
|
|
{ |
46
|
|
|
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
|
|
|
public function get($key, $default = null) |
57
|
|
|
{ |
58
|
|
|
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
|
|
|
public function all() |
67
|
|
|
{ |
68
|
|
|
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
|
|
|
public function set($key, $value = null) |
79
|
|
|
{ |
80
|
|
|
$this->push($key, $value); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Prepend a value onto an array configuration value. |
85
|
|
|
* |
86
|
|
|
* @param string $key |
87
|
|
|
* @param mixed $value |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
|
|
public function prepend($key, $value) |
91
|
|
|
{ |
92
|
|
|
$this->push($key, $value); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Push a value onto an array configuration value. |
97
|
|
|
* |
98
|
|
|
* @param string $key |
99
|
|
|
* @param mixed $value |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
|
|
public function push($key, $value) |
103
|
|
|
{ |
104
|
|
|
$this->configuration = array_add( |
105
|
|
|
$this->all(), |
106
|
|
|
$this->getModelFromKey($key), |
107
|
|
|
$value |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set the models namespace. |
113
|
|
|
* |
114
|
|
|
* @param string $namespace |
115
|
|
|
* @return this |
116
|
|
|
*/ |
117
|
|
|
public function setModelsNamespace(string $namespce) |
118
|
|
|
{ |
119
|
|
|
$this->modelsNamespace = $namespce; |
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Return the model namespace from key. |
125
|
|
|
* |
126
|
|
|
* @param string $key |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
protected function getModelFromKey(string $key) |
130
|
|
|
{ |
131
|
|
|
$model = $key; |
132
|
|
|
|
133
|
|
|
if (str_contains($key, '.') || ! str_contains($key, '\\')) { |
134
|
|
|
|
135
|
|
|
// Build the class namespace using the base model namespace |
136
|
|
|
// and the parts of the key splitted by dots. |
137
|
|
|
$model = $this->buildNamespace(array_merge( |
138
|
|
|
[$this->modelsNamespace], |
139
|
|
|
array_map('ucfirst', explode('.', $key)) |
140
|
|
|
)); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if (class_exists($model)) { |
144
|
|
|
return $model; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
throw new ConfModelNotFoundException("Could not find model [{$key}]. We tried to load from [{$model}]."); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|