|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Laravel Platfourm package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Longman\Platfourm\Database\Helpers; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class CacheKeys. |
|
15
|
|
|
*/ |
|
16
|
|
View Code Duplication |
class CacheKeys |
|
|
|
|
|
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected static $storeFile = 'repository-cache-keys.json'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected static $keys = null; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param $group |
|
30
|
|
|
* @param $key |
|
31
|
|
|
* |
|
32
|
|
|
* @return void |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function putKey($group, $key) |
|
35
|
|
|
{ |
|
36
|
|
|
self::loadKeys(); |
|
37
|
|
|
|
|
38
|
|
|
self::$keys[$group] = self::getKeys($group); |
|
39
|
|
|
|
|
40
|
|
|
if (!in_array($key, self::$keys[$group])) { |
|
41
|
|
|
self::$keys[$group][] = $key; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
self::storeKeys(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param $group |
|
49
|
|
|
* |
|
50
|
|
|
* @return array|mixed |
|
51
|
|
|
*/ |
|
52
|
|
|
public static function getKeys($group) |
|
53
|
|
|
{ |
|
54
|
|
|
self::loadKeys(); |
|
55
|
|
|
self::$keys[$group] = isset(self::$keys[$group]) ? self::$keys[$group] : []; |
|
56
|
|
|
|
|
57
|
|
|
return self::$keys[$group]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return array|mixed |
|
62
|
|
|
*/ |
|
63
|
|
|
public static function loadKeys() |
|
64
|
|
|
{ |
|
65
|
|
|
if (!is_null(self::$keys) && is_array(self::$keys)) { |
|
66
|
|
|
return self::$keys; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$file = self::getFileKeys(); |
|
70
|
|
|
|
|
71
|
|
|
if (!file_exists($file)) { |
|
72
|
|
|
self::storeKeys(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$content = file_get_contents($file); |
|
76
|
|
|
self::$keys = json_decode($content, true); |
|
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
return self::$keys; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return int |
|
83
|
|
|
*/ |
|
84
|
|
|
public static function storeKeys() |
|
85
|
|
|
{ |
|
86
|
|
|
$file = self::getFileKeys(); |
|
87
|
|
|
self::$keys = is_null(self::$keys) ? [] : self::$keys; |
|
88
|
|
|
$content = json_encode(self::$keys); |
|
89
|
|
|
|
|
90
|
|
|
return file_put_contents($file, $content); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
public static function getFileKeys() |
|
97
|
|
|
{ |
|
98
|
|
|
$file = storage_path('framework/cache/' . self::$storeFile); |
|
99
|
|
|
|
|
100
|
|
|
return $file; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param $method |
|
105
|
|
|
* @param $parameters |
|
106
|
|
|
* |
|
107
|
|
|
* @return mixed |
|
108
|
|
|
*/ |
|
109
|
|
|
public static function __callStatic($method, $parameters) |
|
110
|
|
|
{ |
|
111
|
|
|
$instance = new static(); |
|
112
|
|
|
|
|
113
|
|
|
return call_user_func_array([$instance, $method], $parameters); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param $method |
|
118
|
|
|
* @param $parameters |
|
119
|
|
|
* |
|
120
|
|
|
* @return mixed |
|
121
|
|
|
*/ |
|
122
|
|
|
public function __call($method, $parameters) |
|
123
|
|
|
{ |
|
124
|
|
|
$instance = new static(); |
|
125
|
|
|
|
|
126
|
|
|
return call_user_func_array([$instance, $method], $parameters); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.