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
|
|
|
* @author Michael Heap <[email protected]> |
37
|
|
|
* @copyright 2013-present Mediasift Ltd www.datasift.com |
38
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
39
|
|
|
* @link http://datasift.github.io/storyplayer |
40
|
|
|
*/ |
41
|
|
|
|
42
|
|
|
namespace StoryplayerInternals\SPv2\Modules\RuntimeTable; |
43
|
|
|
|
44
|
|
|
use DataSift\Stone\ObjectLib\BaseObject; |
45
|
|
|
use Storyplayer\SPv2\Modules\Exceptions; |
46
|
|
|
use Storyplayer\SPv2\Modules\Log; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* UsingRuntimeTable |
50
|
|
|
* |
51
|
|
|
* @uses Prose |
52
|
|
|
* @author Michael Heap <[email protected]> |
53
|
|
|
*/ |
54
|
|
|
class UsingRuntimeTable 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
|
|
|
|
71
|
|
|
$log = Log::usingLog()->startAction("add entry '{$key}' to {$tableName} table"); |
72
|
|
|
|
73
|
|
|
// get the table config |
74
|
|
|
$tables = $this->getAllTables(); |
75
|
|
|
|
76
|
|
|
// make sure it exists |
77
|
|
|
if (!isset($tables->$tableName)){ |
78
|
|
|
$log->addStep("{$tableName} does not exist in the runtime config. creating empty table", function() use ($tables, $tableName){ |
79
|
|
|
$tables->$tableName = new BaseObject(); |
80
|
|
|
}); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// make sure we don't have a duplicate entry |
84
|
|
View Code Duplication |
if (isset($tables->$tableName->$key)){ |
|
|
|
|
85
|
|
|
$msg = "Table already contains an entry for '{$key}'"; |
86
|
|
|
$log->endAction($msg); |
87
|
|
|
throw Exceptions::newActionFailedException(__METHOD__, $msg); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// add the entry |
91
|
|
|
$tables->$tableName->$key = $value; |
92
|
|
|
|
93
|
|
|
// save the updated runtime config |
94
|
|
|
$log->addStep("saving runtime config to disk", function() { |
95
|
|
|
$this->st->saveRuntimeConfig(); |
96
|
|
|
}); |
97
|
|
|
|
98
|
|
|
// all done |
99
|
|
|
$log->endAction(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* removeItem |
104
|
|
|
* |
105
|
|
|
* Removes an item from the runtimeConfig file |
106
|
|
|
* |
107
|
|
|
* @param string $key The key that we want to remove |
108
|
|
|
* |
109
|
|
|
* @return void |
110
|
|
|
*/ |
111
|
|
|
public function removeItem($key) |
112
|
|
|
{ |
113
|
|
|
// get our table name from the constructor |
114
|
|
|
$tableName = $this->args[0]; |
115
|
|
|
|
116
|
|
|
// what are we doing? |
117
|
|
|
$log = Log::usingLog()->startAction("remove entry '{$key}' from {$tableName} table"); |
118
|
|
|
|
119
|
|
|
// get the table config |
120
|
|
|
$tables = $this->getAllTables(); |
121
|
|
|
|
122
|
|
|
// make sure it exists |
123
|
|
|
if (!isset($tables->$tableName)) { |
124
|
|
|
$msg = "table is empty / does not exist. '{$key}' not removed"; |
125
|
|
|
$log->endAction($msg); |
126
|
|
|
return; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// make sure we have an entry to remove |
130
|
|
View Code Duplication |
if (!isset($tables->$tableName->$key)) { |
|
|
|
|
131
|
|
|
$msg = "table does not contain an entry for '{$key}'"; |
132
|
|
|
$log->endAction($msg); |
133
|
|
|
return; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
// remove the entry |
137
|
|
|
unset($tables->$tableName->$key); |
138
|
|
|
|
139
|
|
|
// remove the table if it's empty |
140
|
|
|
if (!count(get_object_vars($tables->$tableName))) { |
141
|
|
|
Log::usingLog()->writeToLog("table '{$tableName}' is empty, removing from runtime config"); |
142
|
|
|
unset($tables->$tableName); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// save the changes |
146
|
|
|
//$this->st->saveRuntimeConfig(); |
|
|
|
|
147
|
|
|
|
148
|
|
|
// all done |
149
|
|
|
$log->endAction(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* updateItem |
154
|
|
|
* |
155
|
|
|
* Replace an item in the runtimeConfig file |
156
|
|
|
* |
157
|
|
|
* @param string $key |
158
|
|
|
* The key that we want to update |
159
|
|
|
* @param mixed $value |
160
|
|
|
* The new value for $key |
161
|
|
|
* |
162
|
|
|
* @return void |
163
|
|
|
*/ |
164
|
|
|
public function updateItem($key, $value) |
165
|
|
|
{ |
166
|
|
|
// get our table name from the constructor |
167
|
|
|
$tableName = $this->args[0]; |
168
|
|
|
|
169
|
|
|
// what are we doing? |
170
|
|
|
$log = Log::usingLog()->startAction("update entry '{$key}' in {$tableName} table"); |
171
|
|
|
|
172
|
|
|
// get the table config |
173
|
|
|
$tables = $this->getAllTables(); |
174
|
|
|
|
175
|
|
|
// make sure it exists |
176
|
|
|
if (!isset($tables->$tableName)) { |
177
|
|
|
$msg = "table is empty / does not exist. '{$key}' not updated"; |
178
|
|
|
$log->endAction($msg); |
179
|
|
|
return; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
// make sure we have an entry to update |
183
|
|
|
$tables->$tableName->$key = $value; |
184
|
|
|
$log->endAction(); |
185
|
|
|
|
186
|
|
|
// save the changes |
187
|
|
|
$this->st->saveRuntimeConfig(); |
188
|
|
|
|
189
|
|
|
// all done |
190
|
|
|
$log->endAction(); |
191
|
|
|
|
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Add an item to a module's runtime config table |
196
|
|
|
* |
197
|
|
|
* @param string $key The key to save data under |
198
|
|
|
* @param string $value The value to save |
199
|
|
|
* |
200
|
|
|
* @return void |
201
|
|
|
*/ |
202
|
|
|
public function addItemToGroup($group, $key, $value) |
203
|
|
|
{ |
204
|
|
|
// get our table name from the constructor |
205
|
|
|
$tableName = $this->args[0]; |
206
|
|
|
|
207
|
|
|
$log = Log::usingLog()->startAction("add entry '{$group}->{$key}' to {$tableName} table"); |
208
|
|
|
|
209
|
|
|
// get the table config |
210
|
|
|
$tables = $this->getAllTables(); |
211
|
|
|
|
212
|
|
|
// make sure it exists |
213
|
|
|
if (!isset($tables->$tableName)){ |
214
|
|
|
$tables->$tableName = new BaseObject(); |
215
|
|
|
} |
216
|
|
|
if (!isset($tables->$tableName->$group)) { |
217
|
|
|
$tables->$tableName->$group = new BaseObject; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
// make sure we don't have a duplicate entry |
221
|
|
View Code Duplication |
if (isset($tables->$tableName->$group->$key)){ |
|
|
|
|
222
|
|
|
$msg = "table already contains an entry for '{$group}->{$key}'"; |
223
|
|
|
$log->endAction($msg); |
224
|
|
|
throw Exceptions::newActionFailedException(__METHOD__, $msg); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
// add the entry |
228
|
|
|
$tables->$tableName->$group->$key = $value; |
229
|
|
|
|
230
|
|
|
// make sure that the table's group is always available for |
231
|
|
|
// template expansion |
232
|
|
|
// |
233
|
|
|
// NOTE: any code that adds groups to tables by hand does NOT |
234
|
|
|
// get this guarantee |
235
|
|
|
$activeConfig = $this->st->getActiveConfig(); |
236
|
|
|
$activeConfig->setData($tableName, $tables->$tableName); |
237
|
|
|
|
238
|
|
|
// save the updated runtime config |
239
|
|
|
$log->addStep("saving runtime config to disk", function() { |
240
|
|
|
$this->st->saveRuntimeConfig(); |
241
|
|
|
}); |
242
|
|
|
|
243
|
|
|
// all done |
244
|
|
|
$log->endAction(); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Removes an item from the runtimeConfig file |
249
|
|
|
* |
250
|
|
|
* @param string $key The key that we want to remove |
251
|
|
|
* |
252
|
|
|
* @return void |
253
|
|
|
*/ |
254
|
|
|
public function removeItemFromGroup($group, $key) |
255
|
|
|
{ |
256
|
|
|
// get our table name from the constructor |
257
|
|
|
$tableName = $this->args[0]; |
258
|
|
|
|
259
|
|
|
// what are we doing? |
260
|
|
|
$log = Log::usingLog()->startAction("remove entry '{$group}->{$key}' from {$tableName} table"); |
261
|
|
|
|
262
|
|
|
// get the table config |
263
|
|
|
$tables = $this->getAllTables(); |
264
|
|
|
|
265
|
|
|
// make sure it exists |
266
|
|
|
if (!isset($tables->$tableName)) { |
267
|
|
|
$msg = "table is empty / does not exist. '{$group}->{$key}' not removed"; |
268
|
|
|
$log->endAction($msg); |
269
|
|
|
return; |
270
|
|
|
} |
271
|
|
View Code Duplication |
if (!isset($tables->$tableName->$group)) { |
|
|
|
|
272
|
|
|
$msg = "table has no group '{$group}'. '{$group}->{$key}' not removed"; |
273
|
|
|
$log->endAction($msg); |
274
|
|
|
return; |
275
|
|
|
} |
276
|
|
View Code Duplication |
if (!isset($tables->$tableName->$group->$key)) { |
|
|
|
|
277
|
|
|
$msg = "table does not contain an entry for '{$group}->{$key}'"; |
278
|
|
|
$log->endAction($msg); |
279
|
|
|
return; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
// remove the entry |
283
|
|
|
unset($tables->$tableName->$group->$key); |
284
|
|
|
|
285
|
|
|
// remove the table if it's empty |
286
|
|
View Code Duplication |
if (!count(get_object_vars($tables->$tableName->$group))) { |
|
|
|
|
287
|
|
|
$log->addStep("table group '{$tableName}->{$group}' is empty, removing from runtime config", function() use ($tables, $tableName, $group){ |
288
|
|
|
unset($tables->$tableName->$group); |
289
|
|
|
}); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
// save the changes |
293
|
|
|
$this->st->saveRuntimeConfig(); |
294
|
|
|
|
295
|
|
|
// all done |
296
|
|
|
$log->endAction(); |
297
|
|
|
|
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Removes an item from the runtimeConfig file |
302
|
|
|
* |
303
|
|
|
* @param string $key The key that we want to remove |
304
|
|
|
* |
305
|
|
|
* @return void |
306
|
|
|
*/ |
307
|
|
|
public function removeItemFromAllGroups($key) |
308
|
|
|
{ |
309
|
|
|
// get our table name from the constructor |
310
|
|
|
$tableName = $this->args[0]; |
311
|
|
|
|
312
|
|
|
// what are we doing? |
313
|
|
|
$log = Log::usingLog()->startAction("remove entry '{$key}' from all groups in {$tableName} table"); |
314
|
|
|
|
315
|
|
|
// get the table config |
316
|
|
|
$tables = $this->getAllTables(); |
317
|
|
|
|
318
|
|
|
// make sure it exists |
319
|
|
|
if (!isset($tables->$tableName)) { |
320
|
|
|
$msg = "table is empty / does not exist. '{$key}' not removed"; |
321
|
|
|
$log->endAction($msg); |
322
|
|
|
return; |
323
|
|
|
} |
324
|
|
|
$groups = get_object_vars($tables->$tableName); |
325
|
|
|
if (!count($groups)) { |
326
|
|
|
$msg = "table has no groups. '{$key}' not removed"; |
327
|
|
|
$log->endAction($msg); |
328
|
|
|
return; |
329
|
|
|
} |
330
|
|
|
foreach ($tables->$tableName as $group => $contents) { |
331
|
|
|
if (isset($tables->$tableName->$group->$key)) { |
332
|
|
|
// remove the entry |
333
|
|
|
unset($tables->$tableName->$group->$key); |
334
|
|
|
|
335
|
|
|
// remove the table if it's empty |
336
|
|
View Code Duplication |
if (!count(get_object_vars($tables->$tableName->$group))) { |
|
|
|
|
337
|
|
|
$log->addStep("table group '{$tableName}->{$group}' is empty, removing from runtime config", function() use ($tables, $tableName, $group){ |
338
|
|
|
unset($tables->$tableName->$group); |
339
|
|
|
}); |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
// save the changes |
344
|
|
|
$this->st->saveRuntimeConfig(); |
345
|
|
|
|
346
|
|
|
// all done |
347
|
|
|
$log->endAction(); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* removeTable |
352
|
|
|
* |
353
|
|
|
* deletes the entire table |
354
|
|
|
* |
355
|
|
|
* @return void |
356
|
|
|
*/ |
357
|
|
|
public function removeTable() |
358
|
|
|
{ |
359
|
|
|
// get our table name from the constructor |
360
|
|
|
$tableName = $this->args[0]; |
361
|
|
|
|
362
|
|
|
// what are we doing? |
363
|
|
|
$log = Log::usingLog()->startAction("remove the {$tableName} table from the runtime config"); |
364
|
|
|
|
365
|
|
|
// get the table config |
366
|
|
|
$tables = $this->getAllTables(); |
367
|
|
|
|
368
|
|
|
// make sure it exists |
369
|
|
|
if (!isset($tables->$tableName)) { |
370
|
|
|
$log->endAction(); |
371
|
|
|
return; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
// remove the table |
375
|
|
|
unset($tables->$tableName); |
376
|
|
|
|
377
|
|
|
// save the changes |
378
|
|
|
$this->st->saveRuntimeConfig(); |
379
|
|
|
|
380
|
|
|
// all done |
381
|
|
|
$log->endAction(); |
382
|
|
|
} |
383
|
|
|
} |
384
|
|
|
|
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.