|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yiranzai\Dht; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class Hash |
|
7
|
|
|
* @package Yiranzai\Dht |
|
8
|
|
|
*/ |
|
9
|
|
|
class Hash extends Dht |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
public const DEFAULT_PATH = __DIR__ . DIRECTORY_SEPARATOR . 'Config' . DIRECTORY_SEPARATOR; |
|
13
|
|
|
/** |
|
14
|
|
|
* @var Filesystem |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $file; |
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $cachePath = self::DEFAULT_PATH; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $guarded = ['file']; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Hash constructor. |
|
28
|
|
|
* @param array $config |
|
29
|
|
|
*/ |
|
30
|
21 |
|
public function __construct($config = []) |
|
31
|
|
|
{ |
|
32
|
21 |
|
if (!empty($config)) { |
|
33
|
6 |
|
foreach ($config as $key => $item) { |
|
34
|
6 |
|
if (in_array($key, $this->guarded, true)) { |
|
35
|
3 |
|
continue; |
|
36
|
|
|
} |
|
37
|
6 |
|
if ($key === 'algo') { |
|
38
|
3 |
|
$this->algo($item); |
|
39
|
3 |
|
continue; |
|
40
|
|
|
} |
|
41
|
6 |
|
$this->$key = $item; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
21 |
|
$this->file = new Filesystem(); |
|
45
|
21 |
|
if (!$this->file->exists($this->cachePath)) { |
|
46
|
6 |
|
$this->file->makeDirectory($this->cachePath); |
|
47
|
|
|
} |
|
48
|
21 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param string|array $data |
|
52
|
|
|
* @param string $path |
|
53
|
|
|
*/ |
|
54
|
3 |
|
public static function cache($data, $path = self::DEFAULT_PATH): void |
|
55
|
|
|
{ |
|
56
|
3 |
|
$file = new Filesystem(); |
|
57
|
3 |
|
if (!$file->exists($path)) { |
|
58
|
3 |
|
$file->makeDirectory($path); |
|
59
|
|
|
} |
|
60
|
3 |
|
if (is_array($data)) { |
|
61
|
3 |
|
$data = json_encode($data); |
|
62
|
3 |
|
} elseif ($data instanceof \JsonSerializable) { |
|
|
|
|
|
|
63
|
3 |
|
$data = json_encode($data); |
|
64
|
|
|
} else { |
|
65
|
3 |
|
json_decode($data, true); |
|
66
|
3 |
|
if (json_last_error() !== JSON_ERROR_NONE) { |
|
67
|
|
|
throw new \RuntimeException('The data must be json or array'); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
3 |
|
$file->put($path . 'config', $data); |
|
72
|
3 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string $path |
|
76
|
|
|
* @return mixed |
|
77
|
|
|
*/ |
|
78
|
6 |
|
public static function getCache($path = self::DEFAULT_PATH) |
|
79
|
|
|
{ |
|
80
|
6 |
|
$file = new Filesystem(); |
|
81
|
6 |
|
if (!$file->exists($path . 'config')) { |
|
82
|
3 |
|
throw new \RuntimeException('This path already exists'); |
|
83
|
|
|
} |
|
84
|
3 |
|
$data = json_decode($file->get($path . 'config')); |
|
85
|
3 |
|
if (json_last_error() !== JSON_ERROR_NONE) { |
|
86
|
|
|
throw new \RuntimeException('The data is invalid'); |
|
87
|
|
|
} |
|
88
|
3 |
|
return $data; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
6 |
|
public function toArray(): array |
|
95
|
|
|
{ |
|
96
|
6 |
|
$array = array(); |
|
97
|
6 |
|
foreach ($this as $key => $value) { |
|
98
|
6 |
|
if (in_array($key, $this->guarded, true)) { |
|
99
|
6 |
|
continue; |
|
100
|
|
|
} |
|
101
|
6 |
|
$array[$key] = $value; |
|
102
|
|
|
} |
|
103
|
6 |
|
return $array; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param string $str |
|
108
|
|
|
* @return $this |
|
109
|
|
|
*/ |
|
110
|
3 |
|
public function path(string $str): self |
|
111
|
|
|
{ |
|
112
|
3 |
|
$this->cachePath = $str; |
|
113
|
3 |
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|