1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Maestriam\Samurai\Entities; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Maestriam\Samurai\Contracts\Entities\ComposerContract; |
7
|
|
|
use Maestriam\Samurai\Exceptions\InvalidComposerFileException; |
8
|
|
|
|
9
|
|
|
class Composer extends Source implements ComposerContract |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Caminho absoluto do arquivo |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private string $path; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Descrição do tema |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private string $desc; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Informações |
29
|
|
|
* |
30
|
|
|
* @var object |
31
|
|
|
*/ |
32
|
|
|
private object $info; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Nome do template |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected string $template = 'composer'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Instância com as regras de negócio sobre composer.json |
43
|
|
|
* |
44
|
|
|
* @param Vendor $vendor |
45
|
|
|
* @param string $desc |
46
|
|
|
*/ |
47
|
|
|
public function __construct(Theme $theme, string $desc = null) |
48
|
|
|
{ |
49
|
|
|
$this->setTheme($theme); |
50
|
|
|
|
51
|
|
|
if ($desc) { |
52
|
|
|
$this->description($desc); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritDoc} |
58
|
|
|
*/ |
59
|
|
|
public function description(string $desc = null) : string|Composer |
60
|
|
|
{ |
61
|
|
|
if (! $desc) { |
62
|
|
|
return $this->getDescription(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this->setDescription($desc); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Verifica se o arquivo já existe dentro do tema |
70
|
|
|
* |
71
|
|
|
* @return boolean |
72
|
|
|
*/ |
73
|
|
|
public function exists() : bool |
74
|
|
|
{ |
75
|
|
|
return $this->fileExists(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Executa a criação de um composer.json dentro do tema |
80
|
|
|
* |
81
|
|
|
* @return Composer |
82
|
|
|
*/ |
83
|
|
|
public function create() : Composer |
84
|
|
|
{ |
85
|
|
|
$info = $this->createFile(); |
86
|
|
|
|
87
|
|
|
$this->setPath($info->absolute_path); |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Carrega as informações do arquivo composer.json, dentro do tema. |
94
|
|
|
* |
95
|
|
|
* @return Composer |
96
|
|
|
*/ |
97
|
|
|
public function load() : Composer |
98
|
|
|
{ |
99
|
|
|
$pack = $this->theme()->package(); |
100
|
|
|
|
101
|
|
|
$content = $this->loadContent(); |
102
|
|
|
|
103
|
|
|
$json = json_decode($content); |
104
|
|
|
|
105
|
|
|
if (! $this->isValid($json)) { |
106
|
|
|
throw new InvalidComposerFileException($pack); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $this->extract($json); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Retorna as informações do arquivo composer.json |
114
|
|
|
* |
115
|
|
|
* @return object |
116
|
|
|
*/ |
117
|
|
|
public function info() : object |
118
|
|
|
{ |
119
|
|
|
return $this->info; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritDoc} |
124
|
|
|
*/ |
125
|
|
|
public function preview() : string |
126
|
|
|
{ |
127
|
|
|
return $this->previewContent(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Retorna o caminho do arquivo composer.json dentro do tema |
132
|
|
|
* |
133
|
|
|
* @return string|null |
134
|
|
|
*/ |
135
|
|
|
public function path() : ?string |
136
|
|
|
{ |
137
|
|
|
return $this->path; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* {@inheritDoc} |
142
|
|
|
*/ |
143
|
|
|
protected function placeholders() : array |
144
|
|
|
{ |
145
|
|
|
return [ |
146
|
|
|
'description' => $this->description(), |
147
|
|
|
'authorName' => $this->theme()->author()->name(), |
148
|
|
|
'authorEmail' => $this->theme()->author()->email(), |
149
|
|
|
'package' => $this->theme()->vendor()->package(), |
150
|
|
|
]; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* {@inheritDoc} |
155
|
|
|
*/ |
156
|
|
|
protected function filename() : string |
157
|
|
|
{ |
158
|
|
|
return 'composer.json'; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Define a descrição do tema |
163
|
|
|
* |
164
|
|
|
* @param string $desc |
165
|
|
|
* @return Composer |
166
|
|
|
*/ |
167
|
|
|
private function setDescription(string $desc) : Composer |
168
|
|
|
{ |
169
|
|
|
$this->desc = $desc; |
170
|
|
|
|
171
|
|
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Define as informações extraídas do |
176
|
|
|
* |
177
|
|
|
* @param object $info |
178
|
|
|
* @return Composer |
179
|
|
|
*/ |
180
|
|
|
private function setInfo(object $info) : Composer |
181
|
|
|
{ |
182
|
|
|
$this->info = $info; |
183
|
|
|
|
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Retorna a descrição do tema |
189
|
|
|
* Se não houver uma descrição definida, retorna a descrição padrão |
190
|
|
|
* inserida no arquivo config.php |
191
|
|
|
* |
192
|
|
|
* @return string |
193
|
|
|
*/ |
194
|
|
|
private function getDescription() : string |
195
|
|
|
{ |
196
|
|
|
return $this->desc ?? $this->config()->description(); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Verifica se o arquivo composer do tema é válido |
201
|
|
|
* |
202
|
|
|
* @param object $json |
203
|
|
|
* @return boolean |
204
|
|
|
*/ |
205
|
|
|
private function isValid(object $json) : bool |
206
|
|
|
{ |
207
|
|
|
$keys = ['name', 'description', 'type', 'require', 'authors']; |
208
|
|
|
|
209
|
|
|
foreach ($keys as $k) { |
210
|
|
|
if (! property_exists($json, $k) || $json->$k == null) { |
211
|
|
|
return false; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
if (! is_array($json->authors) || empty($json->authors)) { |
216
|
|
|
return false; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return true; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Pega as informações do arquivo Json |
224
|
|
|
* |
225
|
|
|
* @param object $json |
226
|
|
|
* @return Composer |
227
|
|
|
*/ |
228
|
|
|
private function extract(object $json) : Composer |
229
|
|
|
{ |
230
|
|
|
$this->setInfo($json)->description($json->description); |
231
|
|
|
|
232
|
|
|
return $this; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Define o caminho completo do arquivo composer.json dentro do tema |
237
|
|
|
* |
238
|
|
|
* @param string $path |
239
|
|
|
* @return Composer |
240
|
|
|
*/ |
241
|
|
|
private function setPath(string $path) : Composer |
242
|
|
|
{ |
243
|
|
|
if (! is_file($path)) { |
244
|
|
|
throw new \Exception(''); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
$this->path = $path; |
248
|
|
|
return $this; |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|