Skip to content

Commit da2d629

Browse files
author
lizhuangzhuang
committed
first commit
0 parents  commit da2d629

15 files changed

+558
-0
lines changed

.editorconfig

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at http://editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
indent_size = 4
9+
indent_style = space
10+
end_of_line = lf
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true
13+
14+
[*.md]
15+
trim_trailing_whitespace = false

.gitattributes

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Path-based git attributes
2+
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html
3+
4+
# Ignore all test and documentation with "export-ignore".
5+
/.gitattributes export-ignore
6+
/.gitignore export-ignore
7+
/.editorconfig export-ignore
8+
/.travis.yml export-ignore
9+
/.styleci.yml export-ignore
10+
/phpunit.xml.dist export-ignore
11+
/tests export-ignore

.gitignore

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Crap
2+
*.log
3+
.sass-cache
4+
.Spotlight-V100
5+
.Trashes
6+
.DS_Store
7+
.DS_Store?
8+
ehthumbs.db
9+
Thumbs.db
10+
11+
# IDE
12+
*.sublime-*
13+
*.idea
14+
.idea
15+
16+
# Project
17+
build
18+
composer.lock
19+
docs
20+
vendor
21+
.phpunit.result.cache

.styleci.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
preset: laravel
2+
3+
disabled:
4+
- single_class_element_per_statement

.travis.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
language: php
2+
3+
php:
4+
- 7.1
5+
- 7.2
6+
7+
env:
8+
matrix:
9+
- COMPOSER_FLAGS=""
10+
11+
before_script:
12+
- travis_retry composer self-update
13+
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source
14+
15+
script:
16+
- php vendor/bin/phpunit --coverage-text

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Laravel Module Options
2+
3+
## Installation
4+
5+
```bash
6+
composer require reallyli/laravel-module-options
7+
```

composer.json

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"name": "reallyli/laravel-module-options",
3+
"description": "Kv storage based on modular development",
4+
"keywords": [
5+
"laravel-module-options",
6+
"key-value storage from database"
7+
],
8+
"homepage": "https://github.com/reallyli/laravel-module-options",
9+
"license": "MIT",
10+
"authors": [
11+
{
12+
"name": "reallyli",
13+
"email": "[email protected]",
14+
"role": "Developer"
15+
}
16+
],
17+
"require": {
18+
"php": ">=5.6",
19+
"illuminate/support": "^5.4|^6.0",
20+
"illuminate/database": "^5.4|^6.0"
21+
},
22+
"require-dev": {
23+
"orchestra/database": "^3.7",
24+
"orchestra/testbench": "~3.7"
25+
},
26+
"autoload": {
27+
"psr-4": {
28+
"Reallyli\\Options\\": "src"
29+
}
30+
},
31+
"autoload-dev": {
32+
"psr-4": {
33+
"Reallyli\\Options\\Test\\": "tests"
34+
}
35+
},
36+
"scripts": {
37+
"test": "vendor/bin/phpunit"
38+
},
39+
"config": {
40+
"sort-packages": true
41+
},
42+
"extra": {
43+
"laravel": {
44+
"providers": [
45+
"Reallyli\\Options\\OptionsServiceProvider"
46+
],
47+
"aliases": {
48+
"Option": "Reallyli\\Options\\Facade\\Option"
49+
}
50+
}
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateOptionsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('options', function (Blueprint $table) {
17+
$table->increments('id');
18+
$table->string('key')->unique();
19+
$table->text('value');
20+
$table->string('comment')->nullable();
21+
$table->string('module', 40)->index()->nullable();
22+
$table->timestamps();
23+
$table->softDeletes();
24+
});
25+
}
26+
27+
/**
28+
* Reverse the migrations.
29+
*
30+
* @return void
31+
*/
32+
public function down()
33+
{
34+
Schema::dropIfExists('options');
35+
}
36+
}

phpunit.xml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
verbose="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false">
12+
<testsuites>
13+
<testsuite name="Reallyli Test Suite">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">src/</directory>
20+
</whitelist>
21+
</filter>
22+
</phpunit>

src/Console/OptionSetCommand.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Reallyli\Options\Console;
4+
5+
use Reallyli\Options\Option as Option;
6+
use Illuminate\Console\Command;
7+
8+
class OptionSetCommand extends Command
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'option:set
16+
{module : Option module}
17+
{key : Option key}
18+
{value : Option value}
19+
{comment : Option comment}';
20+
21+
/**
22+
* The console command description.
23+
*
24+
* @var string
25+
*/
26+
protected $description = 'Create an option.';
27+
28+
/**
29+
* Execute the console command.
30+
*
31+
* @return mixed
32+
*/
33+
public function handle()
34+
{
35+
Option::set(
36+
$this->argument('module'),
37+
$this->argument('key'),
38+
$this->argument('value'),
39+
$this->argument('comment')
40+
);
41+
42+
$this->info('Option added.');
43+
}
44+
}

src/Facade/Option.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Reallyli\Options\Facade;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
/**
8+
* @method static string get($module, $key, $default = null)
9+
* @method static string set($module, $key, $value = null, $comment = null)
10+
* @method static string exists($module, $key)
11+
* @method static string remove($module, $key)
12+
* @method static string increase($module, $key, $step =1)
13+
* @method static string decrease($module, $key, $step =1)
14+
*
15+
*/
16+
class Option extends Facade
17+
{
18+
/**
19+
* Get the registered name of the component.
20+
*
21+
* @return string
22+
*/
23+
protected static function getFacadeAccessor()
24+
{
25+
return 'option';
26+
}
27+
}

src/Option.php

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
3+
namespace Reallyli\Options;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\SoftDeletes;
7+
8+
class Option extends Model
9+
{
10+
use SoftDeletes;
11+
12+
/**
13+
* The attributes that are mass assignable.
14+
*
15+
* @var [type]
16+
*/
17+
protected $fillable = [
18+
'key',
19+
'value',
20+
'comment',
21+
'module'
22+
];
23+
24+
/**
25+
* Determine if the given option value exists.
26+
*
27+
* @param mixed $module
28+
* @param string $key
29+
* @return bool
30+
*/
31+
public function exists($module, $key)
32+
{
33+
return self::where([
34+
'module' => $module,
35+
'key' => $key
36+
])->exists();
37+
}
38+
39+
/**
40+
* Get the specified option value.
41+
*
42+
* @param mixed $module
43+
* @param mixed $key
44+
* @param mixed $default
45+
* @return mixed
46+
*/
47+
public function get($module, $key, $default = null)
48+
{
49+
$option = self::where([
50+
'module' => $module,
51+
'key' => $key
52+
])->first();
53+
54+
return $option ? $option->value : $default;
55+
}
56+
57+
/**
58+
* Set a given option value.
59+
*
60+
* @param mixed $module
61+
* @param array|string $key
62+
* @param mixed $value
63+
* @param mixed $comment
64+
* @return void
65+
*/
66+
public function set($module, $key, $value = null, $comment = null)
67+
{
68+
$keys = is_array($key) ? $key : [$key => $value];
69+
70+
$data = [
71+
'module' => $module,
72+
'comment' => $comment
73+
];
74+
75+
foreach ($keys as $key => $value) {
76+
$data['value'] = $value;
77+
78+
self::updateOrCreate(['key' => $key, 'module' => $module], $data);
79+
}
80+
}
81+
82+
/**
83+
* Remove/delete the specified option value.
84+
*
85+
* @param mixed $module
86+
* @param string $key
87+
* @return bool
88+
*/
89+
public function remove($module, $key)
90+
{
91+
return (bool) self::where([
92+
'module' => $module,
93+
'key' => $key
94+
])->delete();
95+
}
96+
97+
/**
98+
* Increase the specified option value.
99+
*
100+
* @param mixed $module
101+
* @param string $key
102+
* @param int $step
103+
* @return mixed
104+
*/
105+
public function increase($module, $key, $step = 1)
106+
{
107+
$option = self::where([
108+
'module' => $module,
109+
'key' => $key
110+
])->first();
111+
112+
return $option ? $option->increment('value', $step) : null;
113+
}
114+
115+
/**
116+
* Decrease the specified option value.
117+
*
118+
* @param mixed $module
119+
* @param string $key
120+
* @param int $step
121+
* @return bool
122+
*/
123+
public function decrease($module, $key, $step = 1)
124+
{
125+
$option = self::where([
126+
'module' => $module,
127+
'key' => $key
128+
])->first();
129+
130+
return $option ? $option->decrement('value', $step) : null;
131+
}
132+
}

0 commit comments

Comments
 (0)