Skip to content

Commit a5b4ca9

Browse files
committed
Model design, fixes
1 parent 5107201 commit a5b4ca9

File tree

6 files changed

+179
-6
lines changed

6 files changed

+179
-6
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
"laravel": {
2727
"providers": [
2828
"SoftinkLab\\LaravelKeyvalueStorage\\KeyValueStorageServiceProvider"
29-
]
29+
],
30+
"aliases": {
31+
"KVOption": "SoftinkLab\\LaravelKeyvalueStorage\\KeyValueStorageServiceProvider\\Facades\\KVOption"
32+
}
3033
}
3134
}
3235
}

config/kvstorage.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Storage Type
8+
|--------------------------------------------------------------------------
9+
|
10+
| You can define the storage mechanism for your key values. We support
11+
| database and json file storage.
12+
|
13+
| Supported: "database", "file"
14+
|
15+
*/
16+
17+
'method' => 'database',
18+
19+
/*
20+
|--------------------------------------------------------------------------
21+
| File Path
22+
|--------------------------------------------------------------------------
23+
|
24+
| If you select file storage, you can specify a path to store your file.
25+
| Leading slash is required. Folder should be in "storage/app/" folder.
26+
|
27+
| Ex: "kvstorage/" gives this path -> "storage/app/kvstorage/"
28+
|
29+
*/
30+
31+
'path' => "kvstorage/",
32+
];

database/migrations/2020_03_22_100000_create_kv_options_table.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use Illuminate\Database\Schema\Blueprint;
55
use Illuminate\Support\Facades\Schema;
66

7-
class CreateKvOptionsTable extends Migration
7+
class CreateKvStorageTable extends Migration
88
{
99
/**
1010
* Run the migrations.
@@ -13,11 +13,11 @@ class CreateKvOptionsTable extends Migration
1313
*/
1414
public function up()
1515
{
16-
Schema::create('kv_options', function (Blueprint $table) {
16+
Schema::create('kv_storage', function (Blueprint $table) {
1717
$table->bigIncrements('id');
1818
$table->string('key')->unique();
1919
$table->text('value');
20-
$table->text('comment');
20+
$table->text('comment')->nullable();
2121
});
2222
}
2323

@@ -28,6 +28,6 @@ public function up()
2828
*/
2929
public function down()
3030
{
31-
Schema::dropIfExists('kv_options');
31+
Schema::dropIfExists('kv_storage');
3232
}
3333
}

src/Facades/KVOption.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace SoftinkLab\LaravelKeyvalueStorage\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
class KVOption extends Facade
8+
{
9+
/**
10+
* Get the registered name of the component.
11+
*
12+
* @return string
13+
*/
14+
protected static function getFacadeAccessor()
15+
{
16+
return 'kvoption';
17+
}
18+
}

src/KVOption.php

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
namespace SoftinkLab\LaravelKeyvalueStorage;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class KVOption extends Model
8+
{
9+
/**
10+
* Indicates if the model should be timestamped.
11+
*
12+
* @var bool
13+
*/
14+
public $timestamps = false;
15+
16+
/**
17+
* The table associated with the model.
18+
*
19+
* @var string
20+
*/
21+
protected $table = 'kv_storage';
22+
23+
/**
24+
* The attributes that are mass assignable.
25+
*
26+
* @var [type]
27+
*/
28+
protected $fillable = [
29+
'key',
30+
'value',
31+
'comment',
32+
];
33+
34+
/**
35+
* Determine if the given key exists.
36+
*
37+
* @param string $key
38+
* @return bool
39+
*/
40+
public function exists($key)
41+
{
42+
return $this->where('key', $key)->exists();
43+
}
44+
45+
/**
46+
* Get the specified option by key.
47+
*
48+
* @param string $key
49+
* @return mixed
50+
*/
51+
public function get($key)
52+
{
53+
if ($option = $this->where('key', $key)->first()) {
54+
return $option->value;
55+
}
56+
57+
return null;
58+
}
59+
60+
/**
61+
* Set a given option.
62+
*
63+
* @param string $key
64+
* @param string $comment
65+
* @param mixed $value
66+
* @return void
67+
*/
68+
public function set($key, $value, $comment)
69+
{
70+
$this->updateOrCreate(
71+
['key' => $key],
72+
['value' => $value, 'comment' => $comment]
73+
);
74+
}
75+
76+
/**
77+
* Set given options of array.
78+
*
79+
* @param array $array
80+
* @return void
81+
*/
82+
public function setArray($array)
83+
{
84+
foreach ($array as $option) {
85+
// Check if comment is available.
86+
if (count($option) == 2){
87+
$this->updateOrCreate(
88+
['key' => $option[0]],
89+
['value' => $option[1]]
90+
);
91+
}else{
92+
$this->updateOrCreate(
93+
['key' => $option[0]],
94+
['value' => $option[1], 'comment' => $option[2]]
95+
);
96+
}
97+
}
98+
}
99+
100+
/**
101+
* Delete the specified option.
102+
*
103+
* @param string $key
104+
* @return bool
105+
*/
106+
public function remove($key)
107+
{
108+
return (bool) $this->where('key', $key)->delete();
109+
}
110+
111+
}

src/KeyValueStorageServiceProvider.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,20 @@ class KeyValueStorageServiceProvider extends ServiceProvider
88
{
99
public function boot()
1010
{
11+
// Database Table
1112
$this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
13+
14+
// Configurations
15+
$this->publishes([
16+
__DIR__ . '/../config/kvstorage.php' => config_path('kvstorage.php'),
17+
]);
1218
}
1319

20+
/**
21+
* Register the application services.
22+
*/
1423
public function register()
1524
{
16-
25+
$this->app->bind('kvoption', KVOption::class);
1726
}
1827
}

0 commit comments

Comments
 (0)