1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Modules\Extend\Utils { |
4
|
|
|
|
5
|
|
|
use Exception, Modules\Settings, Arr, Cookie, Explorer, Request; |
6
|
|
|
|
7
|
|
|
trait Extension { |
8
|
|
|
|
9
|
|
|
private static $section = '', $dir_name = '', $items = [], $active = '', $primary = ''; |
10
|
|
|
|
11
|
|
|
# Get section |
12
|
|
|
|
13
|
|
|
private static function getSection(string $section) { |
14
|
|
|
|
15
|
|
|
return (($section === SECTION_ADMIN) ? SECTION_ADMIN : SECTION_SITE); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
# Get directory name |
19
|
|
|
|
20
|
|
|
private static function getDirName(string $section) { |
21
|
|
|
|
22
|
|
|
return (self::$root_dir . (self::$separate ? ($section . '/') : '')); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
# Get items |
26
|
|
|
|
27
|
|
|
private static function getItems(string $dir_name) { |
28
|
|
|
|
29
|
|
|
$items = []; |
30
|
|
|
|
31
|
|
|
foreach (Explorer::listDirs($dir_name) as $name) { |
32
|
|
|
|
33
|
|
|
$file_name = ($dir_name . $name . '/Config.php'); |
34
|
|
|
|
35
|
|
|
if (!is_array($include = Explorer::php($file_name))) continue; |
36
|
|
|
|
37
|
|
|
$config = Arr::select($include, self::$data); |
38
|
|
|
|
39
|
|
|
if (!(self::valid($config['name']) && ($config['name'] === $name))) continue; |
40
|
|
|
|
41
|
|
|
$items[$name] = $config; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
# ------------------------ |
45
|
|
|
|
46
|
|
|
ksort($items); return $items; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
# Get user defined extension name |
50
|
|
|
|
51
|
|
|
private static function getUserDefined() { |
52
|
|
|
|
53
|
|
|
$name = ''; $param = self::$param[self::$section]; |
54
|
|
|
|
55
|
|
|
if (self::exists($name_cookie = Cookie::get($param))) $name = $name_cookie; |
56
|
|
|
|
57
|
|
|
if (self::exists($name_get = Request::get(self::$name))) $name = $name_get; |
58
|
|
|
|
59
|
|
|
if ('' !== $name) Cookie::set($param, $name, self::$cookie_expires); else return false; |
60
|
|
|
|
61
|
|
|
# ------------------------ |
62
|
|
|
|
63
|
|
|
return $name; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
# Check if name valid |
67
|
|
|
|
68
|
|
|
public static function valid(string $name) { |
69
|
|
|
|
70
|
|
|
return (preg_match(self::$regex_name, $name) ? true : false); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
# Validate name |
74
|
|
|
|
75
|
|
|
public static function validate(string $name) { |
76
|
|
|
|
77
|
|
|
return (self::valid($name) ? $name : false); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
# Check if extension exists |
81
|
|
|
|
82
|
|
|
public static function exists(string $name) { |
83
|
|
|
|
84
|
|
|
return (self::valid($name) && isset(self::$items[$name])); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
# Init extensions list |
88
|
|
|
|
89
|
|
|
public static function init($section) { |
90
|
|
|
|
91
|
|
|
$section = self::getSection($section); $dir_name = self::getDirName($section); |
92
|
|
|
|
93
|
|
|
if (!Explorer::isDir($dir_name)) throw new Exception\General(self::$error_directory); |
94
|
|
|
|
95
|
|
|
self::$section = $section; self::$dir_name = $dir_name; self::$items = self::getItems($dir_name); |
96
|
|
|
|
97
|
|
|
$selectable = self::$selectable[$section]; $param = self::$param[$section]; |
98
|
|
|
|
99
|
|
|
$primary = (self::exists(self::$default[$section]) ? self::$default[$section] : false); |
100
|
|
|
|
101
|
|
|
if ($selectable && (false !== ($name = self::getUserDefined()))) $name_valid = true; |
102
|
|
|
|
103
|
|
|
else $name_valid = (self::exists($name = Settings::get($param)) || (false !== ($name = $primary))); |
104
|
|
|
|
105
|
|
|
if (!($name_valid || (null !== ($name = key(self::$items))))) throw new Exception\General(self::$error_select); |
106
|
|
|
|
107
|
|
|
# ------------------------ |
108
|
|
|
|
109
|
|
|
self::$active = $name; self::$primary = $primary; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
# Return active section |
113
|
|
|
|
114
|
|
|
public static function section() { |
115
|
|
|
|
116
|
|
|
return self::$section; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
# Return directory name |
120
|
|
|
|
121
|
|
|
public static function dirName() { |
122
|
|
|
|
123
|
|
|
return self::$dir_name; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
# Return items |
127
|
|
|
|
128
|
|
|
public static function items(string $section = null) { |
129
|
|
|
|
130
|
|
|
if (null === $section) return self::$items; |
131
|
|
|
|
132
|
|
|
$section = self::getSection($section); $dir_name = self::getDirName($section); |
133
|
|
|
|
134
|
|
|
return (($dir_name !== self::$dir_name) ? self::getItems($dir_name) : self::$items); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
# Return active extension name |
138
|
|
|
|
139
|
|
|
public static function active() { |
140
|
|
|
|
141
|
|
|
return self::$active; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
# Return primary extension name |
145
|
|
|
|
146
|
|
|
public static function primary() { |
147
|
|
|
|
148
|
|
|
return self::$primary; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
# Get active extension path |
152
|
|
|
|
153
|
|
|
public static function path() { |
154
|
|
|
|
155
|
|
|
if ('' === self::$active) return false; |
156
|
|
|
|
157
|
|
|
return (self::$dir_name . self::$active . '/'); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
# Get primary extension path |
161
|
|
|
|
162
|
|
|
public static function pathPrimary() { |
163
|
|
|
|
164
|
|
|
if ('' === self::$primary) return false; |
165
|
|
|
|
166
|
|
|
return (self::$dir_name . self::$primary . '/'); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
# Get active extension data |
170
|
|
|
|
171
|
|
|
public static function data(string $name = null) { |
172
|
|
|
|
173
|
|
|
if ('' === self::$active) return false; |
174
|
|
|
|
175
|
|
|
if (null === $name) return self::$items[self::$active]; |
176
|
|
|
|
177
|
|
|
return (isset(self::$items[self::$active][$name]) ? self::$items[self::$active][$name] : false); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|