1 | <?php |
||
8 | class NamespacedRepository extends NamespacedItemResolver implements ArrayAccess |
||
9 | { |
||
10 | /** |
||
11 | * The loader implementation. |
||
12 | * |
||
13 | * @var \LaravelPlus\Extension\Repository\LoaderInterface |
||
14 | */ |
||
15 | protected $loader; |
||
16 | |||
17 | /** |
||
18 | * All of the configuration items. |
||
19 | * |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $items = []; |
||
23 | |||
24 | /** |
||
25 | * Create a new configuration repository. |
||
26 | * |
||
27 | * @param \LaravelPlus\Extension\Repository\LoaderInterface $loader |
||
28 | */ |
||
29 | 6 | public function __construct(LoaderInterface $loader) |
|
33 | |||
34 | /** |
||
35 | * Determine if the given configuration value exists. |
||
36 | * |
||
37 | * @param string $key |
||
38 | * |
||
39 | * @return bool |
||
40 | */ |
||
41 | 1 | public function has($key) |
|
42 | { |
||
43 | 1 | $default = microtime(true); |
|
44 | |||
45 | 1 | return $this->get($key, $default) !== $default; |
|
46 | } |
||
47 | |||
48 | /** |
||
49 | * Determine if a configuration group exists. |
||
50 | * |
||
51 | * @param string $key |
||
52 | * |
||
53 | * @return bool |
||
54 | */ |
||
55 | 1 | public function hasGroup($key) |
|
56 | { |
||
57 | 1 | list($namespace, $group, $item) = $this->parseKey($key); |
|
58 | |||
59 | 1 | return $this->loader->exists($group, $namespace); |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * Get the specified configuration value. |
||
64 | * |
||
65 | * @param string $key |
||
66 | * @param mixed $default |
||
67 | * |
||
68 | * @return mixed |
||
69 | */ |
||
70 | 5 | public function get($key, $default = null) |
|
71 | { |
||
72 | 5 | list($namespace, $group, $item) = $this->parseKey($key); |
|
73 | |||
74 | // Configuration items are actually keyed by "collection", which is simply a |
||
75 | // combination of each namespace and groups, which allows a unique way to |
||
76 | // identify the arrays of configuration items for the particular files. |
||
77 | 5 | $collection = $this->getCollection($group, $namespace); |
|
78 | |||
79 | 5 | $this->load($group, $namespace, $collection); |
|
80 | |||
81 | 5 | return array_get($this->items[$collection], $item, $default); |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * Set a given configuration value. |
||
86 | * |
||
87 | * @param string $key |
||
88 | * @param mixed $value |
||
89 | */ |
||
90 | 1 | public function set($key, $value) |
|
91 | { |
||
92 | 1 | list($namespace, $group, $item) = $this->parseKey($key); |
|
93 | |||
94 | 1 | $collection = $this->getCollection($group, $namespace); |
|
95 | |||
96 | // We'll need to go ahead and lazy load each configuration groups even when |
||
97 | // we're just setting a configuration item so that the set item does not |
||
98 | // get overwritten if a different item in the group is requested later. |
||
99 | 1 | $this->load($group, $namespace, $collection); |
|
100 | |||
101 | 1 | if (is_null($item)) { |
|
102 | $this->items[$collection] = $value; |
||
103 | } else { |
||
104 | 1 | array_set($this->items[$collection], $item, $value); |
|
105 | } |
||
106 | 1 | } |
|
107 | |||
108 | /** |
||
109 | * Load the configuration group for the key. |
||
110 | * |
||
111 | * @param string $group |
||
112 | * @param string $namespace |
||
113 | * @param string $collection |
||
114 | */ |
||
115 | 5 | protected function load($group, $namespace, $collection) |
|
116 | { |
||
117 | // If we've already loaded this collection, we will just bail out since we do |
||
118 | // not want to load it again. Once items are loaded a first time they will |
||
119 | // stay kept in memory within this class and not loaded from disk again. |
||
120 | 5 | if (isset($this->items[$collection])) { |
|
121 | 4 | return; |
|
122 | } |
||
123 | |||
124 | 5 | $items = $this->loader->load($group, $namespace); |
|
125 | |||
126 | 5 | $this->items[$collection] = $items; |
|
127 | 5 | } |
|
128 | |||
129 | /** |
||
130 | * Get the configuration namespace for a package. |
||
131 | * |
||
132 | * @param string $package |
||
133 | * @param string $namespace |
||
134 | * |
||
135 | * @return string |
||
136 | */ |
||
137 | protected function getPackageNamespace($package, $namespace) |
||
138 | { |
||
139 | if (is_null($namespace)) { |
||
140 | list($vendor, $namespace) = explode('/', $package); |
||
141 | } |
||
142 | |||
143 | return $namespace; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Get the collection identifier. |
||
148 | * |
||
149 | * @param string $group |
||
150 | * @param string $namespace |
||
151 | * |
||
152 | * @return string |
||
153 | */ |
||
154 | 5 | protected function getCollection($group, $namespace = null) |
|
155 | { |
||
156 | 5 | $namespace = $namespace ?: '*'; |
|
157 | |||
158 | 5 | return $namespace.'::'.$group; |
|
159 | } |
||
160 | |||
161 | /** |
||
162 | * Add a new namespace to the loader. |
||
163 | * |
||
164 | * @param string $namespace |
||
165 | * @param string $hint |
||
166 | */ |
||
167 | public function addNamespace($namespace, $hint) |
||
171 | |||
172 | /** |
||
173 | * Returns all registered namespaces with the config |
||
174 | * loader. |
||
175 | * |
||
176 | * @return array |
||
177 | */ |
||
178 | public function getNamespaces() |
||
182 | |||
183 | /** |
||
184 | * Get the loader implementation. |
||
185 | * |
||
186 | * @return \LaravelPlus\Extension\Repository\LoaderInterface |
||
187 | */ |
||
188 | 6 | public function getLoader() |
|
192 | |||
193 | /** |
||
194 | * Set the loader implementation. |
||
195 | * |
||
196 | * @param \LaravelPlus\Extension\Repository\LoaderInterface $loader |
||
197 | */ |
||
198 | public function setLoader(LoaderInterface $loader) |
||
202 | |||
203 | /** |
||
204 | * Get all of the configuration items. |
||
205 | * |
||
206 | * @return array |
||
207 | */ |
||
208 | public function getItems() |
||
212 | |||
213 | /** |
||
214 | * Determine if the given configuration option exists. |
||
215 | * |
||
216 | * @param string $key |
||
217 | * |
||
218 | * @return bool |
||
219 | */ |
||
220 | public function offsetExists($key) |
||
224 | |||
225 | /** |
||
226 | * Get a configuration option. |
||
227 | * |
||
228 | * @param string $key |
||
229 | * |
||
230 | * @return mixed |
||
231 | */ |
||
232 | public function offsetGet($key) |
||
236 | |||
237 | /** |
||
238 | * Set a configuration option. |
||
239 | * |
||
240 | * @param string $key |
||
241 | * @param mixed $value |
||
242 | */ |
||
243 | public function offsetSet($key, $value) |
||
247 | |||
248 | /** |
||
249 | * Unset a configuration option. |
||
250 | * |
||
251 | * @param string $key |
||
252 | */ |
||
253 | public function offsetUnset($key) |
||
257 | } |
||
258 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..