1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by rozbo at 2017/3/19 下午5:09 |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace puck; |
7
|
|
|
use puck\tools\Arr; |
8
|
|
|
|
9
|
|
|
class Config { |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* All of the configuration items. |
13
|
|
|
* |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
protected $items = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Create a new configuration repository. |
20
|
|
|
* |
21
|
|
|
* @param array $items |
22
|
|
|
* @return void |
|
|
|
|
23
|
|
|
*/ |
24
|
|
|
public function __construct(array $items = []) |
25
|
|
|
{ |
26
|
|
|
$this->items = $items; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Determine if the given configuration value exists. |
31
|
|
|
* |
32
|
|
|
* @param string $key |
33
|
|
|
* @return bool |
34
|
|
|
*/ |
35
|
|
|
public function has($key) |
36
|
|
|
{ |
37
|
|
|
return Arr::has($this->items, $key); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get the specified configuration value. |
42
|
|
|
* |
43
|
|
|
* @param string $key |
44
|
|
|
* @param mixed $default |
45
|
|
|
* @return mixed |
46
|
|
|
*/ |
47
|
|
|
public function get($key, $default = null) |
48
|
|
|
{ |
49
|
|
|
return Arr::get($this->items, $key, $default); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Set a given configuration value. |
54
|
|
|
* |
55
|
|
|
* @param array|string $key |
56
|
|
|
* @param mixed $value |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
public function set($key, $value = null) |
60
|
|
|
{ |
61
|
|
|
$keys = is_array($key) ? $key : [$key => $value]; |
62
|
|
|
|
63
|
|
|
foreach ($keys as $key => $value) { |
64
|
|
|
Arr::set($this->items, $key, $value); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Prepend a value onto an array configuration value. |
70
|
|
|
* |
71
|
|
|
* @param string $key |
72
|
|
|
* @param mixed $value |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
public function prepend($key, $value) |
76
|
|
|
{ |
77
|
|
|
$array = $this->get($key); |
78
|
|
|
|
79
|
|
|
array_unshift($array, $value); |
80
|
|
|
|
81
|
|
|
$this->set($key, $array); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Push a value onto an array configuration value. |
86
|
|
|
* |
87
|
|
|
* @param string $key |
88
|
|
|
* @param mixed $value |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
|
|
public function push($key, $value) |
92
|
|
|
{ |
93
|
|
|
$array = $this->get($key); |
94
|
|
|
|
95
|
|
|
$array[] = $value; |
96
|
|
|
|
97
|
|
|
$this->set($key, $array); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get all of the configuration items for the application. |
102
|
|
|
* |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
|
public function all() |
106
|
|
|
{ |
107
|
|
|
return $this->items; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Determine if the given configuration option exists. |
112
|
|
|
* |
113
|
|
|
* @param string $key |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
public function offsetExists($key) |
117
|
|
|
{ |
118
|
|
|
return $this->has($key); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get a configuration option. |
123
|
|
|
* |
124
|
|
|
* @param string $key |
125
|
|
|
* @return mixed |
126
|
|
|
*/ |
127
|
|
|
public function offsetGet($key) |
128
|
|
|
{ |
129
|
|
|
return $this->get($key); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Set a configuration option. |
134
|
|
|
* |
135
|
|
|
* @param string $key |
136
|
|
|
* @param mixed $value |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
|
|
public function offsetSet($key, $value) |
140
|
|
|
{ |
141
|
|
|
$this->set($key, $value); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Unset a configuration option. |
146
|
|
|
* |
147
|
|
|
* @param string $key |
148
|
|
|
* @return void |
149
|
|
|
*/ |
150
|
|
|
public function offsetUnset($key) |
151
|
|
|
{ |
152
|
|
|
$this->set($key, null); |
153
|
|
|
} |
154
|
|
|
} |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.