|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package Cadmium\System\Modules\Extend |
|
5
|
|
|
* @author Anton Romanov |
|
6
|
|
|
* @copyright Copyright (c) 2015-2017, Anton Romanov |
|
7
|
|
|
* @link http://cadmium-cms.com |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Modules\Extend\Utils { |
|
11
|
|
|
|
|
12
|
|
|
use Utils\Schema, Arr, JSON, Explorer; |
|
13
|
|
|
|
|
14
|
|
|
abstract class Loader { |
|
15
|
|
|
|
|
16
|
|
|
protected $dir_name = '', $items = []; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Load items |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
protected function loadItems(array $pattern = []) : array { |
|
23
|
|
|
|
|
24
|
|
|
$items = []; |
|
25
|
|
|
|
|
26
|
|
|
foreach (Explorer::iterateDirs($this->dir_name) as $name) { |
|
27
|
|
|
|
|
28
|
|
|
if (null !== ($data = $this->loadItem($name))) $items[$name] = ($pattern + $data); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
# ------------------------ |
|
32
|
|
|
|
|
33
|
|
|
return Arr::sortby($items, 'title'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Load an item data |
|
38
|
|
|
* |
|
39
|
|
|
* @return array|null : the data or null on failure |
|
40
|
|
|
*/ |
|
41
|
|
|
|
|
42
|
|
|
protected function loadItem(string $name) { |
|
43
|
|
|
|
|
44
|
|
|
$path = ($this->dir_name . $name . '/'); |
|
45
|
|
|
|
|
46
|
|
|
if (null === ($data = JSON::load($path . '.Config.json'))) return null; |
|
47
|
|
|
|
|
48
|
|
|
if (null === ($data = Schema::get(static::$schema_prototype)->validate($data))) return null; |
|
49
|
|
|
|
|
50
|
|
|
if (!(static::$extension_class::isValid($data['name']) && ($data['name'] === $name))) return null; |
|
51
|
|
|
|
|
52
|
|
|
# ------------------------ |
|
53
|
|
|
|
|
54
|
|
|
return (['path' => $path] + $data); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get the directory name |
|
59
|
|
|
*/ |
|
60
|
|
|
|
|
61
|
|
|
public function getDirName() : string { |
|
62
|
|
|
|
|
63
|
|
|
return $this->dir_name; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get an item |
|
68
|
|
|
* |
|
69
|
|
|
* @return array|false : the item or false if the item does not exist |
|
70
|
|
|
*/ |
|
71
|
|
|
|
|
72
|
|
|
public function getItem(string $name) { |
|
73
|
|
|
|
|
74
|
|
|
return ($this->items[$name] ?? false); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get the items list |
|
79
|
|
|
* |
|
80
|
|
|
* @param $plain : tells to return the array in simplified format ([$name => $title]) |
|
81
|
|
|
*/ |
|
82
|
|
|
|
|
83
|
|
|
public function getItems(bool $plain = false) : array { |
|
84
|
|
|
|
|
85
|
|
|
return ($plain ? array_column($this->items, 'title', 'name') : $this->items); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|