Skip to content

Commit c89ae4f

Browse files
committed
wip
1 parent cda091c commit c89ae4f

File tree

1 file changed

+223
-0
lines changed

1 file changed

+223
-0
lines changed

src/Repository.php

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
<?php
2+
3+
namespace Ofcold\Component\Config;
4+
5+
use ArrayAccess;
6+
use Illuminate\Contracts\Config\Repository as RepositoryInterface;
7+
use Illuminate\Filesystem\Filesystem;
8+
use Illuminate\Support\Arr;
9+
use Ofcold\Component\Support\DirectoryFiles;
10+
use SplFileInfo;
11+
12+
class Repository implements ArrayAccess, RepositoryInterface
13+
{
14+
/**
15+
* All of the configuration items.
16+
*
17+
* @var array
18+
*/
19+
protected $items = [];
20+
21+
/**
22+
* Create a new configuration repository.
23+
*
24+
* @param array $items
25+
* @return void
26+
*/
27+
public function __construct(array $items = [])
28+
{
29+
$this->items = $items;
30+
}
31+
32+
/**
33+
* Set the configuration items.
34+
*
35+
* @param array $items
36+
*/
37+
public function setItems(array $items = [])
38+
{
39+
$this->items = $items;
40+
41+
return $this;
42+
}
43+
44+
/**
45+
* Determine if the given configuration value exists.
46+
*
47+
* @param string $key
48+
*
49+
* @return bool
50+
*/
51+
public function has($key)
52+
{
53+
return Arr::has($this->items, $key);
54+
}
55+
56+
/**
57+
* Get the specified configuration value.
58+
*
59+
* @param array|string $key
60+
* @param mixed $default
61+
*
62+
* @return mixed
63+
*/
64+
public function get($key, $default = null)
65+
{
66+
if (is_array($key)) {
67+
return $this->getMany($key);
68+
}
69+
70+
return Arr::get($this->items, $key, $default);
71+
}
72+
73+
/**
74+
* Add a namespace to configuration.
75+
*
76+
* @param string $directory
77+
* @param ?string $namespace
78+
*
79+
* @param $directory
80+
*/
81+
public function addNamespace(string $directory, ?string $namespace = null): void
82+
{
83+
$files = DirectoryFiles::make($directory);
84+
85+
$namespace = is_null($namespace) || trim($namespace) === '' ? '' : $namespace.'::';
86+
87+
foreach ($files as $key => $path) {
88+
$this->set($namespace.$key, require $path);
89+
}
90+
}
91+
92+
/**
93+
* Get many configuration values.
94+
*
95+
* @param array $keys
96+
*
97+
* @return array
98+
*/
99+
public function getMany(array $keys): array
100+
{
101+
$config = [];
102+
103+
foreach ($keys as $key => $default) {
104+
if (is_numeric($key)) {
105+
[$key, $default] = [$default, null];
106+
}
107+
108+
$config[$key] = Arr::get($this->items, $key, $default);
109+
}
110+
111+
return $config;
112+
}
113+
114+
/**
115+
* Set a given configuration value.
116+
*
117+
* @param array|string $key
118+
* @param mixed $value
119+
*
120+
* @return void
121+
*/
122+
public function set($key, $value = null): void
123+
{
124+
$keys = is_array($key) ? $key : [$key => $value];
125+
126+
foreach ($keys as $key => $value) {
127+
Arr::set($this->items, $key, $value);
128+
}
129+
}
130+
131+
/**
132+
* Prepend a value onto an array configuration value.
133+
*
134+
* @param string $key
135+
* @param mixed $value
136+
*
137+
* @return void
138+
*/
139+
public function prepend($key, $value): void
140+
{
141+
$array = $this->get($key);
142+
143+
array_unshift($array, $value);
144+
145+
$this->set($key, $array);
146+
}
147+
148+
/**
149+
* Push a value onto an array configuration value.
150+
*
151+
* @param string $key
152+
* @param mixed $value
153+
*
154+
* @return void
155+
*/
156+
public function push($key, $value): void
157+
{
158+
$array = $this->get($key);
159+
160+
$array[] = $value;
161+
162+
$this->set($key, $array);
163+
}
164+
165+
/**
166+
* Get all of the configuration items for the application.
167+
*
168+
* @return array
169+
*/
170+
public function all(): array
171+
{
172+
return $this->items;
173+
}
174+
175+
/**
176+
* Determine if the given configuration option exists.
177+
*
178+
* @param string $key
179+
*
180+
* @return bool
181+
*/
182+
public function offsetExists($key)
183+
{
184+
return $this->has($key);
185+
}
186+
187+
/**
188+
* Get a configuration option.
189+
*
190+
* @param string $key
191+
*
192+
* @return mixed
193+
*/
194+
public function offsetGet($key)
195+
{
196+
return $this->get($key);
197+
}
198+
199+
/**
200+
* Set a configuration option.
201+
*
202+
* @param string $key
203+
* @param mixed $value
204+
*
205+
* @return void
206+
*/
207+
public function offsetSet($key, $value)
208+
{
209+
$this->set($key, $value);
210+
}
211+
212+
/**
213+
* Unset a configuration option.
214+
*
215+
* @param string $key
216+
*
217+
* @return void
218+
*/
219+
public function offsetUnset($key)
220+
{
221+
$this->set($key, null);
222+
}
223+
}

0 commit comments

Comments
 (0)