This repository was archived by the owner on Aug 19, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplement.php
More file actions
391 lines (340 loc) · 10.9 KB
/
Complement.php
File metadata and controls
391 lines (340 loc) · 10.9 KB
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
<?php
/**
* PHP library for adding addition of complements for Eliasis Framework.
*
* @author Josantonius <hello@josantonius.com>
* @copyright 2017 - 2018 (c) Josantonius - Eliasis Complement
* @license https://opensource.org/licenses/MIT - The MIT License (MIT)
* @link https://github.com/eliasis-framework/complement
* @since 1.0.9
*/
namespace Eliasis\Complement;
use Eliasis\Complement\Exception\ComplementException;
use Eliasis\Framework\App;
use Josantonius\File\File;
use Josantonius\Json\Json;
use Josantonius\Url\Url;
/**
* Complement class.
*/
abstract class Complement
{
use Traits\ComplementHandler;
use Traits\ComplementAction;
use Traits\ComplementImport;
use Traits\ComplementState;
use Traits\ComplementRequest;
use Traits\ComplementView;
/**
* Complement instances.
*
* @var array
*/
protected static $instances;
/**
* Available complements.
*
* @var array
*/
protected $complement = [];
/**
* Id of current complement called.
*
* @var array
*/
protected static $id = 'Default';
/**
* Errors for file management.
*
* @var array
*/
protected static $errors = [];
/**
* Complement type.
*
* @var string
*/
private static $type = 'default';
/**
* Receives the name of the complement to execute: Complement::Name().
*
* @param string $index → complement name
* @param array $params → params
*
* @uses \Eliasis\Framework\App::getCurrentID()
* @uses \Eliasis\Complement\ComplementHandler::getType()
*
* @throws ComplementException → complement not found
*
* @return object
*/
public static function __callstatic($index, $params = false)
{
$type = self::getType();
$appID = App::getCurrentID();
if (! array_key_exists($index, self::$instances[$appID][$type])) {
$msg = self::getType('ucfirst', false) . ' or method not found';
throw new ComplementException($msg . ': ' . $index);
}
self::$id = $index;
$that = self::getComplementInstance();
if (! $params) {
return $that;
}
$method = (isset($params[0])) ? $params[0] : '';
$args = (isset($params[1])) ? $params[1] : 0;
if (method_exists($that, $method)) {
return call_user_func_array([$that, $method], [$args]);
}
}
/**
* Load all complements found in the directory.
*
* @uses \Eliasis\Complement\ComplementRequest::requestHandler()
* @uses \Eliasis\Complement\ComplementHandler::getType()
*/
public static function run()
{
if (! session_id()) {
session_start();
}
$complementType = self::getType('strtoupper');
$path = App::$complementType();
if ($paths = File::getFilesFromDir($path)) {
foreach ($paths as $path) {
if (! $path->isDot() && $path->isDir()) {
$_path = Url::addBackSlash($path->getPath());
$slug = $path->getBasename();
$file = $_path . $slug . '/' . $slug . '.json';
if (! File::exists($file)) {
continue;
}
self::load($file);
}
}
}
self::requestHandler(self::getType('strtolower', false));
}
/**
* Load complement configuration from json file and set settings.
*
* @param string $file → path or url to the complement configuration file
*
* @uses \Josantonius\Json\Json::fileToArray()
* @uses \Eliasis\Complement\ComplementHandler->setComplement()
*
* @return bool true
*/
public static function load($file)
{
$complement = Json::fileToArray($file);
$complement['config-file'] = $file;
self::$id = isset($complement['id']) ? $complement['id'] : 'Default';
$that = self::getComplementInstance();
return $that->setComplement($complement);
}
/**
* Get components/plugins/modules/templates list.
*
* @param string $filter → complement category filter
* @param string $sort → PHP sorting function to complements sort
*
* @uses \Eliasis\Complement\ComplementHandler::getType()
* @uses \Eliasis\Framework\App::getCurrentID()
*
* @return array $data → complements list
*/
public static function getList($filter = 'all', $sort = 'asort')
{
$data = [];
$type = self::getType();
$complementID = self::getCurrentID();
$appID = App::getCurrentID();
$complements = array_keys(self::$instances[$appID][$type]);
foreach ($complements as $id) {
self::setCurrentID($id);
$that = self::getComplementInstance();
$complement = $that->complement;
if (! isset($complement['category'])) {
continue;
}
$skip = ($filter != 'all' && $complement['category'] != $filter);
if ($skip || $id == 'Default' || ! $complement) {
continue;
}
if ($that->hasNewVersion() && $complement['state'] === 'active') {
$complement['state'] = 'outdated';
$that->setState('outdated');
}
$data[$complement['id']] = [
'id' => $complement['id'],
'name' => $complement['name'],
'version' => $complement['version'],
'description' => $complement['description'],
'state' => $complement['state'],
'category' => $complement['category'],
'path' => $complement['path']['root'],
'url' => $complement['url'],
'author' => $complement['author'],
'author-url' => $complement['author-url'],
'license' => $complement['license'],
'state' => $complement['state'],
'slug' => $complement['slug'],
'image' => $complement['image'],
'hooks-controller' => $complement['hooks-controller'],
'url-import' => $complement['url-import'],
'extra' => $complement['extra']
];
}
self::setCurrentID($complementID);
self::getComplementInstance();
$sorting = '|asort|arsort|krsort|ksort|rsort|shuffle|sort|';
strpos($sorting, $sort) ? $sort($data) : asort($data);
return $data;
}
/**
* Get the current complement ID.
*
* @since 1.1.0
*
* @return string → complement ID
*/
public static function getCurrentID()
{
return self::$id;
}
/**
* Define the current complement ID.
*
* @since 1.1.0
*
* @param string $id → complement ID
*
* @return bool
*/
public static function setCurrentID($id)
{
$type = self::getType();
$appID = App::getCurrentID();
if (array_key_exists($id, self::$instances[$appID][$type])) {
self::$id = $id;
return true;
}
return false;
}
/**
* Set and get script url.
*
* @param string $pathUrl → url where JS files will be created & loaded
* @param bool $vue → include Vue.js in the script
* @param bool $vueResource → include vue-resource in the script
*
* @uses \Eliasis\Complement\ComplementView::setFile()
*
* @return string → script url
*/
public static function script($pathUrl = null, $vue = true, $vueResource = true)
{
$that = self::getComplementInstance();
$file = $vue ? 'vue+' : '';
$file .= $vueResource ? 'vue-resource+' : '';
return $that->setFile($file . 'eliasis-complement.min', 'script', $pathUrl);
}
/**
* Set and get url style.
*
* @param string $pathUrl → url where CSS files will be created & loaded
*
* @uses \Eliasis\Complement\ComplementView::setFile()
*
* @return array → urls of the styles
*/
public static function style($pathUrl = null)
{
$that = self::getComplementInstance();
return $that->setFile('eliasis-complement.min', 'style', $pathUrl);
}
/**
* Check if complement exists.
*
* @param string $complementID → complement id
*
* @uses \Eliasis\Framework\App::getCurrentID()
* @uses \Eliasis\Complement\ComplementHandler::getType()
*
* @return bool
*/
public static function exists($complementID)
{
$type = self::getType();
return array_key_exists(
$complementID,
self::$instances[App::getCurrentID()][$type]
);
}
/**
* Get library path.
*
* @return string → library path
*/
public static function getLibraryPath()
{
return Url::addBackSlash(dirname(__DIR__));
}
/**
* Get library version.
*
* @uses \Josantonius\Json\Json::fileToArray()
*
* @return string
*/
public static function getLibraryVersion()
{
$path = self::getLibraryPath();
$composer = Json::fileToArray($path . 'composer.json');
return isset($composer['version']) ? $composer['version'] : '1.1.1';
}
/**
* Get complements view.
*
* @param string $filter → complements category to display
* @param array $remote → urls of the remote optional complements
* @param string $sort → PHP sorting function to complements sort
* @param array $translations → translations for button texts
*
* @uses \Eliasis\Complement\ComplementView::renderizate()
*
* @return bool true
*/
public static function render($filter = 'all', $remote = null, $sort = 'asort', $translations = null)
{
$that = self::getComplementInstance();
$translations = $translations ?: [
'active' => 'active',
'activate' => 'activate',
'install' => 'install',
'update' => 'update',
'uninstall' => 'uninstall'
];
return $that->renderizate($filter, $remote, $sort, $translations);
}
/**
* Get complement instance.
*
* @uses \Eliasis\Framework\App::getCurrentID()
* @uses \Eliasis\Complement\ComplementHandler::getType()
*
* @return object → complement instance
*/
protected static function getComplementInstance()
{
$type = self::getType();
$appID = App::getCurrentID();
$complementID = self::getCurrentID();
$complement = get_called_class();
if (! isset(self::$instances[$appID][$type][$complementID])) {
self::$instances[$appID][$type][$complementID] = new $complement();
}
return self::$instances[$appID][$type][$complementID];
}
}