|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: Mark |
|
5
|
|
|
* Date: 05/03/2016 |
|
6
|
|
|
* Time: 14:42. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace App\Model; |
|
10
|
|
|
|
|
11
|
|
|
use App\Plugins\PluginHandler; |
|
12
|
|
|
use Illuminate\Database\Eloquent\Model as EloquentModel; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class Plugins. |
|
16
|
|
|
* |
|
17
|
|
|
* @property string $name |
|
18
|
|
|
* |
|
19
|
|
|
* @property boolean $required |
|
20
|
|
|
* @property boolean $enabled |
|
21
|
|
|
* |
|
22
|
|
|
* @property PluginHandler $handler |
|
23
|
|
|
* |
|
24
|
|
|
* @property Plugin $options |
|
25
|
|
|
*/ |
|
26
|
|
|
class Plugin extends EloquentModel |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* The table associated with the model. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $table = 'plugins'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The attributes that are mass assignable. |
|
37
|
|
|
* |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $fillable = []; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* The table date columns, casted to Carbon. |
|
44
|
|
|
* |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $dates = ['created_at', 'updated_at']; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* The attributes that should be cast to native types. |
|
51
|
|
|
* |
|
52
|
|
|
* @var array |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $casts = ['enabled' => 'boolean', 'required' => 'boolean']; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Return the plugins namespace |
|
58
|
|
|
* |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function dirNamespace() |
|
62
|
|
|
{ |
|
63
|
|
|
return sprintf("App\Plugins\%s", ucfirst($this->name)); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* |
|
68
|
|
|
* @return PluginHandler |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function getHandlerAttribute() |
|
71
|
|
|
{ |
|
72
|
|
|
return app(sprintf("%s\%sController", $this->dirNamespace(), ucfirst($this->name))); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* ==========================================================. |
|
77
|
|
|
* |
|
78
|
|
|
* GET THE ATTRIBUTES OF THE MODEL |
|
79
|
|
|
* |
|
80
|
|
|
* ========================================================== |
|
81
|
|
|
*/ |
|
82
|
|
|
public function icon() |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->handler->icon(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function name() |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->name; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function version() |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->handler->version(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function isEnabled() |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->getAttribute('enabled') == true; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function isDisabled() |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->getAttribute('enabled') == false; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function isFrontEnd() |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->getAttribute('is_frontend'); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function isBackEnd() |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->getAttribute('is_backend'); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function getCreatedAt() |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->getAttribute('created_at'); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function getUpdatedAt() |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->getAttribute('updated_at'); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function setEnabled($boolean) |
|
128
|
|
|
{ |
|
129
|
|
|
if ($boolean == true) { |
|
130
|
|
|
return $this->enable(); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return $this->disable(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Enable the product. |
|
138
|
|
|
* |
|
139
|
|
|
* @return $this |
|
140
|
|
|
*/ |
|
141
|
|
|
public function enable() |
|
142
|
|
|
{ |
|
143
|
|
|
$this->setAttribute('enabled', true); |
|
144
|
|
|
|
|
145
|
|
|
return $this; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Disable the product. |
|
150
|
|
|
* |
|
151
|
|
|
* @return $this |
|
152
|
|
|
*/ |
|
153
|
|
|
public function disable() |
|
154
|
|
|
{ |
|
155
|
|
|
$this->setAttribute('enabled', false); |
|
156
|
|
|
|
|
157
|
|
|
return $this; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function setInstalled(bool $boolean) |
|
161
|
|
|
{ |
|
162
|
|
|
$this->setAttribute('installed', $boolean ? 1 : 0); |
|
163
|
|
|
|
|
164
|
|
|
return $this; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @return mixed |
|
|
|
|
|
|
169
|
|
|
*/ |
|
170
|
|
|
public function isInstalled() |
|
171
|
|
|
{ |
|
172
|
|
|
return $this->getAttribute('installed') ? true : false; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
public function setName($string) |
|
176
|
|
|
{ |
|
177
|
|
|
$this->setAttribute('name', $string); |
|
178
|
|
|
|
|
179
|
|
|
return $this; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
public function setVersion($integer) |
|
183
|
|
|
{ |
|
184
|
|
|
$this->setAttribute('version', $integer); |
|
185
|
|
|
|
|
186
|
|
|
return $this; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
public function setIcon($string) |
|
190
|
|
|
{ |
|
191
|
|
|
$this->setAttribute('icon', $string); |
|
192
|
|
|
|
|
193
|
|
|
return $this; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
public function adminUrl() |
|
197
|
|
|
{ |
|
198
|
|
|
if ($this->isBackEnd()) { |
|
199
|
|
|
return url('/admin/'.$this->name()); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
throw new \Exception('This is not a backend enabled plugin and should not be used here.'); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
public function userUrl() |
|
206
|
|
|
{ |
|
207
|
|
|
if ($this->isFrontEnd()) { |
|
208
|
|
|
return url($this->name()); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
throw new \Exception('This is not a frontend enabled plugin and should not be used here.'); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
public function isHidden() |
|
215
|
|
|
{ |
|
216
|
|
|
return $this->getAttribute('hidden') ? true : false; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
public function setHide(bool $boolean) |
|
220
|
|
|
{ |
|
221
|
|
|
$this->setAttribute('hidden', $boolean ? 1 : 0); |
|
222
|
|
|
|
|
223
|
|
|
return $this; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
public function setRequired($boolean) |
|
227
|
|
|
{ |
|
228
|
|
|
$this->setAttribute('required', $boolean); |
|
229
|
|
|
|
|
230
|
|
|
return $this; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
public function setFrontEnd($boolean) |
|
234
|
|
|
{ |
|
235
|
|
|
$this->setAttribute('is_frontend', $boolean); |
|
236
|
|
|
|
|
237
|
|
|
return $this; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
public function setBackEnd($boolean) |
|
241
|
|
|
{ |
|
242
|
|
|
$this->setAttribute('is_backend', $boolean); |
|
243
|
|
|
|
|
244
|
|
|
return $this; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
249
|
|
|
*/ |
|
250
|
|
|
public function options() |
|
251
|
|
|
{ |
|
252
|
|
|
return $this->hasMany(PluginOption::class, 'plugin_id', 'id'); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
public function option($key) |
|
|
|
|
|
|
256
|
|
|
{ |
|
257
|
|
|
return $this->options->where('key', $key)->first()->value(); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
public function feeds() |
|
261
|
|
|
{ |
|
262
|
|
|
return $this->hasMany(PluginFeed::class, 'plugin_id', 'id'); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
public function install() |
|
266
|
|
|
{ |
|
267
|
|
|
$controller = app(printf("App\Plugins\%s", $this->name)); |
|
268
|
|
|
|
|
269
|
|
|
$controller->install(); |
|
270
|
|
|
} |
|
271
|
|
|
} |
|
272
|
|
|
|
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.