Skip to content

Commit b7092de

Browse files
committed
fixed compoer require
2 parents bb6f4bf + 8a7cc96 commit b7092de

File tree

4 files changed

+96
-20
lines changed

4 files changed

+96
-20
lines changed

Diff for: helpers.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,18 @@
1010

1111
/**
1212
* @param $file
13+
*
1314
* @return array
1415
*/
1516
function load($file)
1617
{
1718
$extension = pathinfo($file, PATHINFO_EXTENSION);
1819

1920
switch ($extension) {
20-
case "ini":
21-
$config = parse_ini_file($file, true);;
21+
case 'ini':
22+
$config = parse_ini_file($file, true);
2223
break;
23-
case "yml":
24+
case 'yml':
2425
$config = Yaml::parse(file_get_contents($file));
2526
break;
2627
case 'json':
@@ -32,4 +33,4 @@ function load($file)
3233
}
3334

3435
return $config;
35-
}
36+
}

Diff for: src/Config.php

+49-9
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@
99

1010
namespace FastD\Config;
1111

12-
1312
use FastD\Utils\ArrayObject;
1413

1514
/**
16-
* Class Config
17-
*
18-
* @package FastD\Config
15+
* Class Config.
1916
*/
2017
class Config extends ArrayObject
2118
{
@@ -28,6 +25,7 @@ class Config extends ArrayObject
2825

2926
/**
3027
* Config constructor.
28+
*
3129
* @param $config
3230
* @param array $variables
3331
*/
@@ -40,6 +38,7 @@ public function __construct(array $config = [], array $variables = [])
4038

4139
/**
4240
* @param $file
41+
*
4342
* @return $this
4443
*/
4544
public function load($file)
@@ -53,12 +52,14 @@ public function load($file)
5352

5453
/**
5554
* @param $value
55+
*
5656
* @return string
5757
*/
5858
protected function replace($value)
5959
{
6060
if ('env' === substr($value, 0, 3)) {
6161
$env = substr($value, 4);
62+
6263
return env($env);
6364
}
6465

@@ -71,29 +72,68 @@ protected function replace($value)
7172

7273
/**
7374
* @param $key
74-
* @param null $default
75-
* @return mixed|null|string
75+
* @param $default
76+
*
77+
* @return mixed
7678
*/
7779
public function get($key, $default = null)
7880
{
7981
try {
8082
$value = $this->find($key);
83+
8184
return is_string($value) ? $this->replace($value) : $value;
8285
} catch (\Exception $exception) {
8386
return $default;
8487
}
8588
}
8689

90+
public function has($key)
91+
{
92+
try {
93+
$this->find($key);
94+
} catch (\Exception $exception) {
95+
return false;
96+
}
97+
98+
return true;
99+
}
100+
101+
public function set($key, $value)
102+
{
103+
if ($this->offsetExists($key)) {
104+
return parent::set($key, $value);
105+
}
106+
$keys = explode('.', $key);
107+
$firstDimension = array_shift($keys);
108+
109+
$data = [];
110+
if ($this->offsetExists($firstDimension)) {
111+
$data = $this->offsetGet($firstDimension);
112+
! is_array($data) && $data = [$data];
113+
}
114+
115+
$target = &$data;
116+
117+
foreach ($keys as $key) {
118+
$target = &$target[$key];
119+
}
120+
$target = $value;
121+
122+
return parent::set($firstDimension, $data);
123+
}
124+
87125
/**
88126
* @param $variable
127+
*
89128
* @return string
90129
*/
91130
protected function variable($variable)
92131
{
93132
return preg_replace_callback(sprintf('/%s(\w*\.*\w*)%s/', static::GLUE, static::GLUE), function ($match) {
94-
if (!$this->has($match[1])) {
133+
if ( ! $this->has($match[1])) {
95134
throw new \Exception($match[1]);
96135
}
136+
97137
return $this->variables[$match[1]];
98138
}, $variable);
99139
}
@@ -103,6 +143,6 @@ protected function variable($variable)
103143
*/
104144
public function all()
105145
{
106-
return (array) $this;
146+
return (array)$this;
107147
}
108-
}
148+
}

Diff for: tests/ConfigTest.php

+40-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* @link https://www.github.com/janhuang
77
* @link http://www.fast-d.cn/
88
*/
9-
109
use FastD\Config\Config;
1110

1211
class ConfigTest extends PHPUnit_Framework_TestCase
@@ -19,23 +18,59 @@ class ConfigTest extends PHPUnit_Framework_TestCase
1918
public function setUp()
2019
{
2120
$this->config = new Config([], [
22-
'name' => 'bar'
21+
'name' => 'bar',
2322
]);
2423
}
2524

2625
public function testLoad()
2726
{
28-
$this->config->load(__DIR__ . '/config/config.yml');
27+
$this->config->load(__DIR__.'/config/config.yml');
2928

3029
$this->assertEquals('yml', $this->config->find('foo'));
3130
$this->assertEquals([
32-
'foo' => 'yml'
31+
'foo' => 'yml',
3332
], $this->config->all());
3433
}
3534

3635
public function testVariable()
3736
{
38-
$this->config->load(__DIR__ . '/config/variable.yml');
37+
$this->config->load(__DIR__.'/config/variable.yml');
3938
$this->assertEquals('bar', $this->config->get('name'));
4039
}
40+
41+
public function testSetConfig()
42+
{
43+
$this->config->set('a', 'hello');
44+
$this->config->set('a.b.c', 'world');
45+
$this->assertEquals([
46+
'a' => [
47+
'hello',
48+
'b' => [
49+
'c' => 'world',
50+
],
51+
],
52+
], $this->config->all());
53+
$this->config->set('a.b', 'world');
54+
$this->assertEquals([
55+
'a' => [
56+
'hello',
57+
'b' => 'world',
58+
],
59+
], $this->config->all());
60+
}
61+
62+
public function testHasConfig()
63+
{
64+
$this->config->set('a', [
65+
'b' => [
66+
'c' => 'hello world',
67+
],
68+
]);
69+
$this->assertSame(true, $this->config->has('a'));
70+
$this->assertSame(true, $this->config->has('a.b'));
71+
$this->assertSame(true, $this->config->has('a.b.c'));
72+
$this->assertSame(false, $this->config->has('a.b.cd'));
73+
$this->assertSame(false, $this->config->has('b'));
74+
$this->assertSame(false, $this->config->has('b.c'));
75+
}
4176
}

Diff for: tests/HelperTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class HelperTest extends PHPUnit_Framework_TestCase
1111
{
1212
public function testLoad()
1313
{
14-
$this->assertEquals(load(__DIR__ . '/config/config.ini'), ['foo' => 'bar']);
15-
$this->assertEquals(load(__DIR__ . '/config/config.yml'), ['foo' => 'yml']);
14+
$this->assertEquals(load(__DIR__.'/config/config.ini'), ['foo' => 'bar']);
15+
$this->assertEquals(load(__DIR__.'/config/config.yml'), ['foo' => 'yml']);
1616
}
1717
}

0 commit comments

Comments
 (0)