-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 28662f1
Showing
34 changed files
with
913 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# composer | ||
composer.lock | ||
composer.phar | ||
vendor/ | ||
|
||
# phpunit | ||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
build: | ||
nodes: | ||
analysis: | ||
tests: | ||
override: | ||
- php-scrutinizer-run | ||
|
||
checks: | ||
php: true | ||
tools: | ||
php_code_coverage: | ||
enabled: true | ||
external_code_coverage: | ||
timeout: 600 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
preset: psr2 | ||
|
||
finder: | ||
exclude: | ||
- docs | ||
- vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
language: php | ||
|
||
php: | ||
- 7.1 | ||
- 7.2 | ||
- 7.3 | ||
|
||
# faster builds on new travis setup not using sudo | ||
sudo: false | ||
|
||
services: | ||
- mysql | ||
|
||
# cache vendor dirs | ||
cache: | ||
directories: | ||
- $HOME/.composer/cache | ||
|
||
install: | ||
- travis_retry composer self-update && composer --version | ||
- export PATH="$HOME/.composer/vendor/bin:$PATH" | ||
- travis_retry composer install --prefer-dist --no-interaction | ||
|
||
before_script: | ||
- mysql -e 'CREATE DATABASE test;' | ||
- php tests/yii migrate/fresh --interactive=0 | ||
|
||
script: | ||
- vendor/bin/codecept run --coverage --coverage-xml | ||
|
||
after_script: | ||
- wget -c https://scrutinizer-ci.com/ocular.phar | ||
- php ocular.phar code-coverage:upload --format=php-clover tests/_output/coverage.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
v1.0.0 | ||
------ | ||
First release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
BSD 3-Clause License | ||
|
||
Copyright (c) 2019, Razon Yang | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
1. Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
|
||
2. Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
|
||
3. Neither the name of the copyright holder nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
Yii2 Setting Manager | ||
==================== | ||
|
||
[![Build Status](https://travis-ci.org/razonyang/yii2-setting.svg?branch=master)](https://travis-ci.org/razonyang/yii2-setting) | ||
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/razonyang/yii2-setting/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/razonyang/yii2-setting/?branch=master) | ||
[![Code Coverage](https://scrutinizer-ci.com/g/razonyang/yii2-setting/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/razonyang/yii2-setting/?branch=master) | ||
[![Latest Stable Version](https://img.shields.io/packagist/v/razonyang/yii2-setting.svg)](https://packagist.org/packages/razonyang/yii2-setting) | ||
[![Total Downloads](https://img.shields.io/packagist/dt/razonyang/yii2-setting.svg)](https://packagist.org/packages/razonyang/yii2-setting) | ||
[![LICENSE](https://img.shields.io/github/license/razonyang/yii2-setting)](LICENSE) | ||
|
||
|
||
Installation | ||
------------ | ||
|
||
``` | ||
composer require razonyang/yii2-setting | ||
``` | ||
|
||
Usage | ||
----- | ||
|
||
Configuration: | ||
|
||
```php | ||
return [ | ||
// console | ||
'controllerMap' => [ | ||
'migrate' => [ | ||
'migrationNamespaces' => [ | ||
'RazonYang\Yii2\Setting\Migration', | ||
], | ||
], | ||
], | ||
|
||
'components' => [ | ||
// common | ||
'settingManager' => [ | ||
'class' => \RazonYang\Yii2\Setting\DbManager::class, | ||
'enableCache' => YII_DEBUG ? false : true, | ||
'cache' => 'cache', | ||
'mutex' => 'mutex', | ||
'duration' => 600, | ||
'db' => 'db', | ||
'settingTable' => '{{%setting}}', | ||
], | ||
], | ||
]; | ||
``` | ||
|
||
Migration: | ||
|
||
``` | ||
$ yii migrate | ||
``` | ||
|
||
```php | ||
$settingManager = Yii::$app->get('settingManager'); | ||
|
||
// fetch by ID | ||
$value = $settingManager->get($id, $defaultValue); // defaultValue is optional | ||
|
||
// fetchs all settings | ||
$settings = $settingManager->getAll(); | ||
|
||
// flush cache | ||
$settingManager->flushCache(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
paths: | ||
tests: tests | ||
output: tests/_output | ||
data: tests/_data | ||
support: tests/_support | ||
envs: tests/_envs | ||
actor_suffix: Tester | ||
settings: | ||
bootstrap: _bootstrap.php | ||
extensions: | ||
enabled: | ||
- Codeception\Extension\RunFailed | ||
coverage: | ||
enabled: true | ||
include: | ||
- src/* | ||
exclude: | ||
- src/Migration/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"name": "razonyang/yii2-setting", | ||
"description": "Yii2 Setting Manager", | ||
"type": "yii2-extension", | ||
"keywords": ["yii2","extension","setting manager"], | ||
"license": "BSD-3-Clause", | ||
"authors": [ | ||
{ | ||
"name": "Razon Yang", | ||
"email": "razonyang@gmail.com" | ||
} | ||
], | ||
"minimum-stability": "dev", | ||
"prefer-stable": true, | ||
"require": { | ||
"php": "^7.1", | ||
"yiisoft/yii2": "~2.0.13" | ||
}, | ||
"require-dev": { | ||
"codeception/codeception": "^3.0", | ||
"codeception/verify": "^1.1", | ||
"phpunit/phpunit": "^7" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"RazonYang\\Yii2\\Setting\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"RazonYang\\Yii2\\Setting\\Tests\\": "tests/" | ||
} | ||
}, | ||
"repositories": [ | ||
{ | ||
"type": "composer", | ||
"url": "https://asset-packagist.org" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
namespace RazonYang\Yii2\Setting; | ||
|
||
use yii\db\Connection; | ||
use yii\di\Instance; | ||
use yii\helpers\ArrayHelper; | ||
|
||
class DbManager extends Manager | ||
{ | ||
/** | ||
* @var Connection $db | ||
*/ | ||
public $db = 'db'; | ||
|
||
/** | ||
* @var string $modelClass | ||
*/ | ||
public $settingTable = '{{%setting}}'; | ||
|
||
public function init() | ||
{ | ||
parent::init(); | ||
|
||
$this->db = Instance::ensure($this->db, Connection::class); | ||
} | ||
|
||
public function load(): array | ||
{ | ||
$tableName = $this->db->quoteTableName($this->settingTable); | ||
$sql = "SELECT id, value FROM $tableName"; | ||
$rows = $this->db->createCommand($sql)->queryAll(); | ||
return ArrayHelper::map($rows, 'id', 'value'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
namespace RazonYang\Yii2\Setting; | ||
|
||
use yii\base\Component; | ||
use yii\caching\CacheInterface; | ||
use yii\di\Instance; | ||
use yii\mutex\Mutex; | ||
|
||
/** | ||
* Manager is the base manager. | ||
*/ | ||
abstract class Manager extends Component implements ManagerInterface | ||
{ | ||
/** | ||
* @var CacheInterface $cache | ||
*/ | ||
public $cache = 'cache'; | ||
|
||
/** | ||
* @var bool $enableCache whether enable caching. | ||
*/ | ||
public $enableCache = true; | ||
|
||
/** | ||
* @var Mutex $mutex | ||
*/ | ||
public $mutex = 'mutex'; | ||
|
||
/** | ||
* @var string $cacheKey | ||
*/ | ||
public $cacheKey = self::class; | ||
|
||
/** | ||
* @var int $duration | ||
*/ | ||
public $duration = 600; | ||
|
||
public function init() | ||
{ | ||
parent::init(); | ||
|
||
$this->cache = Instance::ensure($this->cache, CacheInterface::class); | ||
$this->mutex = Instance::ensure($this->mutex, Mutex::class); | ||
} | ||
|
||
private $data; | ||
|
||
public function get(string $id, ?string $defaultValue = null): ?string | ||
{ | ||
$data = $this->getAll(); | ||
return $data[$id] ?? $defaultValue; | ||
} | ||
|
||
public function getAll(): array | ||
{ | ||
if ($this->data !== null) { | ||
return $this->data; | ||
} | ||
|
||
// retrieves from cache | ||
if ($this->enableCache && ($this->data = $this->cache->get($this->cacheKey)) !== false) { | ||
return $this->data; | ||
} | ||
|
||
// retrieves all data | ||
$this->data = $this->load(); | ||
|
||
// cache | ||
if ($this->enableCache) { | ||
$lock = $this->cacheKey; | ||
// acquire lock avoid concurrence | ||
if ($this->mutex->acquire($lock, 0)) { | ||
$this->cache->set($this->cacheKey, $this->data, $this->duration); | ||
} | ||
} | ||
|
||
return $this->data; | ||
} | ||
|
||
/** | ||
* Loads all data as array that mapping from id to value. | ||
* | ||
* @return array | ||
*/ | ||
abstract protected function load(): array; | ||
|
||
public function flushCache(): bool | ||
{ | ||
$this->data = null; | ||
// flush cache | ||
return $this->cache->delete($this->cacheKey); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
namespace RazonYang\Yii2\Setting; | ||
|
||
interface ManagerInterface | ||
{ | ||
/** | ||
* Gets data by the given id. | ||
* | ||
* @param string $id | ||
* @param null|string $defaultValue | ||
* | ||
* @return string|nul returns the data if exists, otherwise default value or null. | ||
*/ | ||
public function get(string $id, ?string $defaultValue = null): ?string; | ||
|
||
/** | ||
* Gets all data. | ||
* | ||
* @return array | ||
*/ | ||
public function getAll(): array; | ||
|
||
/** | ||
* Flushs cache. | ||
* | ||
* @return bool | ||
*/ | ||
public function flushCache(): bool; | ||
} |
Oops, something went wrong.