1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Maestriam\Samurai\Entities; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\File; |
6
|
|
|
use Maestriam\Samurai\Entities\Foundation; |
7
|
|
|
use Maestriam\Samurai\Foundation\DirectiveFinder; |
8
|
|
|
use Maestriam\Samurai\Exceptions\ThemeExistsException; |
9
|
|
|
use Maestriam\Samurai\Exceptions\ThemeNotFoundException; |
10
|
|
|
use Maestriam\Samurai\Contracts\Entities\ThemeContract; |
11
|
|
|
use Maestriam\Samurai\Foundation\ThemePublisher; |
12
|
|
|
|
13
|
|
|
class Theme extends Foundation implements ThemeContract |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Nome do distribuidor do tema/nome do tema |
17
|
|
|
* Ex: vendor/theme |
18
|
|
|
* |
19
|
|
|
* @var Vendor |
20
|
|
|
*/ |
21
|
|
|
protected Vendor $vendorInstance; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Informações do autor do tema |
25
|
|
|
* |
26
|
|
|
* @var Author |
27
|
|
|
*/ |
28
|
|
|
protected Author $authorInstance; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Controle de caminhos de tema |
32
|
|
|
* |
33
|
|
|
* @var Structure |
34
|
|
|
*/ |
35
|
|
|
protected Structure $structureInstance; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Controle do arquivo composer.json do tema |
39
|
|
|
* |
40
|
|
|
* @var Composer |
41
|
|
|
*/ |
42
|
|
|
protected Composer $composerInstance; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Regras de negócio sobre busca de diretivas dentro do tema. |
46
|
|
|
* |
47
|
|
|
* @var DirectiveFinder |
48
|
|
|
*/ |
49
|
|
|
protected DirectiveFinder $finderInstance; |
50
|
|
|
|
51
|
|
|
protected ThemePublisher $publiserInstance; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Regras de negócio do tema |
55
|
|
|
* |
56
|
|
|
* @param string $package |
57
|
|
|
*/ |
58
|
|
|
public function __construct(string $package) |
59
|
|
|
{ |
60
|
|
|
$this->init($package); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritDoc} |
65
|
|
|
*/ |
66
|
|
|
public function vendor() : Vendor |
67
|
|
|
{ |
68
|
|
|
return $this->vendorInstance; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritDoc} |
73
|
|
|
*/ |
74
|
|
|
public function author(string $author = null) : Author|Theme |
75
|
|
|
{ |
76
|
|
|
if (! $author) { |
77
|
|
|
return $this->getAuthor(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this->setAuthor($author); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritDoc} |
85
|
|
|
*/ |
86
|
|
|
public function name() : string |
87
|
|
|
{ |
88
|
|
|
return $this->vendor()->name(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritDoc} |
93
|
|
|
*/ |
94
|
|
|
public function namespace() : string |
95
|
|
|
{ |
96
|
|
|
return $this->vendor()->namespace(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritDoc} |
101
|
|
|
*/ |
102
|
|
|
public function paths() : Structure |
103
|
|
|
{ |
104
|
|
|
return $this->structure(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritDoc} |
109
|
|
|
*/ |
110
|
|
|
public function package() : string |
111
|
|
|
{ |
112
|
|
|
return $this->vendor()->package(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritDoc} |
117
|
|
|
*/ |
118
|
|
|
public function exists() : bool |
119
|
|
|
{ |
120
|
|
|
return $this->composer()->exists(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritDoc} |
125
|
|
|
*/ |
126
|
|
|
public function preview() : string |
127
|
|
|
{ |
128
|
|
|
return $this->composer()->preview(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritDoc} |
133
|
|
|
*/ |
134
|
|
|
public function find() : ?Theme |
135
|
|
|
{ |
136
|
|
|
if (! $this->exists()) { |
137
|
|
|
return null; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $this->import(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* {@inheritDoc} |
145
|
|
|
*/ |
146
|
|
|
public function description(string $description = null) : Theme|string |
147
|
|
|
{ |
148
|
|
|
if (! $description) { |
149
|
|
|
return $this->composer()->description(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$this->composer()->description($description); |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* {@inheritDoc} |
159
|
|
|
*/ |
160
|
|
|
public function make() : Theme |
161
|
|
|
{ |
162
|
|
|
if ($this->exists()) { |
163
|
|
|
throw new ThemeExistsException($this->package()); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$this->structure()->init(); |
167
|
|
|
|
168
|
|
|
$this->composer()->create()->load(); |
169
|
|
|
|
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* {@inheritDoc} |
175
|
|
|
*/ |
176
|
|
|
public function findOrCreate() : Theme |
177
|
|
|
{ |
178
|
|
|
if (! $this->exists()) { |
179
|
|
|
return $this->make(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $this->import(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* {@inheritDoc} |
187
|
|
|
*/ |
188
|
|
|
public function use() : Theme |
189
|
|
|
{ |
190
|
|
|
$this->guard(); |
191
|
|
|
|
192
|
|
|
$this->register(); |
193
|
|
|
|
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* {@inheritDoc} |
199
|
|
|
*/ |
200
|
|
|
public function include(string $sentence) : Includer |
201
|
|
|
{ |
202
|
|
|
return $this->finder()->include($sentence); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* {@inheritDoc} |
207
|
|
|
*/ |
208
|
|
|
public function component(string $sentence) : Component |
209
|
|
|
{ |
210
|
|
|
return $this->finder()->component($sentence); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* {@inheritDoc} |
215
|
|
|
*/ |
216
|
|
|
public function directives() : array |
217
|
|
|
{ |
218
|
|
|
return $this->finder()->all(); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* {@inheritDoc} |
223
|
|
|
*/ |
224
|
|
|
public function load() : Theme |
225
|
|
|
{ |
226
|
|
|
foreach ($this->directives() as $directive) { |
227
|
|
|
$directive->load(); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return $this; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* {@inheritDoc} |
235
|
|
|
*/ |
236
|
|
|
public function publish() : bool |
237
|
|
|
{ |
238
|
|
|
return $this->publiserInstance->publish(); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* {@inheritDoc} |
243
|
|
|
*/ |
244
|
|
|
public function url(string $file = null) : string |
245
|
|
|
{ |
246
|
|
|
$domain = $this->env()->get('APP_URL'); |
247
|
|
|
$public = $this->config()->publishable(); |
248
|
|
|
$package = $this->package(); |
249
|
|
|
|
250
|
|
|
$file = '/' . $file ?? ''; |
251
|
|
|
$file = str_replace("'", "", $file); |
252
|
|
|
|
253
|
|
|
return sprintf('%s/%s/%s%s', $domain, $public, $package, $file); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Registra o nome do pacote do tema no arquivo de ambiente |
258
|
|
|
* do projeto Laravel |
259
|
|
|
* |
260
|
|
|
* @return void |
261
|
|
|
*/ |
262
|
|
|
private function register() |
263
|
|
|
{ |
264
|
|
|
$name = $this->package(); |
265
|
|
|
|
266
|
|
|
$key = $this->config()->env(); |
267
|
|
|
|
268
|
|
|
$this->env()->set($key, $name); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Inicia as regras de negócio do tema |
273
|
|
|
* Se o tema já existir dentro do projeto, carrega suas informações |
274
|
|
|
* |
275
|
|
|
* @param string $package |
276
|
|
|
* @return Theme |
277
|
|
|
*/ |
278
|
|
|
private function init(string $package) : Theme |
279
|
|
|
{ |
280
|
|
|
$this->setVendor($package) |
281
|
|
|
->setDirectiveFinder() |
282
|
|
|
->setComposer() |
283
|
|
|
->setStructure() |
284
|
|
|
->setPublisher(); |
285
|
|
|
|
286
|
|
|
if ($this->exists()) { |
287
|
|
|
return $this->import(); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
return $this; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
private function setPublisher() |
294
|
|
|
{ |
295
|
|
|
$this->publiserInstance = new ThemePublisher($this); |
296
|
|
|
return $this; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Carrega as informações vindas do arquivo composer.json |
301
|
|
|
* para dentro da instância |
302
|
|
|
* |
303
|
|
|
* @return Theme |
304
|
|
|
*/ |
305
|
|
|
private function import() : Theme |
306
|
|
|
{ |
307
|
|
|
$info = $this->composer()->load()->info(); |
308
|
|
|
|
309
|
|
|
$this->author()->name($info->authors[0]->name); |
310
|
|
|
$this->author()->email($info->authors[0]->email); |
311
|
|
|
|
312
|
|
|
return $this; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Define o nome do vendor do tema. |
317
|
|
|
* |
318
|
|
|
* @return Theme |
319
|
|
|
*/ |
320
|
|
|
private function setVendor(string $vendor) : Theme |
321
|
|
|
{ |
322
|
|
|
$this->vendorInstance = new Vendor($vendor); |
323
|
|
|
|
324
|
|
|
return $this; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Define as informações do autor do tema |
329
|
|
|
* |
330
|
|
|
* @param string $author |
331
|
|
|
* @return Theme |
332
|
|
|
*/ |
333
|
|
|
private function setAuthor(string $author) : Theme |
334
|
|
|
{ |
335
|
|
|
$this->authorInstance = new Author($author); |
336
|
|
|
|
337
|
|
|
return $this; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* Retorna as informações do autor do tema |
342
|
|
|
* |
343
|
|
|
* @return Author |
344
|
|
|
*/ |
345
|
|
|
private function getAuthor() : Author |
346
|
|
|
{ |
347
|
|
|
return $this->authorInstance ?? new Author(); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Define a instância de estrutura de diretórios do tema |
352
|
|
|
* |
353
|
|
|
* @return Theme |
354
|
|
|
*/ |
355
|
|
|
private function setStructure() : Theme |
356
|
|
|
{ |
357
|
|
|
$vendor = $this->vendor(); |
358
|
|
|
|
359
|
|
|
$this->structureInstance = new Structure($vendor); |
360
|
|
|
|
361
|
|
|
return $this; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Retorna a instância de estrutura do tema |
366
|
|
|
* |
367
|
|
|
* @return Structure |
368
|
|
|
*/ |
369
|
|
|
private function structure() : Structure |
370
|
|
|
{ |
371
|
|
|
return $this->structureInstance; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Define o composer do tema |
376
|
|
|
* |
377
|
|
|
* @return Theme |
378
|
|
|
*/ |
379
|
|
|
private function setComposer() : Theme |
380
|
|
|
{ |
381
|
|
|
$this->composerInstance = new Composer($this); |
382
|
|
|
|
383
|
|
|
return $this; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Retorna a instância do composer do tema |
388
|
|
|
* |
389
|
|
|
* @return Theme |
390
|
|
|
*/ |
391
|
|
|
private function composer() : Composer |
392
|
|
|
{ |
393
|
|
|
return $this->composerInstance; |
|
|
|
|
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Define a instância de busca de diretivas dentro do tema. |
398
|
|
|
* |
399
|
|
|
* @return Theme |
400
|
|
|
*/ |
401
|
|
|
private function setDirectiveFinder() : Theme |
402
|
|
|
{ |
403
|
|
|
$this->finderInstance = new DirectiveFinder($this); |
404
|
|
|
|
405
|
|
|
return $this; |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* Retorna a instância de busca de diretivas dentro do tema. |
410
|
|
|
* |
411
|
|
|
* @return DirectiveFinder |
412
|
|
|
*/ |
413
|
|
|
private function finder() : DirectiveFinder |
414
|
|
|
{ |
415
|
|
|
return $this->finderInstance; |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* Faz as devidas validações sobre o tema. |
420
|
|
|
* Se existir algo incompátivel, emite um Exception. |
421
|
|
|
* |
422
|
|
|
* @return void |
423
|
|
|
*/ |
424
|
|
|
private function guard() : void |
425
|
|
|
{ |
426
|
|
|
if ($this->exists()) { |
427
|
|
|
return; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
throw new ThemeNotFoundException($this->package()); |
431
|
|
|
} |
432
|
|
|
} |
433
|
|
|
|