-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Redis based settings
- Loading branch information
Showing
14 changed files
with
374 additions
and
19 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
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
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
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
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
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
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,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; | ||
} | ||
} | ||
} |
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,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(); | ||
} |
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
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 |
---|---|---|
|
@@ -18,7 +18,7 @@ class FieldSettingsManagerTest extends TestCase | |
'email' => "[email protected]", | ||
], | ||
]; | ||
/** @var array */ | ||
/** @var array */ | ||
protected $defaultSettingsTestArray = [ | ||
'project' => 'Main Project', | ||
]; | ||
|
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,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 = []; | ||
} |
Oops, something went wrong.