-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathConfig.php
344 lines (313 loc) · 10.9 KB
/
Config.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
/**
* @package yii2-krajee-base
* @author Kartik Visweswaran <kartikv2@gmail.com>
* @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014 - 2022
* @version 3.0.5
*/
namespace kartik\base;
use Yii;
use ReflectionClass;
use yii\base\InvalidConfigException;
/**
* Global configuration helper class for Krajee extensions.
*
* @author Kartik Visweswaran <kartikv2@gmail.com>
*/
class Config
{
/**
* Krajee repo vendor name
*/
const VENDOR_NAME = 'kartik-v/';
/**
* Krajee extension namespace
*/
const NAMESPACE_PREFIX = '\\kartik\\';
/**
* default reason appended for exceptions
*/
const DEFAULT_REASON = 'for your selected functionality';
/**
* @var array list of valid html inputs
*/
protected static $_validHtmlInputs = [
'hiddenInput',
'textInput',
'passwordInput',
'textArea',
'checkbox',
'radio',
'listBox',
'dropDownList',
'checkboxList',
'radioList',
'input',
'fileInput',
];
/**
* @var array list of valid dropdown inputs
*/
protected static $_validDropdownInputs = [
'listBox',
'dropDownList',
'checkboxList',
'radioList',
'checkboxButtonGroup',
'radioButtonGroup',
];
/**
* @var array list of valid Krajee input widgets
*/
protected static $_validInputWidgets = [
'\kartik\typeahead\Typeahead' => ['yii2-widgets', 'yii2-widget-typeahead'],
'\kartik\select2\Select2' => ['yii2-widgets', 'yii2-widget-select2'],
'\kartik\depdrop\DepDrop' => ['yii2-widgets', 'yii2-widget-depdrop'],
'\kartik\touchspin\TouchSpin' => ['yii2-widgets', 'yii2-widget-touchspin'],
'\kartik\switchinput\SwitchInput' => ['yii2-widgets', 'yii2-widget-switchinput'],
'\kartik\rating\StarRating' => ['yii2-widgets', 'yii2-widget-rating'],
'\kartik\file\FileInput' => ['yii2-widgets', 'yii2-widget-fileinput'],
'\kartik\range\RangeInput' => ['yii2-widgets', 'yii2-widget-rangeinput'],
'\kartik\color\ColorInput' => ['yii2-widgets', 'yii2-widget-colorinput'],
'\kartik\date\DatePicker' => ['yii2-widgets', 'yii2-widget-datepicker'],
'\kartik\time\TimePicker' => ['yii2-widgets', 'yii2-widget-timepicker'],
'\kartik\datetime\DateTimePicker' => ['yii2-widgets', 'yii2-widget-datetimepicker'],
'\kartik\daterange\DateRangePicker' => 'yii2-date-range',
'\kartik\sortinput\SortableInput' => 'yii2-sortinput',
'\kartik\tree\TreeViewInput' => 'yii2-tree-manager',
'\kartik\money\MaskMoney' => 'yii2-money', // deprecated and replaced by yii2-number
'\kartik\number\NumberControl' => 'yii2-number',
'\kartik\checkbox\CheckboxX' => 'yii2-checkbox-x',
'\kartik\slider\Slider' => 'yii2-slider',
];
/**
* Validate multiple extension dependencies.
*
* @param array $extensions the configuration of extensions with each array item setup as required in
* `checkDependency` method. The following keys can be setup:
*
* - `name`: _string_, the extension class name (without vendor namespace prefix)
* - `repo`: _string_, the extension package repository name (without vendor name prefix)
* - `reason`: _string_, a user friendly message for dependency validation failure
*
* @throws InvalidConfigException if extension fails dependency validation
*/
public static function checkDependencies($extensions = [])
{
foreach ($extensions as $extension) {
$name = empty($extension[0]) ? '' : $extension[0];
$repo = empty($extension[1]) ? '' : $extension[1];
$reason = empty($extension[2]) ? '' : self::DEFAULT_REASON;
static::checkDependency($name, $repo, $reason);
}
}
/**
* Validate a single extension dependency
*
* @param string $name the extension class name (without vendor namespace prefix)
* @param mixed $repo the extension package repository names (without vendor name prefix)
* @param string $reason a user friendly message for dependency validation failure
*
* @throws InvalidConfigException if extension fails dependency validation
*/
public static function checkDependency($name = '', $repo = '', $reason = self::DEFAULT_REASON)
{
if (empty($name)) {
return;
}
$command = 'php composer.phar require ';
$version = ' \'@dev\'';
$class = (Lib::substr($name, 0, 8) == self::NAMESPACE_PREFIX) ? $name : self::NAMESPACE_PREFIX.$name;
if (is_array($repo)) {
$repos = "one of '".implode("' OR '", $repo)."' extensions. ";
$installs = $command;
foreach ($repo as $r) {
$sep = $installs === $command ? '' : "{$version}\n\n--- OR ---\n\n{$command}";
$r = Lib::strpos($r, '/') === false ? self::VENDOR_NAME.$r : $r;
$installs .= $sep.$r;
}
$installs .= $version;
} else {
$repo = Lib::strpos($repo, '/') === false ? self::VENDOR_NAME.$repo : $repo;
$repos = "the '".$repo."' extension. ";
$installs = $command.$repo.$version;
}
if (!class_exists($class)) {
throw new InvalidConfigException(
"The class '{$class}' was not found and is required {$reason}.\n\n".
"Please ensure you have installed {$repos}".
"To install, you can run this console command from your application root:\n\n{$installs}"
);
}
}
/**
* Gets list of namespaced Krajee input widget classes as an associative array, where the array keys are the
* namespaced classes, and the array values are the names of the github repository to which these classes belong to.
*
* @return array
*/
public static function getInputWidgets()
{
return static::$_validInputWidgets;
}
/**
* Check if a type of input is any possible valid input (html or widget)
*
* @param string $type the type of input
*
* @return boolean
*/
public static function isValidInput($type)
{
return static::isHtmlInput($type) || static::isInputWidget($type) || $type === 'widget';
}
/**
* Check if a input type is a valid Html Input
*
* @param string $type the type of input
*
* @return boolean
*/
public static function isHtmlInput($type)
{
return in_array($type, static::$_validHtmlInputs);
}
/**
* Check if a type of input is a valid input widget
*
* @param string $type the type of input
*
* @return boolean
*/
public static function isInputWidget($type)
{
return isset(static::$_validInputWidgets[$type]);
}
/**
* Check if a input type is a valid dropdown input
*
* @param string $type the type of input
*
* @return boolean
*/
public static function isDropdownInput($type)
{
return in_array($type, static::$_validDropdownInputs);
}
/**
* Check if a namespaced widget is valid or installed.
*
* @param string $type the widget type
* @param string $reason the message to be displayed for dependency failure
*
* @throws InvalidConfigException
*/
public static function validateInputWidget($type, $reason = self::DEFAULT_REASON)
{
if (static::isInputWidget($type)) {
static::checkDependency($type, static::$_validInputWidgets[$type], $reason);
}
}
/**
* Convert a language string in yii\i18n format to a ISO-639 format (2 or 3 letter code).
*
* @param string $language the input language string
*
* @return string
*/
public static function getLang($language)
{
$pos = Lib::strpos($language, '-');
return $pos > 0 ? Lib::substr($language, 0, $pos) : $language;
}
/**
* Get the current directory of the extended class object
*
* @param object $object the called object instance
*
* @return string
*/
public static function getCurrentDir($object)
{
if (empty($object)) {
return '';
}
$child = new ReflectionClass($object);
return dirname($child->getFileName());
}
/**
* Check if a file exists
*
* @param string $file the file with path in URL format
*
* @return bool
*/
public static function fileExists($file)
{
$file = Lib::str_replace('/', DIRECTORY_SEPARATOR, $file);
return file_exists($file);
}
/**
* Initializes and validates the module (deprecated since v1.9.0 - use `getModule` instead directly)
*
* @param string $class the Module class name
*
* @return \yii\base\Module
*
* @throws InvalidConfigException
*/
public static function initModule($class)
{
$m = $class::MODULE;
$module = $m ? static::getModule($m) : null;
if ($module === null || !$module instanceof $class) {
throw new InvalidConfigException("The '{$m}' module MUST be setup in your Yii configuration file and must be an instance of '{$class}'.");
}
return $module;
}
/**
* Gets the module instance by validating the module name. The check is first done for a submodule of the same name
* and then the check is done for the module within the current Yii2 application.
*
* @param string $m the module identifier
* @param string $class the module class name
*
* @return yii\base\Module
* @throws InvalidConfigException
*
*/
public static function getModule($m, $class = '')
{
$app = Yii::$app;
$mod = isset($app->controller) && $app->controller->module ? $app->controller->module : null;
$module = null;
if ($mod) {
$module = $mod->id === $m ? $mod : $mod->getModule($m);
}
if (!$module) {
$module = $app->getModule($m);
}
if ($module === null) {
throw new InvalidConfigException("The '{$m}' module MUST be setup in your Yii configuration file.");
}
if (!empty($class) && !$module instanceof $class) {
throw new InvalidConfigException("The '{$m}' module MUST be an instance of '{$class}'.");
}
return $module;
}
/**
* Check if HTML options has specified CSS class
* @param array $options the HTML options
* @param string $cssClass the css class to test
* @return bool
*/
public static function hasCssClass($options, $cssClass)
{
if (!isset($options['class'])) {
return false;
}
$classes = is_array($options['class']) ? $options['class'] :
Lib::preg_split('/\s+/', $options['class'], -1, PREG_SPLIT_NO_EMPTY);
return in_array($cssClass, $classes);
}
}