1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ComposerJson; |
4
|
|
|
|
5
|
|
|
use ComposerJson\Schemas\Author; |
6
|
|
|
use ComposerJson\Schemas\Classmap; |
7
|
|
|
use ComposerJson\Schemas\Composer; |
8
|
|
|
use ComposerJson\Schemas\Files; |
9
|
|
|
use ComposerJson\Schemas\Psr0; |
10
|
|
|
use ComposerJson\Schemas\Psr4; |
11
|
|
|
use ComposerJson\Schemas\Repository; |
12
|
|
|
use ComposerJson\Schemas\Support; |
13
|
|
|
use ErrorException; |
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
|
16
|
|
|
class Generator |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Main root object of composer |
20
|
|
|
* |
21
|
|
|
* @var Composer |
22
|
|
|
*/ |
23
|
|
|
private Composer $composer; |
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Check if file is JSON |
27
|
|
|
* |
28
|
|
|
* @param string $source |
29
|
|
|
* |
30
|
|
|
* @return false|array |
31
|
|
|
*/ |
32
|
|
|
private function isJson(string &$source) |
33
|
|
|
{ |
34
|
|
|
$json = json_decode($source, true); |
35
|
|
|
return $json !== false ? $json : false; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Generator constructor. |
40
|
|
|
* |
41
|
|
|
* @param \ComposerJson\Schemas\Composer|null $composer |
42
|
|
|
*/ |
43
|
|
|
public function __construct(Composer $composer = null) |
44
|
|
|
{ |
45
|
|
|
if (null !== $composer) { |
46
|
|
|
$this->composer = $composer; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Load composer to memory |
52
|
|
|
* |
53
|
|
|
* @param \ComposerJson\Schemas\Composer $composer |
54
|
|
|
* |
55
|
|
|
* @return $this |
56
|
|
|
*/ |
57
|
|
|
public function load(Composer $composer): self |
58
|
|
|
{ |
59
|
|
|
$this->composer = $composer; |
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Return array of values |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
public function toArray() |
69
|
|
|
{ |
70
|
|
|
return $this->composer->toArray(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Return JSON with composer |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
public function toJson() |
79
|
|
|
{ |
80
|
|
|
return json_encode($this->toArray(), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Read JSON conde and return Composer object |
85
|
|
|
* |
86
|
|
|
* @param string $file |
87
|
|
|
* |
88
|
|
|
* @return Composer |
89
|
|
|
* @throws \ErrorException |
90
|
|
|
* @throws \InvalidArgumentException |
91
|
|
|
*/ |
92
|
|
|
public function read(string $file) |
93
|
|
|
{ |
94
|
|
|
// Read file from string |
95
|
|
|
if (!$source = file_get_contents($file)) { |
96
|
|
|
throw new ErrorException('Provided file could not to be read'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// Convert JSON to array |
100
|
|
|
if (!$array = $this->isJson($source)) { |
101
|
|
|
throw new ErrorException('Provided string is not a valid JSON'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// Initiate composer object |
105
|
|
|
$this->composer = new Composer(); |
106
|
|
|
|
107
|
|
|
// Parse values |
108
|
|
|
foreach ($array as $key => $value) { |
109
|
|
|
|
110
|
|
|
switch ($key) { |
111
|
|
|
|
112
|
|
|
/* |
113
|
|
|
* Specific objects |
114
|
|
|
*/ |
115
|
|
|
|
116
|
|
|
case 'authors': |
117
|
|
|
$this->composer->authors = $this->parseAuthors($value); |
118
|
|
|
break; |
119
|
|
|
case 'support': |
120
|
|
|
$this->composer->support = $this->parseSupport($value); |
121
|
|
|
break; |
122
|
|
|
case 'repositories': |
123
|
|
|
$this->composer->repositories = $this->parseRepositories($value); |
124
|
|
|
break; |
125
|
|
|
case 'autoload': |
126
|
|
|
$this->composer->autoload = $this->parseAutoload($value); |
127
|
|
|
break; |
128
|
|
|
case 'autoload-dev': |
129
|
|
|
$this->composer->autoloadDev = $this->parseAutoload($value); |
130
|
|
|
break; |
131
|
|
|
|
132
|
|
|
/* |
133
|
|
|
* Convert format of keys |
134
|
|
|
*/ |
135
|
|
|
|
136
|
|
|
case 'require-dev': |
137
|
|
|
$this->composer->requireDev = $value; |
138
|
|
|
break; |
139
|
|
|
case 'include-path': |
140
|
|
|
$this->composer->includePath = $value; |
141
|
|
|
break; |
142
|
|
|
case 'target-dir': |
143
|
|
|
$this->composer->targetDir = $value; |
144
|
|
|
break; |
145
|
|
|
case 'minimum-stability': |
146
|
|
|
$this->composer->minimumStability = $value; |
147
|
|
|
break; |
148
|
|
|
case 'prefer-stable': |
149
|
|
|
$this->composer->preferStable = $value; |
150
|
|
|
break; |
151
|
|
|
case 'non-feature-branches'; |
152
|
|
|
$this->composer->nonFeatureBranches = $value; |
153
|
|
|
break; |
154
|
|
|
|
155
|
|
|
/* |
156
|
|
|
* Default values |
157
|
|
|
*/ |
158
|
|
|
|
159
|
|
|
default: |
160
|
|
|
$this->composer->$key = $value; |
161
|
|
|
break; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $this->composer; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
private function parseAutoload(array $autoload): array |
170
|
|
|
{ |
171
|
|
|
$objects = []; |
172
|
|
|
foreach ($autoload as $type => $options) { |
173
|
|
|
|
174
|
|
|
switch ($type) { |
175
|
|
|
case 'psr-0': |
176
|
|
|
$object = new Psr0(); |
177
|
|
|
break; |
178
|
|
|
case 'psr-4': |
179
|
|
|
$object = new Psr4(); |
180
|
|
|
break; |
181
|
|
|
case 'classmap': |
182
|
|
|
$object = new Classmap(); |
183
|
|
|
break; |
184
|
|
|
case 'files': |
185
|
|
|
$object = new Files(); |
186
|
|
|
break; |
187
|
|
|
default: |
188
|
|
|
throw new InvalidArgumentException('Incorrect type of autoloader provided'); |
189
|
|
|
break; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
// Autoload object options |
193
|
|
|
$object->options = $options; |
194
|
|
|
|
195
|
|
|
// Save object |
196
|
|
|
$objects[$type] = $object; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $objects; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Convert array of repositories to array of repository objects |
204
|
|
|
* |
205
|
|
|
* @param array $repositories |
206
|
|
|
* |
207
|
|
|
* @return \ComposerJson\Schemas\Repository[] |
208
|
|
|
*/ |
209
|
|
|
private function parseRepositories(array $repositories): array |
210
|
|
|
{ |
211
|
|
|
$objects = []; |
212
|
|
|
foreach ($repositories as $repository) { |
213
|
|
|
$object = new Repository(); |
214
|
|
|
|
215
|
|
|
foreach ($repository as $key => $value) { |
216
|
|
|
$object->$key = $value; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
$objects[] = $object; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return $objects; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Convert support array to object of schema |
227
|
|
|
* |
228
|
|
|
* @param array $support |
229
|
|
|
* |
230
|
|
|
* @return \ComposerJson\Schemas\Support |
231
|
|
|
*/ |
232
|
|
|
private function parseSupport(array $support): Support |
233
|
|
|
{ |
234
|
|
|
$object = new Support(); |
235
|
|
|
|
236
|
|
|
foreach ($support as $key => $value) { |
237
|
|
|
$object->$key = $value; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return $object; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Convert authors array to array of objects |
245
|
|
|
* |
246
|
|
|
* @param array $authors |
247
|
|
|
* |
248
|
|
|
* @return \ComposerJson\Schemas\Author[] |
249
|
|
|
*/ |
250
|
|
|
private function parseAuthors(array $authors): array |
251
|
|
|
{ |
252
|
|
|
$objects = []; |
253
|
|
|
foreach ($authors as $author) { |
254
|
|
|
$object = new Author(); |
255
|
|
|
|
256
|
|
|
foreach ($author as $key => $value) { |
257
|
|
|
$object->$key = $value; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
$objects[] = $object; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
return $objects; |
264
|
|
|
} |
265
|
|
|
} |