1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) 2013-present Mediasift Ltd |
5
|
|
|
* All rights reserved. |
6
|
|
|
* |
7
|
|
|
* Redistribution and use in source and binary forms, with or without |
8
|
|
|
* modification, are permitted provided that the following conditions |
9
|
|
|
* are met: |
10
|
|
|
* |
11
|
|
|
* * Redistributions of source code must retain the above copyright |
12
|
|
|
* notice, this list of conditions and the following disclaimer. |
13
|
|
|
* |
14
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
15
|
|
|
* notice, this list of conditions and the following disclaimer in |
16
|
|
|
* the documentation and/or other materials provided with the |
17
|
|
|
* distribution. |
18
|
|
|
* |
19
|
|
|
* * Neither the names of the copyright holders nor the names of his |
20
|
|
|
* contributors may be used to endorse or promote products derived |
21
|
|
|
* from this software without specific prior written permission. |
22
|
|
|
* |
23
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
24
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
25
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
26
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
27
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
28
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
29
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
30
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
31
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
32
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
33
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
34
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
35
|
|
|
* |
36
|
|
|
* @category Libraries |
37
|
|
|
* @package Storyplayer/Prose |
38
|
|
|
* @author Michael Heap <[email protected]> |
39
|
|
|
* @copyright 2013-present Mediasift Ltd www.datasift.com |
40
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
41
|
|
|
* @link http://datasift.github.io/storyplayer |
42
|
|
|
*/ |
43
|
|
|
|
44
|
|
|
namespace Prose; |
45
|
|
|
|
46
|
|
|
use DataSift\Stone\ObjectLib\BaseObject; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* UsingRuntimeTable |
50
|
|
|
* |
51
|
|
|
* @uses Prose |
52
|
|
|
* @author Michael Heap <[email protected]> |
53
|
|
|
*/ |
54
|
|
|
class UsingRuntimeTableForTargetEnvironment extends BaseRuntimeTable |
55
|
|
|
{ |
56
|
|
|
/** |
57
|
|
|
* addItem |
58
|
|
|
* |
59
|
|
|
* Add an item to a module's runtime config table |
60
|
|
|
* |
61
|
|
|
* @param string $key The key to save data under |
62
|
|
|
* @param mixed $value The value to save |
63
|
|
|
* |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
|
|
public function addItem($key, $value) |
67
|
|
|
{ |
68
|
|
|
// get our table name from the constructor |
69
|
|
|
$tableName = $this->args[0]; |
70
|
|
|
$targetEnv = $this->st->getTestEnvironmentName(); |
71
|
|
|
|
72
|
|
|
$log = usingLog()->startAction("add entry '{$key}' to {$tableName}->{$targetEnv} table"); |
73
|
|
|
|
74
|
|
|
// get the table config |
75
|
|
|
$tables = $this->getAllTables(); |
76
|
|
|
|
77
|
|
|
// make sure the table |
78
|
|
View Code Duplication |
if (!isset($tables->$tableName)){ |
79
|
|
|
$log->addStep("{$tableName} does not exist in the runtime config. creating empty table", function() use ($tables, $tableName){ |
80
|
|
|
$tables->$tableName = new BaseObject(); |
81
|
|
|
}); |
82
|
|
|
} |
83
|
|
|
if (!isset($tables->$tableName->$targetEnv)) { |
84
|
|
|
$log->addStep("{$tableName}->{$targetEnv} does not exist in the runtime config. creating empty table", function() use($tables, $tableName, $targetEnv) { |
85
|
|
|
$tables->$tableName->$targetEnv = new BaseObject(); |
86
|
|
|
}); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// make sure we don't have a duplicate entry |
90
|
|
|
if (isset($tables->$tableName->$targetEnv->$key)){ |
91
|
|
|
$msg = "Table already contains an entry for '{$key}'"; |
92
|
|
|
$log->endAction($msg); |
93
|
|
|
throw new E5xx_ActionFailed(__METHOD__, $msg); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// add the entry |
97
|
|
|
$tables->$tableName->$targetEnv->$key = $value; |
98
|
|
|
|
99
|
|
|
// save the updated runtime config |
100
|
|
|
$log->addStep("saving runtime config to disk", function() { |
101
|
|
|
$this->st->saveRuntimeConfig(); |
102
|
|
|
}); |
103
|
|
|
|
104
|
|
|
// all done |
105
|
|
|
$log->endAction(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* removeItem |
110
|
|
|
* |
111
|
|
|
* Removes an item from the runtimeConfig file |
112
|
|
|
* |
113
|
|
|
* @param string $key The key that we want to remove |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
public function removeItem($key) |
118
|
|
|
{ |
119
|
|
|
// get our table name from the constructor |
120
|
|
|
$tableName = $this->args[0]; |
121
|
|
|
$targetEnv = $this->st->getTestEnvironmentName(); |
122
|
|
|
|
123
|
|
|
// what are we doing? |
124
|
|
|
$log = usingLog()->startAction("remove entry '{$key}' from {$tableName}->{$targetEnv} table"); |
125
|
|
|
|
126
|
|
|
// get the table config |
127
|
|
|
$tables = $this->getAllTables(); |
128
|
|
|
|
129
|
|
|
// make sure it exists |
130
|
|
|
if (!isset($tables->$tableName)) { |
131
|
|
|
$msg = "table is empty / does not exist. '{$key}' not removed"; |
132
|
|
|
$log->endAction($msg); |
133
|
|
|
return; |
134
|
|
|
} |
135
|
|
|
if (!isset($tables->$tableName->$targetEnv)) { |
136
|
|
|
$msg = "table for {$targetEnv} is empty / does not exist. '{$key}' not removed"; |
137
|
|
|
$log->endAction($msg); |
138
|
|
|
return; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
// make sure we have an entry to remove |
142
|
|
|
if (!isset($tables->$tableName->$targetEnv->$key)) { |
143
|
|
|
$msg = "table does not contain an entry for '{$key}'"; |
144
|
|
|
$log->endAction($msg); |
145
|
|
|
return; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
// remove the entry |
149
|
|
|
unset($tables->$tableName->$targetEnv->$key); |
150
|
|
|
|
151
|
|
|
// save the changes |
152
|
|
|
$this->st->saveRuntimeConfig(); |
153
|
|
|
|
154
|
|
|
// all done |
155
|
|
|
$log->endAction(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Add an item to a module's runtime config table |
160
|
|
|
* |
161
|
|
|
* @param string $key The key to save data under |
162
|
|
|
* @param mixed $value The value to save |
163
|
|
|
* |
164
|
|
|
* @return void |
165
|
|
|
*/ |
166
|
|
|
public function addItemToGroup($group, $key, $value) |
167
|
|
|
{ |
168
|
|
|
// get our table name from the constructor |
169
|
|
|
$tableName = $this->args[0]; |
170
|
|
|
$targetEnv = $this->st->getTestEnvironmentName(); |
171
|
|
|
|
172
|
|
|
$log = usingLog()->startAction("add entry '{$group}->{$key}' to {$tableName}->{$targetEnv} table"); |
173
|
|
|
|
174
|
|
|
// get the table config |
175
|
|
|
$tables = $this->getAllTables(); |
176
|
|
|
|
177
|
|
|
// make sure it exists |
178
|
|
|
if (!isset($tables->$tableName)){ |
179
|
|
|
$tables->$tableName = new BaseObject(); |
180
|
|
|
} |
181
|
|
|
if (!isset($tables->$tableName->$targetEnv)) { |
182
|
|
|
$tables->$tableName->$targetEnv = new BaseObject; |
183
|
|
|
} |
184
|
|
|
if (!isset($tables->$tableName->$targetEnv->$group)) { |
185
|
|
|
$tables->$tableName->$targetEnv->$group = new BaseObject; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
// make sure we don't have a duplicate entry |
189
|
|
|
if (isset($tables->$tableName->$targetEnv->$group->$key)){ |
190
|
|
|
$msg = "table already contains an entry for '{$group}->{$key}'"; |
191
|
|
|
$log->endAction($msg); |
192
|
|
|
throw new E5xx_ActionFailed(__METHOD__, $msg); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
// add the entry |
196
|
|
|
$tables->$tableName->$targetEnv->$group->$key = $value; |
197
|
|
|
|
198
|
|
|
// make sure that the table's group is always available for |
199
|
|
|
// template expansion |
200
|
|
|
// |
201
|
|
|
// NOTE: any code that adds groups to tables by hand does NOT |
202
|
|
|
// get this guarantee |
203
|
|
|
$activeConfig = $this->st->getActiveConfig(); |
204
|
|
|
$activeConfig->setData($tableName, $tables->$tableName); |
205
|
|
|
|
206
|
|
|
// save the updated runtime config |
207
|
|
|
$log->addStep("saving runtime config to disk", function() { |
208
|
|
|
$this->st->saveRuntimeConfig(); |
209
|
|
|
}); |
210
|
|
|
|
211
|
|
|
// all done |
212
|
|
|
$log->endAction(); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Removes an item from the runtimeConfig file |
217
|
|
|
* |
218
|
|
|
* @param string $key The key that we want to remove |
219
|
|
|
* |
220
|
|
|
* @return void |
221
|
|
|
*/ |
222
|
|
|
public function removeItemFromGroup($group, $key) |
223
|
|
|
{ |
224
|
|
|
// get our table name from the constructor |
225
|
|
|
$tableName = $this->args[0]; |
226
|
|
|
$targetEnv = $this->st->getTestEnvironmentName(); |
227
|
|
|
|
228
|
|
|
// what are we doing? |
229
|
|
|
$log = usingLog()->startAction("remove entry '{$group}->{$key}' from {$tableName}->{$targetEnv} table"); |
230
|
|
|
|
231
|
|
|
// get the table config |
232
|
|
|
$tables = $this->getAllTables(); |
233
|
|
|
|
234
|
|
|
// make sure it exists |
235
|
|
|
if (!isset($tables->$tableName)) { |
236
|
|
|
$msg = "table is empty / does not exist. '{$group}->{$key}' not removed"; |
237
|
|
|
$log->endAction($msg); |
238
|
|
|
return; |
239
|
|
|
} |
240
|
|
|
if (!isset($tables->$tableName->$targetEnv)) { |
241
|
|
|
$msg = "table has no data for '{$targetEnv}'. '{$group}->{$key}' not removed"; |
242
|
|
|
$log->endAction($msg); |
243
|
|
|
return; |
244
|
|
|
} |
245
|
|
|
if (!isset($tables->$tableName->$targetEnv->$group)) { |
246
|
|
|
$msg = "table has no group '{$group}'. '{$group}->{$key}' not removed"; |
247
|
|
|
$log->endAction($msg); |
248
|
|
|
return; |
249
|
|
|
} |
250
|
|
|
if (!isset($tables->$tableName->$group->$targetEnv->$key)) { |
251
|
|
|
$msg = "table does not contain an entry for '{$group}->{$key}'"; |
252
|
|
|
$log->endAction($msg); |
253
|
|
|
return; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
// remove the entry |
257
|
|
|
unset($tables->$tableName->$targetEnv->$group->$key); |
258
|
|
|
|
259
|
|
|
// save the changes |
260
|
|
|
$this->st->saveRuntimeConfig(); |
261
|
|
|
|
262
|
|
|
// all done |
263
|
|
|
$log->endAction(); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Removes an item from the runtimeConfig file |
268
|
|
|
* |
269
|
|
|
* @param string $key The key that we want to remove |
270
|
|
|
* |
271
|
|
|
* @return void |
272
|
|
|
*/ |
273
|
|
|
public function removeItemFromAllGroups($key) |
274
|
|
|
{ |
275
|
|
|
// get our table name from the constructor |
276
|
|
|
$tableName = $this->args[0]; |
277
|
|
|
$targetEnv = $this->st->getTestEnvironmentName(); |
278
|
|
|
|
279
|
|
|
// what are we doing? |
280
|
|
|
$log = usingLog()->startAction("remove entry '{$key}' from all groups in {$tableName}->{$targetEnv} table"); |
281
|
|
|
|
282
|
|
|
// get the table config |
283
|
|
|
$tables = $this->getAllTables(); |
284
|
|
|
|
285
|
|
|
// make sure it exists |
286
|
|
|
if (!isset($tables->$tableName)) { |
287
|
|
|
$msg = "table is empty / does not exist. '{$key}' not removed"; |
288
|
|
|
$log->endAction($msg); |
289
|
|
|
return; |
290
|
|
|
} |
291
|
|
|
if (!isset($tables->$tableName->$targetEnv)) { |
292
|
|
|
$msg = "table is empty / does not exist. '{$key}' not removed"; |
293
|
|
|
$log->endAction($msg); |
294
|
|
|
return; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
$groups = get_object_vars($tables->$tableName->$targetEnv); |
298
|
|
|
if (!count($groups)) { |
299
|
|
|
$msg = "table has no groups. '{$key}' not removed"; |
300
|
|
|
$log->endAction($msg); |
301
|
|
|
return; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
$groupNames = (array_keys($groups)); |
305
|
|
View Code Duplication |
foreach ($groupNames as $groupName) { |
306
|
|
|
$group = $tables->$tableName->$targetEnv->$groupName; |
307
|
|
|
if (isset($group->$key)) { |
308
|
|
|
// remove the entry |
309
|
|
|
unset($group->$key); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
// remove the table if it's empty |
313
|
|
|
if (!count(get_object_vars($tables->$tableName->$targetEnv->$groupName))) { |
314
|
|
|
$log->addStep("table group '{$tableName}->{$targetEnv}->{$groupName}' is empty, removing from runtime config", function() use ($tables, $tableName, $targetEnv, $groupName){ |
315
|
|
|
unset($tables->$tableName->$targetEnv->$groupName); |
316
|
|
|
}); |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
// save the changes |
321
|
|
|
$this->st->saveRuntimeConfig(); |
322
|
|
|
|
323
|
|
|
// all done |
324
|
|
|
$log->endAction(); |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
|