Skip to content

Commit e582e85

Browse files
committed
JSON Data storage
1 parent 0ce71f7 commit e582e85

File tree

5 files changed

+521
-2
lines changed

5 files changed

+521
-2
lines changed

config/kvstorage.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@
1616

1717
'method' => 'database',
1818

19+
/*
20+
|--------------------------------------------------------------------------
21+
| Storage Driver
22+
|--------------------------------------------------------------------------
23+
|
24+
| Specify the filesystem driver for your key file storage in "file" mode.
25+
| You need to configure your storage configuration before use.
26+
|
27+
| Supported: "local", "public", "s3"
28+
| Refer "config/filesystem.php" for more more file systems.
29+
|
30+
*/
31+
32+
'disk' => "local",
33+
1934
/*
2035
|--------------------------------------------------------------------------
2136
| File Path

src/KVOption.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function get($key, $default = null)
6666
* @param mixed $value
6767
* @return void
6868
*/
69-
public function set($key, $value, $comment)
69+
public function set($key, $value, $comment = null)
7070
{
7171
$this->updateOrCreate(
7272
['key' => $key],

src/KVOptionJSON.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
namespace SoftinkLab\LaravelKeyvalueStorage;
4+
5+
use Illuminate\Support\Facades\Storage;
6+
7+
class KVOptionJSON
8+
{
9+
protected $fileName = 'keyvalue-storage.json';
10+
protected $data = array();
11+
12+
public function __construct()
13+
{
14+
// Load file to array
15+
if (Storage::disk(config('kvstorage.disk'))->exists(config('kvstorage.path') . $this->fileName)){
16+
$contents = Storage::disk(config('kvstorage.disk'))->get(config('kvstorage.path') . $this->fileName);
17+
$this->data = json_decode($contents, true);
18+
}
19+
}
20+
21+
/**
22+
* Determine if the given key exists.
23+
*
24+
* @param string $key
25+
* @return bool
26+
*/
27+
public function exists($key) : bool
28+
{
29+
return array_key_exists($key, $this->data);
30+
}
31+
32+
/**
33+
* Get the specified option by key.
34+
*
35+
* @param string $key
36+
* @param mixed $default
37+
* @return mixed
38+
*/
39+
public function get($key, $default = null)
40+
{
41+
if (!array_key_exists($key, $this->data)) {
42+
return $default;
43+
}
44+
45+
return $this->data[$key];
46+
}
47+
48+
/**
49+
* Set a given option.
50+
*
51+
* @param string $key
52+
* @param string $comment
53+
* @param mixed $value
54+
* @return void
55+
*
56+
*/
57+
public function set($key, $value, $comment = null)
58+
{
59+
$this->data[$key] = $value;
60+
$this->setContent();
61+
}
62+
63+
/**
64+
* Set given options of array.
65+
*
66+
* @param array $array
67+
* @return void
68+
*/
69+
public function setArray($array)
70+
{
71+
// Multiple elements are present
72+
if (is_array($array[0])) {
73+
foreach ($array as $option) {
74+
$this->data[$option[0]] = $option[1];
75+
}
76+
} else {
77+
$option = $array;
78+
$this->data[$option[0]] = $option[1];
79+
}
80+
81+
$this->setContent();
82+
}
83+
84+
/**
85+
* Delete the specified option.
86+
*
87+
* @param string $key
88+
* @return bool
89+
*/
90+
public function remove($key)
91+
{
92+
unset($this->data[$key]);
93+
$this->setContent();
94+
95+
return true;
96+
}
97+
98+
/**
99+
* Store the updated array in file.
100+
*
101+
* @return void
102+
*/
103+
public function setContent()
104+
{
105+
Storage::disk(config('kvstorage.disk'))->put(config('kvstorage.path') . $this->fileName, json_encode($this->data));
106+
}
107+
}

src/KeyValueStorageServiceProvider.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ public function boot()
3030
*/
3131
public function register()
3232
{
33-
$this->app->bind('kvoption', KVOption::class);
33+
if (config('kvstorage.method') == 'database'){
34+
$this->app->bind('kvoption', KVOption::class);
35+
}else{
36+
$this->app->bind('kvoption', KVOptionJSON::class);
37+
}
3438

3539
$this->registerHelpers();
3640
}

0 commit comments

Comments
 (0)