-
Notifications
You must be signed in to change notification settings - Fork 0
/
Helper.php
296 lines (262 loc) · 7.49 KB
/
Helper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
/**
* @package vdm/openai
*
* @created 3rd September, 2020
* @author Llewellyn van der Merwe <https://dev.vdm.io>
* @git VDM OpenAi Library <https://git.vdm.dev/joomla/vdm-openai>
* @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace VastDevelopmentMethod\Openai\Joomla\Utilities\Component;
use Joomla\CMS\Factory;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\Input\Input;
use Joomla\Registry\Registry;
use VastDevelopmentMethod\Openai\Joomla\Utilities\String\NamespaceHelper;
/**
* Some component helper
*
* @since 3.0.11
*/
abstract class Helper
{
/**
* The current option
*
* @var string|null
* @since 3.0.11
*/
public static ?string $option = null;
/**
* The component manifest list cache
*
* @var array
* @since 3.2.0
*/
public static array $manifest = [];
/**
* The component params list cache
*
* @var Registry[]
* @since 3.0.11
*/
protected static array $params = [];
/**
* Gets the parameter object for the component
*
* @param string|null $option The option for the component.
*
* @return Registry A Registry object.
* @see Registry
* @since 3.0.11
*/
public static function getParams(?string $option = null): Registry
{
// check that we have an option
if (empty($option))
{
$option = self::getOption();
}
// get global value
if (!isset(self::$params[$option]) || !self::$params[$option] instanceof Registry)
{
self::$params[$option] = ComponentHelper::getParams($option);
}
return self::$params[$option];
}
/**
* Set the component option
*
* @param string|null $option The option
*
* @return void
* @since 3.2.0
*/
public static function setOption(?string $option): void
{
self::$option = $option;
}
/**
* Get the component option
*
* @param string|null $default The default return value if none is found
*
* @return string|null A component option
* @since 3.0.11
*/
public static function getOption(?string $default = 'empty'): ?string
{
if (empty(self::$option))
{
// get the option from the url input
self::$option = (new Input)->getString('option', null);
}
if (empty(self::$option))
{
$app = Factory::getApplication();
// Check if the getInput method exists in the application object
if (method_exists($app, 'getInput'))
{
// get the option from the application
self::$option = $app->getInput()->getCmd('option', $default);
}
else
{
// Use the default value if getInput method does not exist
self::$option = $default;
}
}
return self::$option;
}
/**
* Gets the component code name
*
* @param string|null $option The option for the component.
* @param string|null $default The default return value if none is found
*
* @return string|null A component code name
* @since 3.0.11
*/
public static function getCode(?string $option = null, ?string $default = null): ?string
{
// check that we have an option
if (empty($option))
{
$option = self::getOption();
}
// option with com_
if (is_string($option) && strpos($option, 'com_') === 0)
{
return strtolower(trim(substr($option, 4)));
}
return $default;
}
/**
* Gets the component abstract helper class
*
* @param string|null $option The option for the component.
* @param string|null $default The default return value if none is found
*
* @return string|null A component helper name
*
* @since 3.0.11
*/
public static function get(?string $option = null, ?string $default = null): ?string
{
// check that we have an option
// and get the code name from it
if (($code_name = self::getCode($option, null)) !== null)
{
// we build the helper class name
$helper_name = '\\' . \ucfirst($code_name) . 'Helper';
// check if class exist
if (class_exists($helper_name))
{
return $helper_name;
}
// try loading namespace
if (($namespace = self::getNamespace($option)) !== null)
{
$name = \ucfirst($code_name) . 'Helper';
$namespace_helper = '\\' . $namespace . '\Administrator\Helper\\' . NamespaceHelper::safeSegment($name); // TODO target site or admin locations not just admin...
if (class_exists($namespace_helper))
{
return $namespace_helper;
}
}
}
return $default;
}
/**
* Gets the component namespace if set
*
* @param string|null $option The option for the component.
* @param string|null $default The default return value if none is found
*
* @return string|null A component namespace
*
* @since 3.0.11
*/
public static function getNamespace(?string $option = null): ?string
{
$manifest = self::getManifest($option);
return $manifest->namespace ?? null;
}
/**
* Gets the component abstract helper class
*
* @param string|null $option The option for the component.
* @param string|null $default The default return value if none is found
*
* @return object|null A component helper name
*
* @since 3.0.11
*/
public static function getManifest(?string $option = null): ?object
{
if ($option === null
&& ($option = self::getOption($option)) === null)
{
return null;
}
// get global manifest_cache values
if (!isset(self::$manifest[$option]))
{
$db = Factory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('manifest_cache'))
->from($db->quoteName('#__extensions'))
->where($db->quoteName('type') . ' = ' . $db->quote('component'))
->where($db->quoteName('element') . ' LIKE ' . $db->quote($option));
$db->setQuery($query);
try {
$manifest = $db->loadResult();
self::$manifest[$option] = json_decode($manifest);
} catch (\Exception $e) {
// Handle the database error appropriately.
self::$manifest[$option] = null;
}
}
return self::$manifest[$option];
}
/**
* Check if the helper class of this component has a method
*
* @param string $method The method name to search for
* @param string|null $option The option for the component.
*
* @return bool true if method exist
*
* @since 3.0.11
*/
public static function methodExists(string $method, ?string $option = null): bool
{
// get the helper class
return ($helper = self::get($option, null)) !== null &&
method_exists($helper, $method);
}
/**
* Check if the helper class of this component has a method, and call it with the arguments
*
* @param string $method The method name to search for
* @param array $arguments The arguments for function.
* @param string|null $option The option for the component.
*
* @return mixed return whatever the method returns or null
* @since 3.2.0
*/
public static function _(string $method, array $arguments = [], ?string $option = null)
{
// get the helper class
if (($helper = self::get($option, null)) !== null &&
method_exists($helper, $method))
{
// we know this is not ideal...
// so we need to move these
// functions to their own classes
return call_user_func_array([$helper, $method], $arguments);
}
return null;
}
}