|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Package.php |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright More in license.md |
|
6
|
|
|
* @license https://www.ipublikuj.eu |
|
7
|
|
|
* @author Adam Kadlec <[email protected]> |
|
8
|
|
|
* @package iPublikuj:Packages! |
|
9
|
|
|
* @subpackage Entities |
|
10
|
|
|
* @since 1.0.0 |
|
11
|
|
|
* |
|
12
|
|
|
* @date 27.09.14 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
declare(strict_types = 1); |
|
16
|
|
|
|
|
17
|
|
|
namespace IPub\Packages\Entities; |
|
18
|
|
|
|
|
19
|
|
|
use Nette; |
|
20
|
|
|
use Nette\Utils; |
|
21
|
|
|
|
|
22
|
|
|
use IPub\Packages\Exceptions; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Package abstract entity |
|
26
|
|
|
* |
|
27
|
|
|
* @package iPublikuj:Packages! |
|
28
|
|
|
* @subpackage Entities |
|
29
|
|
|
* |
|
30
|
|
|
* @author Adam Kadlec <[email protected]> |
|
31
|
|
|
*/ |
|
32
|
1 |
|
abstract class Package implements IPackage |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* Implement nette smart magic |
|
36
|
|
|
*/ |
|
37
|
1 |
|
use Nette\SmartObject; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var mixed |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $composerData; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param mixed[] $composerData |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct(array $composerData) |
|
48
|
|
|
{ |
|
49
|
1 |
|
$this->composerData = $composerData; |
|
50
|
|
|
|
|
51
|
1 |
|
if (!isset($composerData['name'])) { |
|
52
|
|
|
throw new Exceptions\UnexpectedValueException('Unknown package, has no name defined (' . json_encode($composerData) . ').'); |
|
53
|
|
|
} |
|
54
|
1 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getName() : string |
|
60
|
|
|
{ |
|
61
|
1 |
|
return $this->composerData['name']; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getTitle() : string |
|
68
|
|
|
{ |
|
69
|
|
|
$extra = $this->getExtra(); |
|
70
|
|
|
|
|
71
|
|
|
return $extra->offsetExists('title') ? $extra->offsetGet('title') : $this->getName(); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritdoc} |
|
76
|
|
|
*/ |
|
77
|
|
|
public function hasTitle() : bool |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->getTitle() ? TRUE : FALSE; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritdoc} |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getDescription() : ?string |
|
86
|
|
|
{ |
|
87
|
|
|
return isset($this->composerData['description']) ? $this->composerData['description'] : NULL; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getVersion() : string |
|
94
|
|
|
{ |
|
95
|
|
|
return isset($this->composerData['version']) ? $this->composerData['version'] : '0.0.0'; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* {@inheritdoc} |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getType() : string |
|
102
|
|
|
{ |
|
103
|
|
|
return isset($this->composerData['type']) ? $this->composerData['type'] : 'undefined'; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* {@inheritdoc} |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getKeywords() : Utils\ArrayHash |
|
110
|
|
|
{ |
|
111
|
|
|
$keywords = []; |
|
112
|
|
|
|
|
113
|
|
|
if (isset($this->composerData['keywords'])) { |
|
114
|
|
|
$keywords = $this->composerData['keywords']; |
|
115
|
|
|
|
|
116
|
|
|
$keywords = is_array($keywords) ? $keywords : array_map('trim', explode(',', $keywords)); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return Utils\ArrayHash::from($keywords); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* {@inheritdoc} |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getHomepage() : ?string |
|
126
|
|
|
{ |
|
127
|
|
|
return isset($this->composerData['homepage']) ? $this->composerData['homepage'] : NULL; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* {@inheritdoc} |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getReleaseDate() : ?\DateTimeInterface |
|
134
|
|
|
{ |
|
135
|
|
|
if (isset($this->composerData['time'])) { |
|
136
|
|
|
try { |
|
137
|
|
|
return new Utils\DateTime($this->composerData['time'], new \DateTimeZone('UTC')); |
|
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
} catch (\Exception $ex) { |
|
140
|
|
|
// If date could not be converted to object, than is in wrong format and is not added to package |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return NULL; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* {@inheritdoc} |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getLicense() : Utils\ArrayHash |
|
151
|
|
|
{ |
|
152
|
|
|
$licenses = []; |
|
153
|
|
|
|
|
154
|
|
|
if (isset($this->composerData['license'])) { |
|
155
|
|
|
$licenses = $this->composerData['license']; |
|
156
|
|
|
|
|
157
|
|
|
$licenses = is_array($licenses) ? $licenses : array_map('trim', explode(',', $licenses)); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
return Utils\ArrayHash::from($licenses); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* {@inheritdoc} |
|
165
|
|
|
*/ |
|
166
|
|
|
public function getAuthors() : Utils\ArrayHash |
|
167
|
|
|
{ |
|
168
|
|
|
$authors = []; |
|
169
|
|
|
|
|
170
|
|
|
if (isset($this->composerData['authors'])) { |
|
171
|
|
|
$authors = $this->composerData['authors']; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
return Utils\ArrayHash::from($authors); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* {@inheritdoc} |
|
180
|
|
|
*/ |
|
181
|
|
|
public function getSupport() : ?array |
|
182
|
|
|
{ |
|
183
|
|
|
return isset($this->composerData['support']) ? $this->composerData['support'] : NULL; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* {@inheritdoc} |
|
188
|
|
|
*/ |
|
189
|
|
|
public function getRequire() : array |
|
190
|
|
|
{ |
|
191
|
|
|
$ret = []; |
|
192
|
|
|
|
|
193
|
|
|
if (isset($this->composerData['require'])) { |
|
194
|
|
|
foreach ($this->composerData['require'] as $name => $require) { |
|
195
|
|
|
if (strpos($name, '/') !== FALSE) { |
|
196
|
|
|
$ret[] = $name; |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
return $ret; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* {@inheritdoc} |
|
206
|
|
|
*/ |
|
207
|
|
|
public function getRequireDev() : array |
|
208
|
|
|
{ |
|
209
|
|
|
$ret = []; |
|
210
|
|
|
|
|
211
|
|
|
if (isset($this->composerData['require-dev'])) { |
|
212
|
|
|
foreach ($this->composerData['require-dev'] as $name => $require) { |
|
213
|
|
|
if (strpos($name, '/') !== FALSE) { |
|
214
|
|
|
$ret[] = $name; |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
return $ret; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* {@inheritdoc} |
|
224
|
|
|
*/ |
|
225
|
|
|
public function getAutoload() : Utils\ArrayHash |
|
226
|
|
|
{ |
|
227
|
|
|
$autoload = []; |
|
228
|
|
|
|
|
229
|
|
|
if (isset($this->composerData['autoload'])) { |
|
230
|
|
|
$autoload = $this->composerData['autoload']; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
return Utils\ArrayHash::from($autoload); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* {@inheritdoc} |
|
238
|
|
|
*/ |
|
239
|
|
|
public function getExtra() : Utils\ArrayHash |
|
240
|
|
|
{ |
|
241
|
|
|
$data = []; |
|
242
|
|
|
|
|
243
|
|
|
if (isset($this->composerData['extra'])) { |
|
244
|
|
|
$data = $this->composerData['extra']; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
return Utils\ArrayHash::from($data); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* {@inheritdoc} |
|
252
|
|
|
*/ |
|
253
|
|
|
public function getConfiguration() : Utils\ArrayHash |
|
254
|
|
|
{ |
|
255
|
|
|
$data = []; |
|
256
|
|
|
|
|
257
|
|
|
if (isset($this->composerData['extra']['ipub']['configuration'])) { |
|
258
|
|
|
$data = $this->composerData['extra']['ipub']['configuration']; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
return Utils\ArrayHash::from($data); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* {@inheritdoc} |
|
266
|
|
|
*/ |
|
267
|
|
|
public function getScripts() : Utils\ArrayHash |
|
268
|
|
|
{ |
|
269
|
|
|
$scripts = ['IPub\Packages\Scripts\ConfigurationScript']; |
|
270
|
|
|
|
|
271
|
|
|
if (isset($this->composerData['extra']['ipub']['scripts'])) { |
|
272
|
|
|
$scripts = array_merge($scripts, $this->composerData['extra']['ipub']['scripts']); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
return Utils\ArrayHash::from($scripts); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* {@inheritdoc} |
|
280
|
|
|
*/ |
|
281
|
|
|
public function getUniqueName() : string |
|
282
|
|
|
{ |
|
283
|
|
|
return sprintf('%s-%s', $this->getName(), $this->composerData['version']); |
|
|
|
|
|
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* {@inheritdoc} |
|
288
|
|
|
*/ |
|
289
|
|
|
public function getPath() : string |
|
290
|
|
|
{ |
|
291
|
|
|
$reflectionClass = new \ReflectionClass($this); |
|
292
|
|
|
|
|
293
|
|
|
return dirname($reflectionClass->getFileName()); |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* {@inheritdoc} |
|
298
|
|
|
*/ |
|
299
|
|
|
public function compare(IPackage $package, string $operator = '==') : bool |
|
300
|
|
|
{ |
|
301
|
|
|
return strtolower($this->getName()) === strtolower($package->getName()) && |
|
|
|
|
|
|
302
|
|
|
version_compare(strtolower($this->getVersion()), strtolower($package->getVersion()), $operator); |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* {@inheritdoc} |
|
307
|
|
|
*/ |
|
308
|
|
|
public function __toString() : string |
|
309
|
|
|
{ |
|
310
|
|
|
return $this->getUniqueName(); |
|
311
|
|
|
} |
|
312
|
|
|
} |
|
313
|
|
|
|