|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nwidart\Modules\Laravel; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Illuminate\Filesystem\Filesystem; |
|
7
|
|
|
use Nwidart\Modules\Entities\ModuleEntity; |
|
8
|
|
|
use Nwidart\Modules\Json; |
|
9
|
|
|
use Nwidart\Modules\Process\Updater; |
|
10
|
|
|
|
|
11
|
|
|
class DatabaseModule extends Module |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var array<string, mixed> |
|
15
|
|
|
*/ |
|
16
|
|
|
public $attributes; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @return ModuleEntity |
|
20
|
|
|
*/ |
|
21
|
|
|
public function getModel() |
|
22
|
|
|
{ |
|
23
|
|
|
return new ModuleEntity(); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Get attributes. |
|
28
|
|
|
* |
|
29
|
|
|
* @return array |
|
30
|
|
|
*/ |
|
31
|
|
|
public function getAttributes() |
|
32
|
|
|
{ |
|
33
|
|
|
return $this->attributes; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Set attributes. |
|
38
|
|
|
* |
|
39
|
|
|
* @param array $attributes |
|
40
|
|
|
*/ |
|
41
|
|
|
public function setAttributes($attributes) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->attributes = $attributes; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get a specific data from json file by given the key. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $key |
|
50
|
|
|
* @param null $default |
|
51
|
|
|
* |
|
52
|
|
|
* @return mixed |
|
53
|
|
|
*/ |
|
54
|
|
|
public function get(string $key, $default = null) |
|
55
|
|
|
{ |
|
56
|
|
|
return isset($this->attributes[$key]) ? $this->attributes[$key] : $default; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Determine whether the given status same with the current module status. |
|
61
|
|
|
* |
|
62
|
|
|
* @param bool $status |
|
63
|
|
|
* |
|
64
|
|
|
* @return bool |
|
65
|
|
|
*/ |
|
66
|
|
|
public function isStatus(bool $status): bool |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->isEnabled(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Determine whether the current module activated. |
|
73
|
|
|
* |
|
74
|
|
|
* @return bool |
|
75
|
|
|
*/ |
|
76
|
|
|
public function isEnabled(): bool |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->attributes['is_active']; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Determine whether the current module not disabled. |
|
83
|
|
|
* |
|
84
|
|
|
* @return bool |
|
85
|
|
|
*/ |
|
86
|
|
|
public function isDisabled(): bool |
|
87
|
|
|
{ |
|
88
|
|
|
return !$this->isEnabled(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Set active state for current module. |
|
93
|
|
|
* |
|
94
|
|
|
* @param bool $active |
|
95
|
|
|
* |
|
96
|
|
|
* @return void |
|
97
|
|
|
*/ |
|
98
|
|
|
public function setActive(bool $active): void |
|
99
|
|
|
{ |
|
100
|
|
|
$this->getModel()->where(['name' => $this->getName()])->update(['is_active' => $active]); |
|
101
|
|
|
$this->flushCache(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Disable the current module. |
|
106
|
|
|
*/ |
|
107
|
|
|
public function disable(): void |
|
108
|
|
|
{ |
|
109
|
|
|
$this->fireEvent('disabling'); |
|
110
|
|
|
|
|
111
|
|
|
$this->setActive(false); |
|
112
|
|
|
|
|
113
|
|
|
$this->fireEvent('disabled'); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Enable the current module. |
|
118
|
|
|
*/ |
|
119
|
|
|
public function enable(): void |
|
120
|
|
|
{ |
|
121
|
|
|
$this->fireEvent('enabling'); |
|
122
|
|
|
|
|
123
|
|
|
$this->setActive(true); |
|
124
|
|
|
|
|
125
|
|
|
$this->fireEvent('enabled'); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Delete the current module. |
|
130
|
|
|
* |
|
131
|
|
|
* @return bool |
|
132
|
|
|
* @throws Exception |
|
133
|
|
|
*/ |
|
134
|
|
|
public function delete(): bool |
|
135
|
|
|
{ |
|
136
|
|
|
$module = $this->getModel()->where(['name' => $this->getName()])->first(); |
|
137
|
|
|
if ($module) { |
|
138
|
|
|
$module->delete(); |
|
139
|
|
|
} |
|
140
|
|
|
$this->flushCache(); |
|
141
|
|
|
|
|
142
|
|
|
return (new Filesystem())->deleteDirectory($this->getPath()); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Get version. |
|
147
|
|
|
* |
|
148
|
|
|
* @return mixed|null |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getVersion() |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->get('version', '1.0.0'); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function update(Updater $updater) |
|
156
|
|
|
{ |
|
157
|
|
|
|
|
158
|
|
|
if (config('modules.database_management.update_file_to_database_when_updating')) { |
|
159
|
|
|
$json = Json::make($this->getPath() . '/' . 'module.json'); |
|
160
|
|
|
$data = $json->getAttributes(); |
|
161
|
|
|
|
|
162
|
|
|
if (!isset($data['version'])) { |
|
163
|
|
|
$data['version'] = '1.0.0'; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
// Check version, if version is higher then update module.json into database. |
|
167
|
|
|
if (version_compare($this->getVersion(), $data['version'], '<=')) { |
|
168
|
|
|
$data = $updater->getModule()->validateAttributes($data); |
|
|
|
|
|
|
169
|
|
|
$this->getModel()->where(['name' => $data['name']])->update($data); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
$response = with($updater)->update($this->getName()); |
|
174
|
|
|
$this->flushCache(); |
|
175
|
|
|
|
|
176
|
|
|
return $response; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: