|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Phpsa\Datastore; |
|
4
|
|
|
|
|
5
|
|
|
use Phpsa\Datastore\Datastore; |
|
6
|
|
|
use Phpsa\Datastore\Asset; |
|
7
|
|
|
use Phpsa\Datastore\DatastoreException; |
|
8
|
|
|
|
|
9
|
|
|
class Helpers { |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* tests if teh array is an associated array or an indexed array |
|
13
|
|
|
* |
|
14
|
|
|
* @param array $array |
|
15
|
|
|
* |
|
16
|
|
|
* @return bool |
|
17
|
|
|
*/ |
|
18
|
|
|
public static function isAssocArray(array $array){ |
|
19
|
|
|
if (array() === $array) return false; |
|
20
|
|
|
return array_keys($array) !== range(0, count($array) - 1); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Splits a word by capitals and glues it together with a space or the glue |
|
25
|
|
|
* |
|
26
|
|
|
* @param string $string |
|
27
|
|
|
* @param boolean $ucfirst |
|
28
|
|
|
* @param string $glue |
|
29
|
|
|
* @return string |
|
30
|
|
|
*/ |
|
31
|
|
|
public static function splitByCaps($string, $ucfirst = true, $glue = false) |
|
32
|
|
|
{ |
|
33
|
|
|
$pattern = "/(.)([A-Z])/"; |
|
34
|
|
|
$replacement = "\\1 \\2"; |
|
35
|
|
|
$return = ($ucfirst) ? |
|
36
|
|
|
ucfirst(preg_replace($pattern, $replacement, $string)) : |
|
37
|
|
|
strtolower(preg_replace($pattern, $replacement, $string)); |
|
38
|
|
|
|
|
39
|
|
|
return ($glue) ? str_replace(' ', $glue, $return) : $return; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* PArse out the asset type to the class namespaced version. |
|
44
|
|
|
* |
|
45
|
|
|
* @param [type] $queryString |
|
|
|
|
|
|
46
|
|
|
* @param bool $key |
|
47
|
|
|
* |
|
48
|
|
|
* @return void |
|
49
|
|
|
*/ |
|
50
|
|
|
public static function parseAssetType($queryString, $key = false){ |
|
51
|
|
|
$assetParts = explode(".", strtolower($queryString)); |
|
52
|
|
|
if(count($assetParts) > 2){ |
|
53
|
|
|
throw new DatastoreException("Assets are only 2 tiers deep"); |
|
54
|
|
|
} |
|
55
|
|
|
$asset = array_pop($assetParts); |
|
56
|
|
|
$list = self::getAssetList(true); |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
$list = !empty($assetParts) && isset($list[$assetParts[0]]) ? $list[$assetParts[0]] : array_shift($list); |
|
59
|
|
|
|
|
60
|
|
|
if(!isset($list[$asset])){ |
|
61
|
|
|
throw new DatastoreException("Asset not found"); |
|
62
|
|
|
} |
|
63
|
|
|
return $key ? $list[$asset]['class'] : $list[$asset]; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Gets the asset definition |
|
68
|
|
|
* |
|
69
|
|
|
* @param [type] $className |
|
|
|
|
|
|
70
|
|
|
* |
|
71
|
|
|
* @return void |
|
72
|
|
|
*/ |
|
73
|
|
|
public static function getAssetItem($className){ |
|
74
|
|
|
return array( |
|
|
|
|
|
|
75
|
|
|
'class' => $className, |
|
76
|
|
|
'name' => Self::assetInfo($className, 'name'), |
|
77
|
|
|
'name_singular' => Self::assetInfo($className, 'name_singular'), |
|
78
|
|
|
'shortname' => Self::assetInfo($className, 'shortname'), |
|
79
|
|
|
'icon' => Self::assetInfo($className, 'icon'), |
|
80
|
|
|
'children' => Self::assetInfo($className, 'children'), |
|
81
|
|
|
'is_child' => Self::assetInfo($className, 'is_child'), |
|
82
|
|
|
'max_instances' => Self::assetInfo($className, 'max_instances'), |
|
83
|
|
|
'about' => self::callStatic($className, 'about'), |
|
84
|
|
|
'private' => Self::assetInfo($className, 'private'), |
|
85
|
|
|
'has_meta' => Self::assetInfo($className, 'meta_description') !== 'off' && Self::assetInfo($className, 'meta_keywords') !== 'off', |
|
86
|
|
|
'status_equals' => Self::assetInfo($className, 'status_equals') |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public static function getStatusEquals($className){ |
|
91
|
|
|
$statusEquals = self::assetInfo($className, 'status_equals'); |
|
92
|
|
|
if(!$statusEquals){ |
|
93
|
|
|
return null; |
|
94
|
|
|
} |
|
95
|
|
|
$props = self::getAssetProps($className,$statusEquals); |
|
96
|
|
|
return isset($props['published'])?$props['published'] : null; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
public static function getAssetProps($className, $property = null){ |
|
101
|
|
|
$properties = self::assetInfo($className, 'properties'); |
|
102
|
|
|
if(!$properties){ |
|
103
|
|
|
throw new DatastoreException("No Properties found for asset"); |
|
104
|
|
|
} |
|
105
|
|
|
if($property){ |
|
106
|
|
|
return isset($properties[$property]) ? $properties[$property] : null; |
|
107
|
|
|
} |
|
108
|
|
|
return $properties; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* List of assets |
|
113
|
|
|
* |
|
114
|
|
|
* @param bool $grouped groupd by type |
|
115
|
|
|
* @param bool $includeChildren includ child assets |
|
116
|
|
|
* |
|
117
|
|
|
* @return void |
|
118
|
|
|
*/ |
|
119
|
|
|
public static function getAssetList($grouped = false, $includeChildren = true) |
|
120
|
|
|
{ |
|
121
|
|
|
$assets = array(); |
|
122
|
|
|
$datastoreAssets = config("datastore.assets"); |
|
123
|
|
|
foreach($datastoreAssets as $className){ |
|
124
|
|
|
|
|
125
|
|
|
$asset = self::getAssetItem($className); |
|
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
if (self::assetNamespace($className) !== 'asset') { |
|
128
|
|
|
throw new DatastoreException('Only assets of type assets should be used ' . $className); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
if(!$includeChildren && $asset['is_child']){ |
|
132
|
|
|
continue; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
if($asset['children'] && $includeChildren && !in_array($asset['children'], $datastoreAssets)){ |
|
136
|
|
|
$child = self::getAssetItem($asset['children']); |
|
|
|
|
|
|
137
|
|
|
if (self::assetNamespace($child['class']) !== 'asset') { |
|
138
|
|
|
throw new DatastoreException('Only assets of type assets should be used ' . $child['class']); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
if ($grouped) |
|
142
|
|
|
{ |
|
143
|
|
|
$mod = Helpers::getModule($child['class']); |
|
144
|
|
|
$assets[strtolower($mod)][strtolower($child['shortname'])] = $child; |
|
145
|
|
|
} |
|
146
|
|
|
else |
|
147
|
|
|
{ |
|
148
|
|
|
$assets[strtolower($child['shortname'])] = $child; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
|
|
153
|
|
|
if ($grouped) |
|
154
|
|
|
{ |
|
155
|
|
|
$mod = Helpers::getModule($className); |
|
156
|
|
|
$assets[strtolower($mod)][strtolower($asset['shortname'])] = $asset; |
|
157
|
|
|
} |
|
158
|
|
|
else |
|
159
|
|
|
{ |
|
160
|
|
|
$assets[strtolower($asset['shortname'])] = $asset; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
ksort($assets); |
|
167
|
|
|
if($grouped){ |
|
168
|
|
|
foreach($assets as &$group){ |
|
169
|
|
|
uasort($group, function ($a , $b) { |
|
170
|
|
|
return strcmp($a['name'], $b['name']); |
|
171
|
|
|
}); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
return $assets; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Gets the module based on the classname |
|
181
|
|
|
* @param string $class |
|
182
|
|
|
* @return string |
|
183
|
|
|
*/ |
|
184
|
|
|
public static function getModule($class) |
|
185
|
|
|
{ |
|
186
|
|
|
$parts = explode('\\', $class); |
|
187
|
|
|
array_pop($parts); |
|
188
|
|
|
$ns = ''; |
|
189
|
|
|
if(end($parts) !== 'Ams'){ |
|
190
|
|
|
$ns = ucfirst(array_pop($parts)); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
return $ns; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Gets the namespace of an asset! |
|
198
|
|
|
* @param string $className |
|
199
|
|
|
* @return string |
|
200
|
|
|
*/ |
|
201
|
|
|
public static function assetNamespace($className) |
|
202
|
|
|
{ |
|
203
|
|
|
return self::callStatic($className, 'getNamespace'); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* gets the url path for the asset |
|
208
|
|
|
* |
|
209
|
|
|
* @param [type] $className |
|
|
|
|
|
|
210
|
|
|
* |
|
211
|
|
|
* @return void |
|
212
|
|
|
* @author Craig Smith <[email protected]> |
|
213
|
|
|
*/ |
|
214
|
|
|
public static function getPath($className){ |
|
215
|
|
|
$mod = self::getModule($className); |
|
216
|
|
|
$sn = Self::assetInfo($className, 'shortname'); |
|
217
|
|
|
return !empty($mod) ? strtolower($mod .'.' . $sn) : strtolower($sn); |
|
|
|
|
|
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Calls a static method |
|
222
|
|
|
* @param string $classname class to call |
|
223
|
|
|
* @param string $method method to call |
|
224
|
|
|
* @param array $params_array method parameters |
|
225
|
|
|
* @return mixed |
|
226
|
|
|
*/ |
|
227
|
|
|
public static function callStatic($classname, $method, $params_array = array()) |
|
228
|
|
|
{ |
|
229
|
|
|
return call_user_func_array(array($classname, $method), $params_array); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* gets information tag from the asset |
|
234
|
|
|
* @param string $classname |
|
235
|
|
|
* @param mixed $lookup - what information to lookup |
|
236
|
|
|
* @return mixed information |
|
237
|
|
|
*/ |
|
238
|
|
|
public static function assetInfo($classname, $lookup = false) |
|
239
|
|
|
{ |
|
240
|
|
|
return self::callStatic($classname, 'getinfo', array($lookup)); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* gets the classname from the path of the file |
|
246
|
|
|
* @param string $file |
|
247
|
|
|
* @return string |
|
248
|
|
|
*/ |
|
249
|
|
|
public static function getClassnameFromPath($file) |
|
250
|
|
|
{ |
|
251
|
|
|
if (!is_file(FCPATH . $file)) |
|
|
|
|
|
|
252
|
|
|
{ |
|
253
|
|
|
return false; |
|
|
|
|
|
|
254
|
|
|
} |
|
255
|
|
|
$fp = fopen(FCPATH . $file, 'r'); |
|
256
|
|
|
|
|
257
|
|
|
$class = $buffer = ''; |
|
258
|
|
|
$i = 0; |
|
259
|
|
|
while (!$class) |
|
260
|
|
|
{ |
|
261
|
|
|
if (feof($fp)) |
|
|
|
|
|
|
262
|
|
|
break; |
|
263
|
|
|
|
|
264
|
|
|
$buffer .= fread($fp, 512); |
|
|
|
|
|
|
265
|
|
|
$tokens = token_get_all($buffer); |
|
266
|
|
|
|
|
267
|
|
|
if (strpos($buffer, '{') === false) |
|
268
|
|
|
continue; |
|
269
|
|
|
|
|
270
|
|
|
for (; $i < count($tokens); $i++) |
|
|
|
|
|
|
271
|
|
|
{ |
|
272
|
|
|
if ($tokens[$i][0] === T_CLASS) |
|
273
|
|
|
{ |
|
274
|
|
|
for ($j = $i + 1; $j < count($tokens); $j++) |
|
|
|
|
|
|
275
|
|
|
{ |
|
276
|
|
|
if ($tokens[$j] === '{') |
|
277
|
|
|
{ |
|
278
|
|
|
$class = $tokens[$i + 2][1]; |
|
279
|
|
|
} |
|
280
|
|
|
} |
|
281
|
|
|
} |
|
282
|
|
|
} |
|
283
|
|
|
} |
|
284
|
|
|
return $class; |
|
285
|
|
|
} |
|
286
|
|
|
} |