Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] Let config get many keys at once. #19770

Merged
merged 2 commits into from
Jun 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/Illuminate/Config/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,40 @@ public function has($key)
/**
* Get the specified configuration value.
*
* @param string $key
* @param array|string $key
* @param mixed $default
* @return mixed
*/
public function get($key, $default = null)
{
if (is_array($key)) {
return $this->getMany($key);
}

return Arr::get($this->items, $key, $default);
}

/**
* Get many configuration values.
*
* @param array $keys
* @return array
*/
public function getMany($keys)
{
$config = [];

foreach ($keys as $key => $default) {
if (is_numeric($key)) {
list($key, $default) = [$default, null];
}

$config[$key] = Arr::get($this->items, $key, $default);
}

return $config;
}

/**
* Set a given configuration value.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Contracts/Config/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function has($key);
/**
* Get the specified configuration value.
*
* @param string $key
* @param array|string $key
* @param mixed $default
* @return mixed
*/
Expand Down
54 changes: 54 additions & 0 deletions tests/Config/RepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function setUp()
$this->repository = new Repository($this->config = [
'foo' => 'bar',
'bar' => 'baz',
'baz' => 'bat',
'null' => null,
'associate' => [
'x' => 'xxx',
Expand All @@ -31,6 +32,9 @@ public function setUp()
'aaa',
'zzz',
],
'x' => [
'z' => 'zoo',
],
]);

parent::setUp();
Expand All @@ -56,6 +60,56 @@ public function testGet()
$this->assertSame('bar', $this->repository->get('foo'));
}

public function testGetWithArrayOfKeys()
{
$this->assertSame([
'foo' => 'bar',
'bar' => 'baz',
'none' => null,
], $this->repository->get([
'foo',
'bar',
'none',
]));

$this->assertSame([
'x.y' => 'default',
'x.z' => 'zoo',
'bar' => 'baz',
'baz' => 'bat',
], $this->repository->get([
'x.y' => 'default',
'x.z' => 'default',
'bar' => 'default',
'baz',
]));
}

public function testGetMany()
{
$this->assertSame([
'foo' => 'bar',
'bar' => 'baz',
'none' => null,
], $this->repository->getMany([
'foo',
'bar',
'none',
]));

$this->assertSame([
'x.y' => 'default',
'x.z' => 'zoo',
'bar' => 'baz',
'baz' => 'bat',
], $this->repository->getMany([
'x.y' => 'default',
'x.z' => 'default',
'bar' => 'default',
'baz',
]));
}

public function testGetWithDefault()
{
$this->assertSame('default', $this->repository->get('not-exist', 'default'));
Expand Down