Skip to content

Commit

Permalink
Redis Based Settings (#24)
Browse files Browse the repository at this point in the history
Redis based settings
  • Loading branch information
glorand authored Sep 17, 2019
1 parent 0a106d7 commit 6f57896
Show file tree
Hide file tree
Showing 14 changed files with 374 additions and 19 deletions.
10 changes: 5 additions & 5 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ build:
- php-scrutinizer-run
- command: phpcs-run
use_website_config: true
- command: './vendor/bin/phpunit --coverage-clover=coverage.xml'
coverage:
file: coverage.xml
format: clover
filter:
excluded_paths:
- 'tests/*'
Expand All @@ -35,4 +31,8 @@ coding_style:
around_operators:
concatenation: true
other:
after_type_cast: false
after_type_cast: false
tools:
external_code_coverage:
timeout: 600
runs: 3
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ before_script:
- composer install

script:
- vendor/bin/phpunit --coverage-clover=coverage.xml
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover

after_script:
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

All notable changes to `glorand/laravel-model-settings` will be documented in this file

## 3.3.1 - 2019-09-04

## 3.4.1 - 2019-09-17
### Added
- Laravel 6 support
- Redis support

## 3.3.0 - 2019-08-29
### Added
Expand Down
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Bug reports, feature requests, and pull requests can be submitted by following o
- [Updating your Eloquent Models](#update_models)
- [Option 1 - `HasSettingsField` trait](#update_models_1)
- [Option 2 - `HasSettingsTable` trait](#update_models_2)
- [Option 3 - `HasSettingsRedis` trait](#update_models_3)
- [Default Settings](#default_settings)
- [Usage](#usage)
- [Get all model's settings](#get_all)
Expand Down Expand Up @@ -113,7 +114,17 @@ class User extends Model
}
```

##Default settings <a name="default_settings"></a>
#### Option 3 - `HasSettingsRedis` trait <a name="update_models_3"></a>
```php
use Glorand\Model\Settings\Traits\HasSettingsRedis;

class User extends Model
{
use HasSettingsRedis;
}
```

## Default settings <a name="default_settings"></a>

```php
use Glorand\Model\Settings\Traits\HasSettingsTable;
Expand Down Expand Up @@ -211,3 +222,4 @@ The MIT License (MIT). Please see [LICENSE](LICENSE) for more information.

## Related Stuff
- [LaraNews - Laravel Model Settings](https://laravel-news.com/laravel-model-settings)
- [made with Laravel - Laravel Model Settings](https://madewithlaravel.com/laravel-model-settings)
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
"illuminate/database": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0",
"illuminate/support": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0",
"illuminate/console": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0",
"illuminate/filesystem": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0"
"illuminate/filesystem": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^7.5|^8.0",
"orchestra/testbench": "~3.8.0|^4.0",
"friendsofphp/php-cs-fixer": "^2.16@dev",
"predis/predis": "^1.1"
"josiasmontag/laravel-redis-mock": "^1.2"
},
"suggest": {
"predis/predis": "Required to use settings with Redis"
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
<log type="coverage-text" target="php://stdout" showUncoveredFiles="true"/>
</logging>-->
<php>
<env name="DB_CONNECTION" value="testing" />

</php>
</phpunit>
52 changes: 52 additions & 0 deletions src/Managers/RedisSettingsManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Glorand\Model\Settings\Managers;

use Glorand\Model\Settings\Contracts\SettingsManagerContract;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Redis;

/**
* Class FieldSettingsManager
* @package Glorand\Model\Settings\Managers
* @property \Illuminate\Database\Eloquent\Model|\Glorand\Model\Settings\Traits\HasSettingsRedis $model
*/
class RedisSettingsManager extends AbstractSettingsManager
{
public function apply(array $settings = []): SettingsManagerContract
{
Redis::set($this->model->cacheKey(), json_encode($settings));

return $this;
}

public function set(string $path, $value): SettingsManagerContract
{
$settings = $this->all();
Arr::set($settings, $path, $value);

return $this->apply($settings);
}

/**
* Delete an item by its unique path.
*
* @param string|null $path
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
*/
public function delete(string $path = null): SettingsManagerContract
{
{
if (!$path) {
$settings = [];
} else {
$settings = $this->all();
Arr::forget($settings, $path);
}

$this->apply($settings);

return $this;
}
}
}
48 changes: 48 additions & 0 deletions src/Traits/HasSettingsRedis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Glorand\Model\Settings\Traits;

use Glorand\Model\Settings\Contracts\SettingsManagerContract;
use Glorand\Model\Settings\Managers\RedisSettingsManager;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Redis;

/**
* Trait HasSettingsRedis
* @package Glorand\Model\Settings\Traits
* @property array $settings
*/
trait HasSettingsRedis
{
use HasSettings;

/**
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
* @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
*/
public function settings(): SettingsManagerContract
{
return new RedisSettingsManager($this);
}

public function getSettingsValue(): array
{
$redisValue = Redis::get($this->cacheKey());

return Arr::wrap(json_decode($redisValue, true));
}

public function cacheKey(string $key = null): string
{
return sprintf(
"r-k-%s:%s:%s",
$this->getTable(),
$this->getKey(),
$this->updated_at->timestamp
) . $key;
}

abstract public function getTable();

abstract public function getKey();
}
2 changes: 1 addition & 1 deletion tests/ConsoleCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function testModelSettingsTableCommand()
config(['model_settings.settings_table_name' => 'custom_settings_table']);
$this->artisan('model-settings:model-settings-table')
->assertExitCode(0);

config(['model_settings.settings_table_name' => null]);
$this->artisan('model-settings:model-settings-table')
->assertExitCode(0);
Expand Down
2 changes: 1 addition & 1 deletion tests/FieldSettingsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class FieldSettingsManagerTest extends TestCase
'email' => "[email protected]",
],
];
/** @var array */
/** @var array */
protected $defaultSettingsTestArray = [
'project' => 'Main Project',
];
Expand Down
19 changes: 19 additions & 0 deletions tests/Models/UserWithRedis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Glorand\Model\Settings\Tests\Models;

use Glorand\Model\Settings\Traits\HasSettingsRedis;
use Illuminate\Database\Eloquent\Model;

class UserWithRedis extends Model
{
use HasSettingsRedis;

protected $table = 'users';

protected $guarded = [];

protected $fillable = ['id', 'name'];

public $defaultSettings = [];
}
Loading

0 comments on commit 6f57896

Please sign in to comment.