-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathRule.php
executable file
·115 lines (98 loc) · 2.36 KB
/
Rule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace Lauthz\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
/**
* Rule Model.
*/
class Rule extends Model
{
/**
* a cache store.
*
* @var \Illuminate\Cache\Repository
*/
protected $store;
/**
* the guard for lauthz.
*
* @var string
*/
protected $guard;
/**
* Fillable.
*
* @var array
*/
protected $fillable = ['ptype', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5'];
/**
* Create a new Eloquent model instance.
*
* @param array $attributes
* @param string $guard
*/
public function __construct(array $attributes = [], $guard = '')
{
$this->guard = $guard;
if (!$guard) {
$this->guard = config('lauthz.default');
}
$connection = $this->config('database.connection') ?: config('database.default');
$this->setConnection($connection);
$this->setTable($this->config('database.rules_table'));
parent::__construct($attributes);
$this->initCache();
}
/**
* Gets rules from caches.
*
* @return mixed
*/
public function getAllFromCache()
{
$get = fn () => $this->select('ptype', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5')->get()->toArray();
if (!$this->config('cache.enabled', false)) {
return $get();
}
return $this->store->remember($this->config('cache.key'), $this->config('cache.ttl'), $get);
}
/**
* Refresh Cache.
*/
public function refreshCache()
{
if (!$this->config('cache.enabled', false)) {
return;
}
$this->forgetCache();
$this->getAllFromCache();
}
/**
* Forget Cache.
*/
public function forgetCache()
{
$this->store->forget($this->config('cache.key'));
}
/**
* Init cache.
*/
protected function initCache()
{
$store = $this->config('cache.store', 'default');
$store = 'default' == $store ? null : $store;
$this->store = Cache::store($store);
}
/**
* Gets config value by key.
*
* @param string $key
* @param string $default
*
* @return mixed
*/
protected function config($key = null, $default = null)
{
return config('lauthz.'.$this->guard.'.'.$key, $default);
}
}