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\Log; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* ExpectsRuntimeTable |
49
|
|
|
* |
50
|
|
|
* @uses Prose |
51
|
|
|
* @author Michael Heap <[email protected]> |
52
|
|
|
*/ |
53
|
|
|
class FromRuntimeTable extends BaseRuntimeTable |
54
|
|
|
{ |
55
|
|
|
/** |
56
|
|
|
* getTable |
57
|
|
|
* |
58
|
|
|
* |
59
|
|
|
* @return object The table from the config |
60
|
|
|
*/ |
61
|
|
View Code Duplication |
public function getTable() |
62
|
|
|
{ |
63
|
|
|
// get our table name from the constructor |
64
|
|
|
$tableName = $this->args[0]; |
65
|
|
|
|
66
|
|
|
// what are we doing? |
67
|
|
|
$log = Log::usingLog()->startAction("get '{$tableName}' table from runtime config"); |
68
|
|
|
|
69
|
|
|
// get the table config |
70
|
|
|
$tables = $this->getAllTables(); |
71
|
|
|
|
72
|
|
|
// make sure we have a table |
73
|
|
|
if (!isset($tables->$tableName)){ |
74
|
|
|
$tables->$tableName = new BaseObject(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// all done |
78
|
|
|
$log->endAction(); |
79
|
|
|
return $tables->$tableName; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
View Code Duplication |
public function getGroupFromTable($group) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
// get our table name from the constructor |
85
|
|
|
$tableName = $this->args[0]; |
86
|
|
|
|
87
|
|
|
// what are we doing? |
88
|
|
|
$log = Log::usingLog()->startAction("get '{$tableName}->{$group}' table group from runtime config"); |
89
|
|
|
|
90
|
|
|
// get the table config |
91
|
|
|
$tables = $this->getAllTables(); |
92
|
|
|
|
93
|
|
|
// make sure we have a table |
94
|
|
|
if (!isset($tables->$tableName)){ |
95
|
|
|
$tables->$tableName = new BaseObject(); |
96
|
|
|
} |
97
|
|
|
// make sure we have a group |
98
|
|
|
if (!isset($tables->$tableName->$group)) { |
99
|
|
|
$tables->$tableName->$group = new BaseObject; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// all done |
103
|
|
|
$log->endAction(); |
104
|
|
|
return $tables->$tableName->$group; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* getDetails |
109
|
|
|
* |
110
|
|
|
* Get details for a specific key |
111
|
|
|
* |
112
|
|
|
* @param string $key key The key to look for inside the tableName table |
113
|
|
|
* |
114
|
|
|
* @return object The details stored under $key |
115
|
|
|
*/ |
116
|
|
|
public function getDetails($key) |
117
|
|
|
{ |
118
|
|
|
// get our table name from the constructor |
119
|
|
|
$tableName = $this->args[0]; |
120
|
|
|
|
121
|
|
|
// what are we doing? |
122
|
|
|
$log = Log::usingLog()->startAction("get details for '{$key}' from {$tableName} table"); |
123
|
|
|
|
124
|
|
|
// get the table config |
125
|
|
|
$tables = $this->getAllTables(); |
126
|
|
|
|
127
|
|
|
// make sure we have a hosts table |
128
|
|
|
if (!isset($tables->$tableName)) { |
129
|
|
|
$msg = "table is empty / does not exist"; |
130
|
|
|
$log->endAction($msg); |
131
|
|
|
|
132
|
|
|
return null; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
// do we have the entry we're looking for? |
136
|
|
View Code Duplication |
if (!isset($tables->$tableName->$key)) { |
|
|
|
|
137
|
|
|
$msg = "table does not contain an entry for '{$key}'"; |
138
|
|
|
$log->endAction($msg); |
139
|
|
|
return null; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
// all done |
143
|
|
|
$log->endAction(); |
144
|
|
|
return $tables->$tableName->$key; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get details for a specific key from a group |
149
|
|
|
* |
150
|
|
|
* @param string $key |
151
|
|
|
* The key to look for inside the tableName table |
152
|
|
|
* |
153
|
|
|
* @return stdClass The details stored under $key |
154
|
|
|
*/ |
155
|
|
|
public function getDetailsFromGroup($group, $key) |
156
|
|
|
{ |
157
|
|
|
// get our table name from the constructor |
158
|
|
|
$tableName = $this->args[0]; |
159
|
|
|
|
160
|
|
|
// what are we doing? |
161
|
|
|
$log = Log::usingLog()->startAction("get details for '{$group}->{$key}' from {$tableName} table"); |
162
|
|
|
|
163
|
|
|
// get the table config |
164
|
|
|
$tables = $this->getAllTables(); |
165
|
|
|
|
166
|
|
|
// make sure we have a table |
167
|
|
|
if (!isset($tables->$tableName)) { |
168
|
|
|
$msg = "table is empty / does not exist"; |
169
|
|
|
$log->endAction($msg); |
170
|
|
|
|
171
|
|
|
return null; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
// make sure we have the group |
175
|
|
View Code Duplication |
if (!isset($tables->$tableName->$group)) { |
|
|
|
|
176
|
|
|
$msg = "table has no group '{$group}'"; |
177
|
|
|
$log->endAction($msg); |
178
|
|
|
|
179
|
|
|
return null; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
// do we have the entry we're looking for? |
183
|
|
View Code Duplication |
if (!isset($tables->$tableName->$group->$key)) { |
|
|
|
|
184
|
|
|
$msg = "table does not contain an entry for '{$group}->{$key}'"; |
185
|
|
|
$log->endAction($msg); |
186
|
|
|
return null; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
// all done |
190
|
|
|
$log->endAction(); |
191
|
|
|
return $tables->$tableName->$group->$key; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.