Skip to content

Commit

Permalink
Fixed retrieval fo multiple keys with default values via `PersistentR…
Browse files Browse the repository at this point in the history
…epository::get()`
  • Loading branch information
klimov-paul committed Jun 13, 2019
1 parent 45c2bd3 commit 780d3e2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Laravel Persistent Configuration Repository
===========================================

1.0.2 under development
-----------------------

- Bug #3: Fixed retrieval fo multiple keys with default values via `PersistentRepository::get()` (klimov-paul)


1.0.1, June 11, 2019
--------------------

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ present in the source config repository will remain as it is.

PersistentRepository fully decorates any config repository, matching [[\Illuminate\Contracts\Config\Repository]] and can substitute [[\Illuminate\Config\Repository]] instance.
In particular this allows you to substitute regular Laravel config by [[\Illuminatech\Config\PersistentRepository]] instance,
applying configuration from database to the entire application. You can so in your `AppServiceProvider` class. For example:
applying configuration from database to the entire application. You can do so in your `AppServiceProvider` class. For example:

```php
<?php
Expand Down
8 changes: 5 additions & 3 deletions src/PersistentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,12 +441,14 @@ private function isPersistentKey($candidateKey): bool
$candidateKey = [$candidateKey];
}

foreach ($candidateKey as $key) {
foreach ($candidateKey as $key => $value) {
$searchKey = (is_numeric($key)) ? $value : $key;

foreach ($this->getItemKeys() as $persistentKey) {
$key = $key.'.';
$searchKey = $searchKey.'.';
$persistentKey = $persistentKey.'.';

if (strncmp($key, $persistentKey, min(strlen($key), strlen($persistentKey))) === 0) {
if (strncmp($searchKey, $persistentKey, min(strlen($searchKey), strlen($persistentKey))) === 0) {
return true;
}
}
Expand Down
5 changes: 5 additions & 0 deletions tests/PersistentRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ public function testConfigRepositoryContract()
$this->assertTrue($this->persistentRepository->has('test.bar'));

$this->assertSame($this->repository->get(['origin.foo', 'origin.bar']), $this->persistentRepository->get(['origin.foo', 'origin.bar']));
$this->assertSame($this->repository->get(['origin.foo' => 'default']), $this->persistentRepository->get(['origin.foo' => 'default']));

$this->persistentRepository->set('new.bar', 'new bar');
$this->assertSame('new bar', $this->repository->get('new.bar'));
Expand Down Expand Up @@ -382,6 +383,10 @@ public function dataProviderLazyRestore(): array
['bar.block.nested', true],
[['foo.name', 'foo.another'], true],
[['foo.another', 'foo.name'], true],
[['bar.block' => ['default']], true],
[['another.block' => ['default']], false],
[['bar.block' => ['default' => 'value']], true],
[['another.block' => ['default' => 'value']], false],
];
}

Expand Down

0 comments on commit 780d3e2

Please sign in to comment.