1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Consigliere\Components; |
4
|
|
|
|
5
|
|
|
use Illuminate\Foundation\AliasLoader; |
6
|
|
|
use Illuminate\Foundation\Application; |
7
|
|
|
use Illuminate\Support\ServiceProvider as ComponentServiceProvider; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
|
10
|
|
|
class Component extends ComponentServiceProvider |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The laravel application instance. |
14
|
|
|
* |
15
|
|
|
* @var Application |
16
|
|
|
*/ |
17
|
|
|
protected $app; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The component name. |
21
|
|
|
* |
22
|
|
|
* @var |
23
|
|
|
*/ |
24
|
|
|
protected $name; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The component path,. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $path; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The constructor. |
35
|
|
|
* |
36
|
|
|
* @param Application $app |
37
|
|
|
* @param $name |
38
|
|
|
* @param $path |
39
|
|
|
*/ |
40
|
|
|
public function __construct(Application $app, $name, $path) |
41
|
|
|
{ |
42
|
|
|
$this->app = $app; |
43
|
|
|
$this->name = $name; |
44
|
|
|
$this->path = realpath($path); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get laravel instance. |
49
|
|
|
* |
50
|
|
|
* @return \Illuminate\Foundation\Application |
51
|
|
|
*/ |
52
|
|
|
public function getLaravel() |
53
|
|
|
{ |
54
|
|
|
return $this->app; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get name. |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function getName() |
63
|
|
|
{ |
64
|
|
|
return $this->name; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get name in lower case. |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
public function getLowerName() |
73
|
|
|
{ |
74
|
|
|
return strtolower($this->name); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get name in studly case. |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function getStudlyName() |
83
|
|
|
{ |
84
|
|
|
return Str::studly($this->name); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get description. |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function getDescription() |
93
|
|
|
{ |
94
|
|
|
return $this->get('description'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get alias. |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public function getAlias() |
103
|
|
|
{ |
104
|
|
|
return $this->get('alias'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get priority. |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function getPriority() |
113
|
|
|
{ |
114
|
|
|
return $this->get('priority'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get path. |
119
|
|
|
* |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
public function getPath() |
123
|
|
|
{ |
124
|
|
|
return $this->path; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Set path. |
129
|
|
|
* |
130
|
|
|
* @param string $path |
131
|
|
|
* |
132
|
|
|
* @return $this |
133
|
|
|
*/ |
134
|
|
|
public function setPath($path) |
135
|
|
|
{ |
136
|
|
|
$this->path = $path; |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Bootstrap the application events. |
143
|
|
|
*/ |
144
|
|
|
public function boot() |
145
|
|
|
{ |
146
|
|
|
if (config('components.register.translations', true) === true) { |
147
|
|
|
$this->registerTranslation(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$this->fireEvent('boot'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Register component's translation. |
155
|
|
|
* |
156
|
|
|
* @return void |
157
|
|
|
*/ |
158
|
|
|
protected function registerTranslation() |
159
|
|
|
{ |
160
|
|
|
$lowerName = $this->getLowerName(); |
161
|
|
|
|
162
|
|
|
$langPath = $this->getPath() . "/Resources/lang"; |
163
|
|
|
|
164
|
|
|
if (is_dir($langPath)) { |
165
|
|
|
$this->loadTranslationsFrom($langPath, $lowerName); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Get json contents. |
171
|
|
|
* |
172
|
|
|
* @return Json |
173
|
|
|
*/ |
174
|
|
|
public function json($file = null) |
175
|
|
|
{ |
176
|
|
|
if (is_null($file)) { |
177
|
|
|
$file = 'component.json'; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return new Json($this->getPath() . '/' . $file, $this->app['files']); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get a specific data from json file by given the key. |
185
|
|
|
* |
186
|
|
|
* @param $key |
187
|
|
|
* @param null $default |
188
|
|
|
* |
189
|
|
|
* @return mixed |
190
|
|
|
*/ |
191
|
|
|
public function get($key, $default = null) |
192
|
|
|
{ |
193
|
|
|
return $this->json()->get($key, $default); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Get a specific data from composer.json file by given the key. |
198
|
|
|
* |
199
|
|
|
* @param $key |
200
|
|
|
* @param null $default |
201
|
|
|
* |
202
|
|
|
* @return mixed |
203
|
|
|
*/ |
204
|
|
|
public function getComposerAttr($key, $default = null) |
205
|
|
|
{ |
206
|
|
|
return $this->json('composer.json')->get($key, $default); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Register the component. |
211
|
|
|
*/ |
212
|
|
|
public function register() |
213
|
|
|
{ |
214
|
|
|
$this->registerAliases(); |
215
|
|
|
|
216
|
|
|
$this->registerProviders(); |
217
|
|
|
|
218
|
|
|
$this->registerFiles(); |
219
|
|
|
|
220
|
|
|
$this->fireEvent('register'); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Register the component event. |
225
|
|
|
* |
226
|
|
|
* @param string $event |
227
|
|
|
*/ |
228
|
|
|
protected function fireEvent($event) |
229
|
|
|
{ |
230
|
|
|
$this->app['events']->fire(sprintf('components.%s.' . $event, $this->getLowerName()), [$this]); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Register the aliases from this component. |
235
|
|
|
*/ |
236
|
|
|
protected function registerAliases() |
237
|
|
|
{ |
238
|
|
|
$loader = AliasLoader::getInstance(); |
239
|
|
|
foreach ($this->get('aliases', []) as $aliasName => $aliasClass) { |
240
|
|
|
$loader->alias($aliasName, $aliasClass); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Register the service providers from this component. |
246
|
|
|
*/ |
247
|
|
|
protected function registerProviders() |
248
|
|
|
{ |
249
|
|
|
foreach ($this->get('providers', []) as $provider) { |
250
|
|
|
$this->app->register($provider); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Register the files from this component. |
256
|
|
|
*/ |
257
|
|
|
protected function registerFiles() |
258
|
|
|
{ |
259
|
|
|
foreach ($this->get('files', []) as $file) { |
260
|
|
|
include $this->path . '/' . $file; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Handle call __toString. |
266
|
|
|
* |
267
|
|
|
* @return string |
268
|
|
|
*/ |
269
|
|
|
public function __toString() |
270
|
|
|
{ |
271
|
|
|
return $this->getStudlyName(); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Determine whether the given status same with the current component status. |
276
|
|
|
* |
277
|
|
|
* @param $status |
278
|
|
|
* |
279
|
|
|
* @return bool |
280
|
|
|
*/ |
281
|
|
|
public function isStatus($status) |
282
|
|
|
{ |
283
|
|
|
return $this->get('active', 0) === $status; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Determine whether the current component activated. |
288
|
|
|
* |
289
|
|
|
* @return bool |
290
|
|
|
*/ |
291
|
|
|
public function enabled() |
292
|
|
|
{ |
293
|
|
|
return $this->active(); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Alternate for "enabled" method. |
298
|
|
|
* |
299
|
|
|
* @return bool |
300
|
|
|
*/ |
301
|
|
|
public function active() |
302
|
|
|
{ |
303
|
|
|
return $this->isStatus(1); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Determine whether the current component not activated. |
308
|
|
|
* |
309
|
|
|
* @return bool |
310
|
|
|
*/ |
311
|
|
|
public function notActive() |
312
|
|
|
{ |
313
|
|
|
return !$this->active(); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Alias for "notActive" method. |
318
|
|
|
* |
319
|
|
|
* @return bool |
320
|
|
|
*/ |
321
|
|
|
public function disabled() |
322
|
|
|
{ |
323
|
|
|
return !$this->enabled(); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Set active state for current component. |
328
|
|
|
* |
329
|
|
|
* @param $active |
330
|
|
|
* |
331
|
|
|
* @return bool |
332
|
|
|
*/ |
333
|
|
|
public function setActive($active) |
334
|
|
|
{ |
335
|
|
|
return $this->json()->set('active', $active)->save(); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Disable the current component. |
340
|
|
|
* |
341
|
|
|
* @return bool |
342
|
|
|
*/ |
343
|
|
|
public function disable() |
344
|
|
|
{ |
345
|
|
|
$this->app['events']->fire('component.disabling', [$this]); |
346
|
|
|
|
347
|
|
|
$this->setActive(0); |
348
|
|
|
|
349
|
|
|
$this->app['events']->fire('component.disabled', [$this]); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Enable the current component. |
354
|
|
|
*/ |
355
|
|
|
public function enable() |
356
|
|
|
{ |
357
|
|
|
$this->app['events']->fire('component.enabling', [$this]); |
358
|
|
|
|
359
|
|
|
$this->setActive(1); |
360
|
|
|
|
361
|
|
|
$this->app['events']->fire('component.enabled', [$this]); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Delete the current component. |
366
|
|
|
* |
367
|
|
|
* @return bool |
368
|
|
|
*/ |
369
|
|
|
public function delete() |
370
|
|
|
{ |
371
|
|
|
return $this->json()->getFilesystem()->deleteDirectory($this->getPath()); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Get extra path. |
376
|
|
|
* |
377
|
|
|
* @param $path |
378
|
|
|
* |
379
|
|
|
* @return string |
380
|
|
|
*/ |
381
|
|
|
public function getExtraPath($path) |
382
|
|
|
{ |
383
|
|
|
return $this->getPath() . '/' . $path; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Handle call to __get method. |
388
|
|
|
* |
389
|
|
|
* @param $key |
390
|
|
|
* |
391
|
|
|
* @return mixed |
392
|
|
|
*/ |
393
|
|
|
public function __get($key) |
394
|
|
|
{ |
395
|
|
|
return $this->get($key); |
396
|
|
|
} |
397
|
|
|
} |
398
|
|
|
|