1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Modules\Extend\Utils\Loader { |
4
|
|
|
|
5
|
|
|
use Modules\Extend, Utils\Schema, Arr, Explorer; |
6
|
|
|
|
7
|
|
|
abstract class Addons extends Extend\Utils\Loader { |
8
|
|
|
|
9
|
|
|
# Get items |
10
|
|
|
|
11
|
|
|
protected function getItems() { |
12
|
|
|
|
13
|
|
|
$items = []; |
14
|
|
|
|
15
|
|
|
foreach (Explorer::iterateDirs($this->dir_name) as $name) { |
16
|
|
|
|
17
|
|
|
if (null !== ($data = $this->getItem($name))) { |
18
|
|
|
|
19
|
|
|
$items[$name] = $data; $items[$name]['installed'] = false; |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
View Code Duplication |
foreach ((Schema::get(static::$schema)->load() ?? []) as $item) { |
|
|
|
|
24
|
|
|
|
25
|
|
|
if (isset($items[$item['name']])) $items[$item['name']]['installed'] = true; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
# ------------------------ |
29
|
|
|
|
30
|
|
|
return Arr::sortby($items, 'title'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
# Get installed items |
34
|
|
|
|
35
|
|
|
protected function getItemsInstalled() { |
36
|
|
|
|
37
|
|
|
$items = []; |
38
|
|
|
|
39
|
|
View Code Duplication |
foreach ((Schema::get(static::$schema)->load() ?? []) as $item) { |
|
|
|
|
40
|
|
|
|
41
|
|
|
if (static::$extension_class::valid($item['name'])) $items[$item['name']] = $item; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
# ------------------------ |
45
|
|
|
|
46
|
|
|
return $items; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
# Constructor |
50
|
|
|
|
51
|
|
|
public function __construct(bool $load_all = true) { |
52
|
|
|
|
53
|
|
|
$this->dir_name = static::$root_dir; |
54
|
|
|
|
55
|
|
|
$this->items = ($load_all ? $this->getItems() : $this->getItemsInstalled()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
# Install/uninstall item |
59
|
|
|
|
60
|
|
|
public function install(string $name, bool $value = true) { |
61
|
|
|
|
62
|
|
|
if (!isset($this->items[$name])) return false; |
63
|
|
|
|
64
|
|
|
$items = []; |
65
|
|
|
|
66
|
|
|
foreach ($this->items as $item) { |
67
|
|
|
|
68
|
|
|
if ($value) { if ($item['installed'] || ($item['name'] === $name)) $items[] = $item; } |
69
|
|
|
|
70
|
|
|
else { if ($item['installed'] && ($item['name'] !== $name)) $items[] = $item; } |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (!Schema::get(static::$schema)->save($items)) return false; |
74
|
|
|
|
75
|
|
|
$this->items[$name]['installed'] = $value; |
76
|
|
|
|
77
|
|
|
# ------------------------ |
78
|
|
|
|
79
|
|
|
return true; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.