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