1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TwoDojo\Module; |
4
|
|
|
|
5
|
|
|
use TwoDojo\Module\Contracts\EventListener; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class AbstractModule |
9
|
|
|
* |
10
|
|
|
* @method onInitialize() |
11
|
|
|
* @method onEnabled() |
12
|
|
|
* @method onDisabled() |
13
|
|
|
*/ |
14
|
|
|
abstract class AbstractModule implements EventListener |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $name; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $uniqueName; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $snakeCaseName; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $description = ''; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
protected $major = 0; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var int |
43
|
|
|
*/ |
44
|
|
|
protected $minor = 0; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var int |
48
|
|
|
*/ |
49
|
|
|
protected $patch = 1; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
protected $authors = []; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
protected $modulePath; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var bool |
63
|
|
|
*/ |
64
|
|
|
protected $isCore = false; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Determine the module is enabled or not |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
public function isEnabled() : bool |
72
|
|
|
{ |
73
|
|
|
return true; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getSnakeCaseName() : string |
77
|
|
|
{ |
78
|
|
|
if (empty($this->snakeCaseName)) { |
79
|
|
|
$this->snakeCaseName = mb_strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $this->name), 'UTF-8'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->snakeCaseName; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get the module name |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function getName() : string |
91
|
|
|
{ |
92
|
|
|
return $this->name; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get the module description |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function getDescription() : string |
101
|
|
|
{ |
102
|
|
|
return $this->description; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get the module major version |
107
|
|
|
* |
108
|
|
|
* @return int |
109
|
|
|
*/ |
110
|
|
|
public function getMajor() : int |
111
|
|
|
{ |
112
|
|
|
return $this->major; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get the module minor version |
117
|
|
|
* |
118
|
|
|
* @return int |
119
|
|
|
*/ |
120
|
|
|
public function getMinor() : int |
121
|
|
|
{ |
122
|
|
|
return $this->minor; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get the module patch version |
127
|
|
|
* |
128
|
|
|
* @return int |
129
|
|
|
*/ |
130
|
|
|
public function getPatch() : int |
131
|
|
|
{ |
132
|
|
|
return $this->patch; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get the module version string |
137
|
|
|
* |
138
|
|
|
* @param boolean $includePrefix Include the version prefix or not. |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
public function getVersion($includePrefix = true) : string |
142
|
|
|
{ |
143
|
|
|
$version = "{$this->getMajor()}.{$this->getMinor()}.{$this->getPatch()}"; |
144
|
|
|
|
145
|
|
|
return $includePrefix ? 'v'.$version : $version; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get the module authors |
150
|
|
|
* |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
|
|
public function getAuthors() |
154
|
|
|
{ |
155
|
|
|
return $this->authors; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get the module prefix |
160
|
|
|
* |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
public function getUniqueName() |
164
|
|
|
{ |
165
|
|
|
if (empty($this->uniqueName)) { |
166
|
|
|
$this->uniqueName = get_class($this); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $this->uniqueName; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
|
|
public function getModulePath() |
176
|
|
|
{ |
177
|
|
|
if (empty($this->modulePath)) { |
178
|
|
|
$this->modulePath = dirname((new \ReflectionClass($this))->getFileName()); |
179
|
|
|
$this->modulePath = realpath($this->modulePath.'/../'); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $this->modulePath; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function isCoreModule() |
186
|
|
|
{ |
187
|
|
|
return $this->isCore; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function onEventReceived(string $event, array $arguments = []) |
191
|
|
|
{ |
192
|
|
|
$signature = 'on'.ucfirst($event); |
193
|
|
|
if (method_exists($this, $signature)) { |
194
|
|
|
$this->{$signature}(...$arguments); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function getEventGroup() |
199
|
|
|
{ |
200
|
|
|
return $this->getUniqueName(); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|