1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) 2013-present Mediasift Ltd |
5
|
|
|
* Copyright (c) 2015-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 Stuart Herbert <[email protected]> |
38
|
|
|
* @copyright 2013-present Mediasift Ltd www.datasift.com |
39
|
|
|
* @copyright 2015-present Ganbaro Digital Ltd www.ganbarodigital.com |
40
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
41
|
|
|
* @link http://datasift.github.io/storyplayer |
42
|
|
|
*/ |
43
|
|
|
|
44
|
|
|
namespace StoryplayerInternals\SPv2\Modules\RuntimeTable; |
45
|
|
|
|
46
|
|
|
use DataSift\Stone\ObjectLib\BaseObject; |
47
|
|
|
use Prose\Prose; |
48
|
|
|
use Storyplayer\SPv2\Modules\Log; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* FromRuntimeTables |
52
|
|
|
* |
53
|
|
|
* @uses Prose |
54
|
|
|
* @author Stuart Herbert <[email protected]> |
55
|
|
|
*/ |
56
|
|
|
class FromRuntimeTables extends Prose |
57
|
|
|
{ |
58
|
|
|
/** |
59
|
|
|
* getAllTables |
60
|
|
|
* |
61
|
|
|
* Return our tables config that we can use for |
62
|
|
|
* in place editing |
63
|
|
|
* |
64
|
|
|
* @return BaseObject |
65
|
|
|
*/ |
66
|
|
|
public function getAllTables() |
67
|
|
|
{ |
68
|
|
|
// what are we doing? |
69
|
|
|
$log = Log::usingLog()->startAction("get all the current runtime tables"); |
70
|
|
|
|
71
|
|
|
// get the tables |
72
|
|
|
$tables = $this->getAllTablesSilently(); |
73
|
|
|
|
74
|
|
|
// all done |
75
|
|
|
$log->endAction(); |
76
|
|
|
return $tables; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* does the runtime table $tableName exist? |
81
|
|
|
* |
82
|
|
|
* @param string $tableName |
83
|
|
|
* the name of the table we want to check |
84
|
|
|
* @return boolean |
85
|
|
|
* TRUE if the table exists |
86
|
|
|
* FALSE otherwise |
87
|
|
|
*/ |
88
|
|
View Code Duplication |
public function getTableExists($tableName) |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
// what are we doing? |
91
|
|
|
$log = Log::usingLog()->startAction("does the runtime table '{$tableName}' exist?"); |
92
|
|
|
|
93
|
|
|
// get the active tables |
94
|
|
|
$tables = $this->getAllTablesSilently(); |
95
|
|
|
|
96
|
|
|
// does our table exist? |
97
|
|
|
if (isset($tables->{$tableName})) { |
98
|
|
|
$log->endAction("it exists"); |
99
|
|
|
return true; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$log->endAction("it does not exist"); |
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* return the current runtime tables, without writing to the log |
108
|
|
|
* |
109
|
|
|
* this exists mostly for other sections of the RuntimeTable module to use |
110
|
|
|
* without spamming the hell out of the logs |
111
|
|
|
* |
112
|
|
|
* @return BaseObject |
113
|
|
|
*/ |
114
|
|
|
public function getAllTablesSilently() |
115
|
|
|
{ |
116
|
|
|
// get the runtime config |
117
|
|
|
$runtimeConfig = $this->st->getRuntimeConfig(); |
118
|
|
|
$runtimeConfigManager = $this->st->getRuntimeConfigManager(); |
119
|
|
|
|
120
|
|
|
// get the active tables |
121
|
|
|
$tables = $runtimeConfigManager->getAllTables($runtimeConfig); |
122
|
|
|
|
123
|
|
|
// all done |
124
|
|
|
return $tables; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* get a table from the runtime tables list |
129
|
|
|
* |
130
|
|
|
* @param string $tableName |
131
|
|
|
* the name of the table that you want |
132
|
|
|
* @return object|null |
133
|
|
|
* returns an object if the table exists |
134
|
|
|
* returns NULL otherwise |
135
|
|
|
*/ |
136
|
|
View Code Duplication |
public function getTable($tableName) |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
// what are we doing? |
139
|
|
|
$log = Log::usingLog()->startAction("get runtime table '{$tableName}'"); |
140
|
|
|
|
141
|
|
|
// do we have one? |
142
|
|
|
$table = $this->getTableSilently($tableName); |
143
|
|
|
|
144
|
|
|
// all done |
145
|
|
|
// |
146
|
|
|
// NOTE: we do not log the contents of the table here, to avoid spamming |
147
|
|
|
// the logs |
148
|
|
|
// |
149
|
|
|
// when we have more fine-grained logging, we can revisit this |
150
|
|
|
$log->endAction(); |
151
|
|
|
return $table; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* return a named table, without writing to the log |
156
|
|
|
* |
157
|
|
|
* @param string $tableName |
158
|
|
|
* the name of the table that we want |
159
|
|
|
* @return BaseObject|null |
160
|
|
|
*/ |
161
|
|
|
public function getTableSilently($tableName) |
162
|
|
|
{ |
163
|
|
|
$tables = $this->getAllTablesSilently(); |
164
|
|
|
if (isset($tables->$tableName)) { |
165
|
|
|
return $tables->$tableName; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return null; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
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.