1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2018 VectorNetworkProject. All rights reserved. MIT license. |
4
|
|
|
* |
5
|
|
|
* GitHub: https://github.com/VectorNetworkProject/TheMix |
6
|
|
|
* Website: https://www.vector-network.tk |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace VectorNetworkProject\TheMix\provider; |
10
|
|
|
|
11
|
|
|
use pocketmine\utils\Config; |
12
|
|
|
use VectorNetworkProject\TheMix\lib\database\Provider; |
13
|
|
|
|
14
|
|
|
class YAML extends Provider |
15
|
|
|
{ |
16
|
|
|
/* @var string $path */ |
17
|
|
|
private $path; |
18
|
|
|
|
19
|
|
|
/* @var string $file */ |
20
|
|
|
private $file; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* JSON constructor. |
24
|
|
|
* |
25
|
|
|
* @param string $file |
26
|
|
|
*/ |
27
|
|
|
public function __construct(string $file = 'config') |
28
|
|
|
{ |
29
|
|
|
$this->path = self::getPath('configs', 'yaml'); |
30
|
|
|
$this->file = $file.'.yml'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param array $data |
35
|
|
|
*/ |
36
|
|
|
public function init(array $data = []): void |
37
|
|
|
{ |
38
|
|
|
if (!$this->hasTable()) { |
39
|
|
|
$this->createTable($data); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param array $table |
45
|
|
|
* |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
|
|
public function createTable(array $table = []): void |
49
|
|
|
{ |
50
|
|
|
@mkdir($this->path, 0755, true); |
|
|
|
|
51
|
|
|
$config = new Config($this->path.$this->file, Config::YAML, $table); |
52
|
|
|
$config->save(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
|
|
public function hasTable(): bool |
59
|
|
|
{ |
60
|
|
|
return file_exists($this->path.$this->file) |
61
|
|
|
? true |
62
|
|
|
: false; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
|
|
public function deleteTable(): bool |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
return unlink($this->path.$this->file) |
71
|
|
|
? true |
72
|
|
|
: false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $key |
77
|
|
|
* @param bool|mixed $data |
78
|
|
|
* |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
|
public function set(string $key, $data): void |
82
|
|
|
{ |
83
|
|
|
$config = new Config($this->path.$this->file, Config::YAML); |
84
|
|
|
$config->set($key, $data); |
85
|
|
|
$config->save(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $key |
90
|
|
|
* |
91
|
|
|
* @return mixed |
92
|
|
|
*/ |
93
|
|
|
public function get(string $key) |
94
|
|
|
{ |
95
|
|
|
$config = new Config($this->path.$this->file, Config::YAML); |
96
|
|
|
|
97
|
|
|
return $config->get($key); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
|
public function getAll(): array |
104
|
|
|
{ |
105
|
|
|
$config = new Config($this->path.$this->file, Config::YAML); |
106
|
|
|
|
107
|
|
|
return $config->getAll(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
|
|
public function getKeys(): array |
114
|
|
|
{ |
115
|
|
|
$config = new Config($this->path.$this->file, Config::YAML); |
116
|
|
|
|
117
|
|
|
return $config->getAll(true); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $key |
122
|
|
|
* |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
View Code Duplication |
public function exists(string $key): bool |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
$config = new Config($this->path.$this->file, Config::YAML); |
128
|
|
|
|
129
|
|
|
return $config->exists($key) |
130
|
|
|
? true |
131
|
|
|
: false; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $key |
136
|
|
|
* |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
|
|
public function remove(string $key): void |
140
|
|
|
{ |
141
|
|
|
$config = new Config($this->path.$this->file, Config::YAML); |
142
|
|
|
$config->remove($key); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: