Skip to content

Commit ff4b8a4

Browse files
authored
Allows simpler construction of stores by passing a string (#60)
1 parent e0df164 commit ff4b8a4

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,10 @@ object, and an implementation of the `\Sven\FileConfig\Drivers\Driver` interface
4848
`Json` driver in the examples.
4949

5050
```php
51-
use Sven\FileConfig\File;
5251
use Sven\FileConfig\Store;
5352
use Sven\FileConfig\Drivers\Json;
5453

55-
$file = new File('/path/to/file.json');
56-
$config = new Store($file, new Json());
54+
$config = new Store('/path/to/file.json', new Json());
5755
```
5856

5957
You can interact with your newly created `$config` object via the `get`, `set`, and `delete`

src/Store.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
class Store
88
{
99
protected array $config;
10+
protected File $file;
1011

11-
public function __construct(protected File $file, protected Driver $driver)
12+
public function __construct(string|File $file, protected Driver $driver)
1213
{
13-
$this->config = $driver->import($file->contents());
14+
$this->file = is_string($file) ? new File($file) : $file;
15+
16+
$this->config = $driver->import($this->file->contents());
1417
}
1518

1619
public function get($key, $default = null)

tests/StoreTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,14 @@ public function it_can_get_all_values_from_the_config(): void
9696
$this->assertEquals('bar', $values['foo']);
9797
$this->assertEquals('def', $values['abc']);
9898
}
99+
100+
/** @test */
101+
public function it_allows_construction_from_string(): void
102+
{
103+
$this->create('test.json', '{"foo":"bar"}');
104+
105+
$store = new Store(__DIR__.'/'.self::TEMP_DIRECTORY.'/test.json', new Json());
106+
107+
$this->assertEquals('bar', $store->get('foo'));
108+
}
99109
}

0 commit comments

Comments
 (0)