|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Modules\Extend\Utils\Loader { |
|
4
|
|
|
|
|
5
|
|
|
use Modules\Extend, Modules\Settings, Arr, Explorer; |
|
6
|
|
|
|
|
7
|
|
|
abstract class Basic extends Extend\Utils\Loader { |
|
8
|
|
|
|
|
9
|
|
|
protected $section = '', $loaded = false, $primary = null, $active = null; |
|
10
|
|
|
|
|
11
|
|
|
# Get section |
|
12
|
|
|
|
|
13
|
|
|
protected function getSection(string $section) { |
|
14
|
|
|
|
|
15
|
|
|
return (($section === SECTION_ADMIN) ? SECTION_ADMIN : SECTION_SITE); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
# Get directory name |
|
19
|
|
|
|
|
20
|
|
|
protected function getDirName() { |
|
21
|
|
|
|
|
22
|
|
|
return static::$root_dir[$this->section]; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
# Get item |
|
26
|
|
|
|
|
27
|
|
|
protected function getItem(string $name) { |
|
28
|
|
|
|
|
29
|
|
|
return ($this->loaded ? ($this->items[$name] ?? null) : parent::getItem($name)); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
# Get first item |
|
33
|
|
|
|
|
34
|
|
|
protected function getFirst() { |
|
35
|
|
|
|
|
36
|
|
|
if ($this->loaded) return ($this->items[key($this->items)] ?? null); |
|
37
|
|
|
|
|
38
|
|
View Code Duplication |
foreach (Explorer::iterateDirs($this->dir_name) as $name) { |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
if (null !== ($data = $this->getItem($name))) return $data; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
# ------------------------ |
|
44
|
|
|
|
|
45
|
|
|
return null; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
# Get items |
|
49
|
|
|
|
|
50
|
|
|
protected function getItems() { |
|
51
|
|
|
|
|
52
|
|
|
$items = []; |
|
53
|
|
|
|
|
54
|
|
View Code Duplication |
foreach (Explorer::iterateDirs($this->dir_name) as $name) { |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
if (null !== ($data = $this->getItem($name))) $items[$name] = $data; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
# ------------------------ |
|
60
|
|
|
|
|
61
|
|
|
return Arr::sortby($items, 'title'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
# Get primary item |
|
65
|
|
|
|
|
66
|
|
|
protected function getPrimary() { |
|
67
|
|
|
|
|
68
|
|
|
$primary = static::$default[$this->section]; |
|
69
|
|
|
|
|
70
|
|
|
return ($this->getItem($primary) ?? null); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
# Get active item |
|
74
|
|
|
|
|
75
|
|
|
protected function getActive() { |
|
76
|
|
|
|
|
77
|
|
|
$active = Settings::get(static::$param[$this->section]); |
|
78
|
|
|
|
|
79
|
|
|
return ($this->getItem($active) ?? $this->primary ?? $this->getFirst()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
# Constructor |
|
83
|
|
|
|
|
84
|
|
|
public function __construct(string $section, bool $load_all = true) { |
|
85
|
|
|
|
|
86
|
|
|
$this->section = $this->getSection($section); $this->dir_name = $this->getDirName(); |
|
87
|
|
|
|
|
88
|
|
|
if ($load_all) { $this->items = $this->getItems(); $this->loaded = true; } |
|
89
|
|
|
|
|
90
|
|
|
$this->primary = $this->getPrimary(); $this->active = $this->getActive(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
# Activate item |
|
94
|
|
|
|
|
95
|
|
|
public function activate(string $name) { |
|
96
|
|
|
|
|
97
|
|
|
if (null === ($data = $this->getItem($name))) return false; |
|
98
|
|
|
|
|
99
|
|
|
$this->active = $data; |
|
100
|
|
|
|
|
101
|
|
|
# ------------------------ |
|
102
|
|
|
|
|
103
|
|
|
return true; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
# Return active section |
|
107
|
|
|
|
|
108
|
|
|
public function section() { |
|
109
|
|
|
|
|
110
|
|
|
return $this->section; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
# Return primary extension name |
|
114
|
|
|
|
|
115
|
|
|
public function primary() { |
|
116
|
|
|
|
|
117
|
|
|
return ($this->primary['name'] ?? false); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
# Return active extension name |
|
121
|
|
|
|
|
122
|
|
|
public function active() { |
|
123
|
|
|
|
|
124
|
|
|
return ($this->active['name'] ?? false); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
# Get primary extension path |
|
128
|
|
|
|
|
129
|
|
|
public function pathPrimary() { |
|
130
|
|
|
|
|
131
|
|
|
if (null === $this->primary) return false; |
|
132
|
|
|
|
|
133
|
|
|
return ($this->dir_name . $this->primary['name'] . '/'); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
# Get active extension path |
|
137
|
|
|
|
|
138
|
|
|
public function path() { |
|
139
|
|
|
|
|
140
|
|
|
if (null === $this->active) return false; |
|
141
|
|
|
|
|
142
|
|
|
return ($this->dir_name . $this->active['name'] . '/'); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
# Get active extension data |
|
146
|
|
|
|
|
147
|
|
|
public function data(string $name = null) { |
|
148
|
|
|
|
|
149
|
|
|
if (null === $name) return ($this->active ?? false); |
|
150
|
|
|
|
|
151
|
|
|
return ($this->active[$name] ?? false); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
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.