forked from kartik-v/yii2-krajee-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.php
216 lines (196 loc) · 7.43 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
<?php
/**
* @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014
* @package yii2-krajee-base
* @version 1.5.0
*/
namespace kartik\base;
use Yii;
use yii\base\InvalidConfigException;
/**
* Global configuration helper class for Krajee extensions
*
* @author Kartik Visweswaran <[email protected]>
* @since 1.0
*/
class Config
{
const VENDOR_NAME = "kartik-v/";
const NAMESPACE_PREFIX = "\\kartik\\";
const DEFAULT_REASON = "for your selected functionality";
protected static $_validHtmlInputs = [
'hiddenInput',
'textInput',
'passwordInput',
'textArea',
'checkbox',
'radio',
'listBox',
'dropDownList',
'checkboxList',
'radioList',
'input',
'fileInput'
];
protected static $_validDropdownInputs = [
'listBox',
'dropDownList',
'checkboxList',
'radioList'
];
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\rating\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-daterange',
'\kartik\sortinput\SortableInput' => 'yii2-sortinput',
'\kartik\money\MaskMoney' => 'yii2-money',
'\kartik\checkbox\CheckboxX' => 'yii2-checkbox',
'\kartik\slider\Slider' => 'yii2-slider',
];
/**
* Validate a single extension dependency
* @param string name the extension class name (without vendor namespace prefix)
* @param string|array 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 " . self::VENDOR_NAME;
$version = ": \"@dev\"";
$class = (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 . implode("{$version}\n\n--- OR ---\n\n{$command}", $repo) . $version;
} else {
$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);
}
}
/**
* 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);
}
}
/**
* 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.
* @returns 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
* @returns bool
*/
public static function isValidInput($type)
{
return static::isHtmlInput($type) || static::isInputWidget($type) || $type === 'widget';
}
/**
* Check if a type of input is a valid input widget
* @param string $type the type of input
* @returns bool
*/
public static function isInputWidget($type)
{
return isset(static::$_validInputWidgets[$type]);
}
/**
* Check if a input type is a valid Html Input
* @param string $type the type of input
* @returns bool
*/
public static function isHtmlInput($type)
{
return in_array($type, static::$_validHtmlInputs);
}
/**
* Check if a input type is a valid dropdown input
* @param string $type the type of input
* @returns bool
*/
public static function isDropdownInput($type)
{
return in_array($type, static::$_validDropdownInputs);
}
/**
* Check if a namespaced widget is valid or installed.
* @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 = strpos($language, "-");
return $pos > 0 ? substr($language, 0, $pos) : $language;
}
/**
* Get the current directory of the extended class object
* @param mixed $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 = str_replace('/', DIRECTORY_SEPARATOR, $file);
return file_exists($file);
}
}