-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
ArrayLoader.php
375 lines (311 loc) · 14.3 KB
/
ArrayLoader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Package\Loader;
use Composer\Package;
use Composer\Package\AliasPackage;
use Composer\Package\Link;
use Composer\Package\RootAliasPackage;
use Composer\Package\RootPackageInterface;
use Composer\Package\Version\VersionParser;
/**
* @author Konstantin Kudryashiv <ever.zet@gmail.com>
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class ArrayLoader implements LoaderInterface
{
protected $versionParser;
protected $loadOptions;
public function __construct(VersionParser $parser = null, $loadOptions = false)
{
if (!$parser) {
$parser = new VersionParser;
}
$this->versionParser = $parser;
$this->loadOptions = $loadOptions;
}
public function load(array $config, $class = 'Composer\Package\CompletePackage')
{
$package = $this->createObject($config, $class);
foreach (Package\BasePackage::$supportedLinkTypes as $type => $opts) {
if (isset($config[$type])) {
$method = 'set'.ucfirst($opts['method']);
$package->{$method}(
$this->parseLinks(
$package->getName(),
$package->getPrettyVersion(),
$opts['description'],
$config[$type]
)
);
}
}
$package = $this->configureObject($package, $config);
return $package;
}
public function loadPackages(array $versions, $class)
{
$packages = array();
$linkCache = array();
foreach ($versions as $version) {
$package = $this->createObject($version, $class);
$this->configureCachedLinks($linkCache, $package, $version);
$package = $this->configureObject($package, $version);
$packages[] = $package;
}
return $packages;
}
private function createObject(array $config, $class)
{
if (!isset($config['name'])) {
throw new \UnexpectedValueException('Unknown package has no name defined ('.json_encode($config).').');
}
if (!isset($config['version'])) {
throw new \UnexpectedValueException('Package '.$config['name'].' has no version defined.');
}
// handle already normalized versions
if (isset($config['version_normalized'])) {
$version = $config['version_normalized'];
// handling of existing repos which need to remain composer v1 compatible, in case the version_normalized contained VersionParser::DEFAULT_BRANCH_ALIAS, we renormalize it
if ($version === VersionParser::DEFAULT_BRANCH_ALIAS) {
$version = $this->versionParser->normalize($config['version']);
}
} else {
$version = $this->versionParser->normalize($config['version']);
}
return new $class($config['name'], $version, $config['version']);
}
private function configureObject($package, array $config)
{
$package->setType(isset($config['type']) ? strtolower($config['type']) : 'library');
if (isset($config['target-dir'])) {
$package->setTargetDir($config['target-dir']);
}
if (isset($config['extra']) && \is_array($config['extra'])) {
$package->setExtra($config['extra']);
}
if (isset($config['bin'])) {
foreach ((array) $config['bin'] as $key => $bin) {
$config['bin'][$key] = ltrim($bin, '/');
}
$package->setBinaries((array) $config['bin']);
}
if (isset($config['installation-source'])) {
$package->setInstallationSource($config['installation-source']);
}
if (isset($config['default-branch']) && $config['default-branch'] === true) {
$package->setIsDefaultBranch(true);
}
if (isset($config['source'])) {
if (!isset($config['source']['type']) || !isset($config['source']['url']) || !isset($config['source']['reference'])) {
throw new \UnexpectedValueException(sprintf(
"Package %s's source key should be specified as {\"type\": ..., \"url\": ..., \"reference\": ...},\n%s given.",
$config['name'],
json_encode($config['source'])
));
}
$package->setSourceType($config['source']['type']);
$package->setSourceUrl($config['source']['url']);
$package->setSourceReference(isset($config['source']['reference']) ? $config['source']['reference'] : null);
if (isset($config['source']['mirrors'])) {
$package->setSourceMirrors($config['source']['mirrors']);
}
}
if (isset($config['dist'])) {
if (!isset($config['dist']['type'])
|| !isset($config['dist']['url'])) {
throw new \UnexpectedValueException(sprintf(
"Package %s's dist key should be specified as ".
"{\"type\": ..., \"url\": ..., \"reference\": ..., \"shasum\": ...},\n%s given.",
$config['name'],
json_encode($config['dist'])
));
}
$package->setDistType($config['dist']['type']);
$package->setDistUrl($config['dist']['url']);
$package->setDistReference(isset($config['dist']['reference']) ? $config['dist']['reference'] : null);
$package->setDistSha1Checksum(isset($config['dist']['shasum']) ? $config['dist']['shasum'] : null);
if (isset($config['dist']['mirrors'])) {
$package->setDistMirrors($config['dist']['mirrors']);
}
}
if (isset($config['suggest']) && \is_array($config['suggest'])) {
foreach ($config['suggest'] as $target => $reason) {
if ('self.version' === trim($reason)) {
$config['suggest'][$target] = $package->getPrettyVersion();
}
}
$package->setSuggests($config['suggest']);
}
if (isset($config['autoload'])) {
$package->setAutoload($config['autoload']);
}
if (isset($config['autoload-dev'])) {
$package->setDevAutoload($config['autoload-dev']);
}
if (isset($config['include-path'])) {
$package->setIncludePaths($config['include-path']);
}
if (!empty($config['time'])) {
$time = preg_match('/^\d++$/D', $config['time']) ? '@'.$config['time'] : $config['time'];
try {
$date = new \DateTime($time, new \DateTimeZone('UTC'));
$package->setReleaseDate($date);
} catch (\Exception $e) {
}
}
if (!empty($config['notification-url'])) {
$package->setNotificationUrl($config['notification-url']);
}
if (!empty($config['archive']['name'])) {
$package->setArchiveName($config['archive']['name']);
}
if (!empty($config['archive']['exclude'])) {
$package->setArchiveExcludes($config['archive']['exclude']);
}
if ($package instanceof Package\CompletePackageInterface) {
if (isset($config['scripts']) && \is_array($config['scripts'])) {
foreach ($config['scripts'] as $event => $listeners) {
$config['scripts'][$event] = (array) $listeners;
}
if (isset($config['scripts']['composer'])) {
trigger_error('The `composer` script name is reserved for internal use, please avoid defining it', E_USER_DEPRECATED);
}
$package->setScripts($config['scripts']);
}
if (!empty($config['description']) && \is_string($config['description'])) {
$package->setDescription($config['description']);
}
if (!empty($config['homepage']) && \is_string($config['homepage'])) {
$package->setHomepage($config['homepage']);
}
if (!empty($config['keywords']) && \is_array($config['keywords'])) {
$package->setKeywords($config['keywords']);
}
if (!empty($config['license'])) {
$package->setLicense(\is_array($config['license']) ? $config['license'] : array($config['license']));
}
if (!empty($config['authors']) && \is_array($config['authors'])) {
$package->setAuthors($config['authors']);
}
if (isset($config['support'])) {
$package->setSupport($config['support']);
}
if (!empty($config['funding']) && \is_array($config['funding'])) {
$package->setFunding($config['funding']);
}
if (isset($config['abandoned'])) {
$package->setAbandoned($config['abandoned']);
}
}
if ($this->loadOptions && isset($config['transport-options'])) {
$package->setTransportOptions($config['transport-options']);
}
if ($aliasNormalized = $this->getBranchAlias($config)) {
$prettyAlias = preg_replace('{(\.9{7})+}', '.x', $aliasNormalized);
if ($package instanceof RootPackageInterface) {
return new RootAliasPackage($package, $aliasNormalized, $prettyAlias);
}
return new AliasPackage($package, $aliasNormalized, $prettyAlias);
}
return $package;
}
private function configureCachedLinks(&$linkCache, $package, array $config)
{
$name = $package->getName();
$prettyVersion = $package->getPrettyVersion();
foreach (Package\BasePackage::$supportedLinkTypes as $type => $opts) {
if (isset($config[$type])) {
$method = 'set'.ucfirst($opts['method']);
$links = array();
foreach ($config[$type] as $prettyTarget => $constraint) {
$target = strtolower($prettyTarget);
if ($constraint === 'self.version') {
$links[$target] = $this->createLink($name, $prettyVersion, $opts['description'], $target, $constraint);
} else {
if (!isset($linkCache[$name][$type][$target][$constraint])) {
$linkCache[$name][$type][$target][$constraint] = array($target, $this->createLink($name, $prettyVersion, $opts['description'], $target, $constraint));
}
list($target, $link) = $linkCache[$name][$type][$target][$constraint];
$links[$target] = $link;
}
}
$package->{$method}($links);
}
}
}
/**
* @param string $source source package name
* @param string $sourceVersion source package version (pretty version ideally)
* @param string $description link description (e.g. requires, replaces, ..)
* @param array $links array of package name => constraint mappings
* @return Link[]
*/
public function parseLinks($source, $sourceVersion, $description, $links)
{
$res = array();
foreach ($links as $target => $constraint) {
$res[strtolower($target)] = $this->createLink($source, $sourceVersion, $description, $target, $constraint);
}
return $res;
}
private function createLink($source, $sourceVersion, $description, $target, $prettyConstraint)
{
if (!\is_string($prettyConstraint)) {
throw new \UnexpectedValueException('Link constraint in '.$source.' '.$description.' > '.$target.' should be a string, got '.\gettype($prettyConstraint) . ' (' . var_export($prettyConstraint, true) . ')');
}
if ('self.version' === $prettyConstraint) {
$parsedConstraint = $this->versionParser->parseConstraints($sourceVersion);
} else {
$parsedConstraint = $this->versionParser->parseConstraints($prettyConstraint);
}
return new Link($source, $target, $parsedConstraint, $description, $prettyConstraint);
}
/**
* Retrieves a branch alias (dev-master => 1.0.x-dev for example) if it exists
*
* @param array $config the entire package config
* @return string|null normalized version of the branch alias or null if there is none
*/
public function getBranchAlias(array $config)
{
if (strpos($config['version'], 'dev-') !== 0 && '-dev' !== substr($config['version'], -4)) {
return;
}
if (isset($config['extra']['branch-alias']) && \is_array($config['extra']['branch-alias'])) {
foreach ($config['extra']['branch-alias'] as $sourceBranch => $targetBranch) {
// ensure it is an alias to a -dev package
if ('-dev' !== substr($targetBranch, -4)) {
continue;
}
// normalize without -dev and ensure it's a numeric branch that is parseable
$validatedTargetBranch = $this->versionParser->normalizeBranch(substr($targetBranch, 0, -4));
if ('-dev' !== substr($validatedTargetBranch, -4)) {
continue;
}
// ensure that it is the current branch aliasing itself
if (strtolower($config['version']) !== strtolower($sourceBranch)) {
continue;
}
// If using numeric aliases ensure the alias is a valid subversion
if (($sourcePrefix = $this->versionParser->parseNumericAliasPrefix($sourceBranch))
&& ($targetPrefix = $this->versionParser->parseNumericAliasPrefix($targetBranch))
&& (stripos($targetPrefix, $sourcePrefix) !== 0)
) {
continue;
}
return $validatedTargetBranch;
}
}
if (isset($config['default-branch']) && $config['default-branch'] === true) {
return VersionParser::DEFAULT_BRANCH_ALIAS;
}
}
}