|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace SetBased\Stratum\MySql\Helper; |
|
5
|
|
|
|
|
6
|
|
|
use SetBased\Exception\RuntimeException; |
|
7
|
|
|
use SetBased\Stratum\Backend\StratumStyle; |
|
8
|
|
|
use SetBased\Stratum\Common\Helper\Util; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class for storing the metadata of stored routines. |
|
12
|
|
|
*/ |
|
13
|
|
|
class StratumMetadata |
|
14
|
|
|
{ |
|
15
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
16
|
|
|
/** |
|
17
|
|
|
* The path to the file were the metadata is stored. |
|
18
|
|
|
* |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
private $filename; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The revision number of the format of the metadata. |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
private $revision; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The metadata of routines. Key is the routine name. |
|
32
|
|
|
* |
|
33
|
|
|
* @var array[] |
|
34
|
|
|
*/ |
|
35
|
|
|
private $routines; |
|
36
|
|
|
|
|
37
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
38
|
|
|
/** |
|
39
|
|
|
* Object constructor. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $filename The path to the file where the metadata is stored. |
|
42
|
|
|
* @param string $revision The revision number of the format of the metadata. |
|
43
|
|
|
* |
|
44
|
|
|
* @throws RuntimeException |
|
45
|
|
|
*/ |
|
46
|
1 |
|
public function __construct(string $filename, string $revision) |
|
47
|
|
|
{ |
|
48
|
1 |
|
$this->filename = $filename; |
|
49
|
1 |
|
$this->revision = $revision; |
|
50
|
|
|
|
|
51
|
1 |
|
$this->readMetadata(); |
|
52
|
1 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
55
|
|
|
/** |
|
56
|
|
|
* Removes the metadata of a routine. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $routineName The name of the routine. |
|
59
|
|
|
*/ |
|
60
|
|
|
public function delMetadata(string $routineName): void |
|
61
|
|
|
{ |
|
62
|
|
|
unset($this->routines[$routineName]); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
66
|
|
|
/** |
|
67
|
|
|
* Returns the metadata of all stored routines. |
|
68
|
|
|
* |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
1 |
|
public function getAllMetadata(): array |
|
72
|
|
|
{ |
|
73
|
1 |
|
return $this->routines; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
77
|
|
|
/** |
|
78
|
|
|
* Returns the metadata of a routine. If no metadata is available an empty array is returned. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $routineName The name of the routine. |
|
81
|
|
|
* |
|
82
|
|
|
* @return array |
|
83
|
|
|
*/ |
|
84
|
1 |
|
public function getMetadata(string $routineName): array |
|
85
|
|
|
{ |
|
86
|
1 |
|
return $this->routines[$routineName] ?? []; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
90
|
|
|
/** |
|
91
|
|
|
* Removes the metadata of stored routines are not in a list of stored routine names. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string[] $routineNames The list of stored routine names. |
|
94
|
|
|
*/ |
|
95
|
1 |
|
public function purge(array $routineNames): void |
|
96
|
|
|
{ |
|
97
|
1 |
|
$this->routines = array_intersect_key($this->routines, array_flip($routineNames)); |
|
98
|
1 |
|
} |
|
99
|
|
|
|
|
100
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
101
|
|
|
/** |
|
102
|
|
|
* Stores the metadata of a stored routine. |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $routineName The name of the routine. |
|
105
|
|
|
* @param array $metadata The metadata of the routine. |
|
106
|
|
|
*/ |
|
107
|
1 |
|
public function putMetadata(string $routineName, array $metadata): void |
|
108
|
|
|
{ |
|
109
|
1 |
|
$this->routines[$routineName] = $metadata; |
|
110
|
1 |
|
} |
|
111
|
|
|
|
|
112
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
113
|
|
|
/** |
|
114
|
|
|
* Writes the metadata of all stored routines to the metadata file. |
|
115
|
|
|
* |
|
116
|
|
|
* @param StratumStyle $io The output object. |
|
117
|
|
|
* |
|
118
|
|
|
* @throws RuntimeException |
|
119
|
|
|
*/ |
|
120
|
1 |
|
public function writeMetadata(StratumStyle $io): void |
|
121
|
|
|
{ |
|
122
|
1 |
|
$data = ['revision' => $this->revision, |
|
123
|
1 |
|
'routines' => $this->routines]; |
|
124
|
|
|
|
|
125
|
1 |
|
$json = json_encode($data, JSON_PRETTY_PRINT); |
|
126
|
1 |
|
if (json_last_error()!==JSON_ERROR_NONE) |
|
127
|
|
|
{ |
|
128
|
|
|
throw new RuntimeException("Error of encoding to JSON: '%s'.", json_last_error_msg()); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
1 |
|
Util::writeTwoPhases($this->filename, $json, $io); |
|
132
|
1 |
|
} |
|
133
|
|
|
|
|
134
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
135
|
|
|
/** |
|
136
|
|
|
* Reads the metadata from file. |
|
137
|
|
|
* |
|
138
|
|
|
* @throws RuntimeException |
|
139
|
|
|
*/ |
|
140
|
1 |
|
private function readMetadata(): void |
|
141
|
|
|
{ |
|
142
|
1 |
|
if (file_exists($this->filename)) |
|
143
|
|
|
{ |
|
144
|
1 |
|
$data = json_decode(file_get_contents($this->filename), true); |
|
145
|
1 |
|
if (json_last_error()!==JSON_ERROR_NONE) |
|
146
|
|
|
{ |
|
147
|
|
|
throw new RuntimeException("Error decoding JSON: '%s'.", json_last_error_msg()); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
1 |
|
if (!is_array($data ?? null) || ($data['revision'] ?? null)!==$this->revision) |
|
152
|
|
|
{ |
|
153
|
|
|
$data = null; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
1 |
|
$this->routines = $data['routines'] ?? []; |
|
157
|
1 |
|
} |
|
158
|
|
|
|
|
159
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
//---------------------------------------------------------------------------------------------------------------------- |
|
163
|
|
|
|