| 1 |
<?php |
| 2 |
/** |
| 3 |
* This file is part of the Composer Merge plugin. |
| 4 |
* |
| 5 |
* Copyright (C) 2015 Bryan Davis, Wikimedia Foundation, and contributors |
| 6 |
* |
| 7 |
* This software may be modified and distributed under the terms of the MIT |
| 8 |
* license. See the LICENSE file for details. |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace Wikimedia\Composer\Merge\V2; |
| 12 |
|
| 13 |
use Composer\Composer; |
| 14 |
use Composer\Json\JsonFile; |
| 15 |
use Composer\Package\BasePackage; |
| 16 |
use Composer\Package\CompletePackage; |
| 17 |
use Composer\Package\Link; |
| 18 |
use Composer\Package\Loader\ArrayLoader; |
| 19 |
use Composer\Package\RootAliasPackage; |
| 20 |
use Composer\Package\RootPackage; |
| 21 |
use Composer\Package\RootPackageInterface; |
| 22 |
use Composer\Package\Version\VersionParser; |
| 23 |
use Composer\Semver\Intervals; |
| 24 |
use UnexpectedValueException; |
| 25 |
|
| 26 |
/** |
| 27 |
* Processing for a composer.json file that will be merged into |
| 28 |
* a RootPackageInterface |
| 29 |
* |
| 30 |
* @author Bryan Davis <[email protected]> |
| 31 |
*/ |
| 32 |
class ExtraPackage |
| 33 |
{ |
| 34 |
|
| 35 |
/** |
| 36 |
* @var Composer $composer |
| 37 |
*/ |
| 38 |
protected $composer; |
| 39 |
|
| 40 |
/** |
| 41 |
* @var Logger $logger |
| 42 |
*/ |
| 43 |
protected $logger; |
| 44 |
|
| 45 |
/** |
| 46 |
* @var string $path |
| 47 |
*/ |
| 48 |
protected $path; |
| 49 |
|
| 50 |
/** |
| 51 |
* @var array $json |
| 52 |
*/ |
| 53 |
protected $json; |
| 54 |
|
| 55 |
/** |
| 56 |
* @var CompletePackage $package |
| 57 |
*/ |
| 58 |
protected $package; |
| 59 |
|
| 60 |
/** |
| 61 |
* @var array<string, bool> $mergedRequirements |
| 62 |
*/ |
| 63 |
protected $mergedRequirements = []; |
| 64 |
|
| 65 |
/** |
| 66 |
* @var VersionParser $versionParser |
| 67 |
*/ |
| 68 |
protected $versionParser; |
| 69 |
|
| 70 |
/** |
| 71 |
* @param string $path Path to composer.json file |
| 72 |
* @param Composer $composer |
| 73 |
* @param Logger $logger |
| 74 |
*/ |
| 75 |
public function __construct($path, Composer $composer, Logger $logger) |
| 76 |
{ |
| 77 |
$this->path = $path; |
| 78 |
$this->composer = $composer; |
| 79 |
$this->logger = $logger; |
| 80 |
$this->json = $this->readPackageJson($path); |
| 81 |
$this->package = $this->loadPackage($this->json); |
| 82 |
$this->versionParser = new VersionParser(); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Get list of additional packages to include if precessing recursively. |
| 87 |
* |
| 88 |
* @return array |
| 89 |
*/ |
| 90 |
public function getIncludes() |
| 91 |
{ |
| 92 |
return isset($this->json['extra']['merge-plugin']['include']) ? |
| 93 |
$this->fixRelativePaths($this->json['extra']['merge-plugin']['include']) : []; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Get list of additional packages to require if precessing recursively. |
| 98 |
* |
| 99 |
* @return array |
| 100 |
*/ |
| 101 |
public function getRequires() |
| 102 |
{ |
| 103 |
return isset($this->json['extra']['merge-plugin']['require']) ? |
| 104 |
$this->fixRelativePaths($this->json['extra']['merge-plugin']['require']) : []; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Get list of merged requirements from this package. |
| 109 |
* |
| 110 |
* @return string[] |
| 111 |
*/ |
| 112 |
public function getMergedRequirements() |
| 113 |
{ |
| 114 |
return array_keys($this->mergedRequirements); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Read the contents of a composer.json style file into an array. |
| 119 |
* |
| 120 |
* The package contents are fixed up to be usable to create a Package |
| 121 |
* object by providing dummy "name" and "version" values if they have not |
| 122 |
* been provided in the file. This is consistent with the default root |
| 123 |
* package loading behavior of Composer. |
| 124 |
* |
| 125 |
* @param string $path |
| 126 |
* @return array |
| 127 |
*/ |
| 128 |
protected function readPackageJson($path) |
| 129 |
{ |
| 130 |
$file = new JsonFile($path); |
| 131 |
$json = $file->read(); |
| 132 |
if (!isset($json['name'])) { |
| 133 |
$json['name'] = 'merge-plugin/' . |
| 134 |
strtr($path, DIRECTORY_SEPARATOR, '-'); |
| 135 |
} |
| 136 |
if (!isset($json['version'])) { |
| 137 |
$json['version'] = '1.0.0'; |
| 138 |
} |
| 139 |
return $json; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* @param array $json |
| 144 |
* @return CompletePackage |
| 145 |
*/ |
| 146 |
protected function loadPackage(array $json) |
| 147 |
{ |
| 148 |
$loader = new ArrayLoader(); |
| 149 |
$package = $loader->load($json); |
| 150 |
// @codeCoverageIgnoreStart |
| 151 |
if (!$package instanceof CompletePackage) { |
| 152 |
throw new UnexpectedValueException( |
| 153 |
'Expected instance of CompletePackage, got ' . |
| 154 |
get_class($package) |
| 155 |
); |
| 156 |
} |
| 157 |
// @codeCoverageIgnoreEnd |
| 158 |
return $package; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Merge this package into a RootPackageInterface |
| 163 |
* |
| 164 |
* @param RootPackageInterface $root |
| 165 |
* @param PluginState $state |
| 166 |
*/ |
| 167 |
public function mergeInto(RootPackageInterface $root, PluginState $state) |
| 168 |
{ |
| 169 |
$this->prependRepositories($root); |
| 170 |
|
| 171 |
$this->mergeRequires('require', $root, $state); |
| 172 |
|
| 173 |
$this->mergePackageLinks('conflict', $root); |
| 174 |
|
| 175 |
if ($state->shouldMergeReplace()) { |
| 176 |
$this->mergePackageLinks('replace', $root); |
| 177 |
} |
| 178 |
|
| 179 |
$this->mergePackageLinks('provide', $root); |
| 180 |
|
| 181 |
$this->mergeSuggests($root); |
| 182 |
|
| 183 |
$this->mergeAutoload('autoload', $root); |
| 184 |
|
| 185 |
$this->mergeExtra($root, $state); |
| 186 |
|
| 187 |
$this->mergeScripts($root, $state); |
| 188 |
|
| 189 |
if ($state->isDevMode()) { |
| 190 |
$this->mergeDevInto($root, $state); |
| 191 |
} else { |
| 192 |
$this->mergeReferences($root); |
| 193 |
$this->mergeAliases($root); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Merge just the dev portion into a RootPackageInterface |
| 199 |
* |
| 200 |
* @param RootPackageInterface $root |
| 201 |
* @param PluginState $state |
| 202 |
*/ |
| 203 |
public function mergeDevInto(RootPackageInterface $root, PluginState $state) |
| 204 |
{ |
| 205 |
$this->mergeRequires('require-dev', $root, $state); |
| 206 |
$this->mergeAutoload('devAutoload', $root); |
| 207 |
$this->mergeReferences($root); |
| 208 |
$this->mergeAliases($root); |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Add a collection of repositories described by the given configuration |
| 213 |
* to the given package and the global repository manager. |
| 214 |
* |
| 215 |
* @param RootPackageInterface $root |
| 216 |
*/ |
| 217 |
protected function prependRepositories(RootPackageInterface $root) |
| 218 |
{ |
| 219 |
if (!isset($this->json['repositories'])) { |
| 220 |
return; |
| 221 |
} |
| 222 |
$repoManager = $this->composer->getRepositoryManager(); |
| 223 |
$newRepos = []; |
| 224 |
|
| 225 |
foreach ($this->json['repositories'] as $repoJson) { |
| 226 |
if (!isset($repoJson['type'])) { |
| 227 |
continue; |
| 228 |
} |
| 229 |
if ($repoJson['type'] === 'path' && isset($repoJson['url'])) { |
| 230 |
$repoJson['url'] = $this->fixRelativePaths(array($repoJson['url']))[0]; |
| 231 |
} |
| 232 |
$this->logger->info("Prepending {$repoJson['type']} repository"); |
| 233 |
$repo = $repoManager->createRepository( |
| 234 |
$repoJson['type'], |
| 235 |
$repoJson |
| 236 |
); |
| 237 |
$repoManager->prependRepository($repo); |
| 238 |
$newRepos[] = $repo; |
| 239 |
} |
| 240 |
|
| 241 |
$unwrapped = self::unwrapIfNeeded($root, 'setRepositories'); |
| 242 |
$unwrapped->setRepositories(array_merge( |
| 243 |
$newRepos, |
| 244 |
$root->getRepositories() |
| 245 |
)); |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Merge require or require-dev into a RootPackageInterface |
| 250 |
* |
| 251 |
* @param string $type 'require' or 'require-dev' |
| 252 |
* @param RootPackageInterface $root |
| 253 |
* @param PluginState $state |
| 254 |
*/ |
| 255 |
protected function mergeRequires( |
| 256 |
$type, |
| 257 |
RootPackageInterface $root, |
| 258 |
PluginState $state |
| 259 |
) { |
| 260 |
$linkType = BasePackage::$supportedLinkTypes[$type]; |
| 261 |
$getter = 'get' . ucfirst($linkType['method']); |
| 262 |
$setter = 'set' . ucfirst($linkType['method']); |
| 263 |
|
| 264 |
$requires = $this->package->{$getter}(); |
| 265 |
if (empty($requires)) { |
| 266 |
return; |
| 267 |
} |
| 268 |
|
| 269 |
$this->mergeStabilityFlags($root, $requires); |
| 270 |
|
| 271 |
$requires = $this->replaceSelfVersionDependencies( |
| 272 |
$type, |
| 273 |
$requires, |
| 274 |
$root |
| 275 |
); |
| 276 |
|
| 277 |
$root->{$setter}($this->mergeOrDefer( |
| 278 |
$type, |
| 279 |
$root->{$getter}(), |
| 280 |
$requires, |
| 281 |
$state |
| 282 |
)); |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Merge two collections of package links and collect duplicates for |
| 287 |
* subsequent processing. |
| 288 |
* |
| 289 |
* @param string $type 'require' or 'require-dev' |
| 290 |
* @param array $origin Primary collection |
| 291 |
* @param array $merge Additional collection |
| 292 |
* @param PluginState $state |
| 293 |
* @return array Merged collection |
| 294 |
*/ |
| 295 |
protected function mergeOrDefer( |
| 296 |
$type, |
| 297 |
array $origin, |
| 298 |
array $merge, |
| 299 |
PluginState $state |
| 300 |
) { |
| 301 |
if ($state->ignoreDuplicateLinks() && $state->replaceDuplicateLinks()) { |
| 302 |
$this->logger->warning("Both replace and ignore-duplicates are true. These are mutually exclusive."); |
| 303 |
$this->logger->warning("Duplicate packages will be ignored."); |
| 304 |
} |
| 305 |
|
| 306 |
foreach ($merge as $name => $link) { |
| 307 |
if (isset($origin[$name])) { |
| 308 |
if ($state->ignoreDuplicateLinks()) { |
| 309 |
$this->logger->info("Ignoring duplicate <comment>{$name}</comment>"); |
| 310 |
continue; |
| 311 |
} |
| 312 |
|
| 313 |
if ($state->replaceDuplicateLinks()) { |
| 314 |
$this->logger->info("Replacing <comment>{$name}</comment>"); |
| 315 |
$this->mergedRequirements[$name] = true; |
| 316 |
$origin[$name] = $link; |
| 317 |
} else { |
| 318 |
$this->logger->info("Merging <comment>{$name}</comment>"); |
| 319 |
$this->mergedRequirements[$name] = true; |
| 320 |
$origin[$name] = $this->mergeConstraints($origin[$name], $link, $state); |
| 321 |
} |
| 322 |
} else { |
| 323 |
$this->logger->info("Adding <comment>{$name}</comment>"); |
| 324 |
$this->mergedRequirements[$name] = true; |
| 325 |
$origin[$name] = $link; |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
if (!$state->isComposer1()) { |
| 330 |
Intervals::clear(); |
| 331 |
} |
| 332 |
|
| 333 |
return $origin; |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
* Merge package constraints. |
| 338 |
* |
| 339 |
* Adapted from Composer's UpdateCommand::appendConstraintToLink |
| 340 |
* |
| 341 |
* @param Link $origin The base package link. |
| 342 |
* @param Link $merge The related package link to merge. |
| 343 |
* @param PluginState $state |
| 344 |
* @return Link Merged link. |
| 345 |
*/ |
| 346 |
protected function mergeConstraints(Link $origin, Link $merge, PluginState $state) |
| 347 |
{ |
| 348 |
$oldPrettyString = $origin->getConstraint()->getPrettyString(); |
| 349 |
$newPrettyString = $merge->getConstraint()->getPrettyString(); |
| 350 |
|
| 351 |
if ($state->isComposer1()) { |
| 352 |
$constraintClass = MultiConstraint::class; |
| 353 |
} else { |
| 354 |
$constraintClass = \Composer\Semver\Constraint\MultiConstraint::class; |
| 355 |
|
| 356 |
if (Intervals::isSubsetOf($origin->getConstraint(), $merge->getConstraint())) { |
| 357 |
return $origin; |
| 358 |
} |
| 359 |
|
| 360 |
if (Intervals::isSubsetOf($merge->getConstraint(), $origin->getConstraint())) { |
| 361 |
return $merge; |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
$newConstraint = $constraintClass::create([ |
| 366 |
$origin->getConstraint(), |
| 367 |
$merge->getConstraint() |
| 368 |
], true); |
| 369 |
$newConstraint->setPrettyString($oldPrettyString.', '.$newPrettyString); |
| 370 |
|
| 371 |
return new Link( |
| 372 |
$origin->getSource(), |
| 373 |
$origin->getTarget(), |
| 374 |
$newConstraint, |
| 375 |
$origin->getDescription(), |
| 376 |
$origin->getPrettyConstraint() . ', ' . $newPrettyString |
| 377 |
); |
| 378 |
} |
| 379 |
|
| 380 |
/** |
| 381 |
* Merge autoload or autoload-dev into a RootPackageInterface |
| 382 |
* |
| 383 |
* @param string $type 'autoload' or 'devAutoload' |
| 384 |
* @param RootPackageInterface $root |
| 385 |
*/ |
| 386 |
protected function mergeAutoload($type, RootPackageInterface $root) |
| 387 |
{ |
| 388 |
$getter = 'get' . ucfirst($type); |
| 389 |
$setter = 'set' . ucfirst($type); |
| 390 |
|
| 391 |
$autoload = $this->package->{$getter}(); |
| 392 |
if (empty($autoload)) { |
| 393 |
return; |
| 394 |
} |
| 395 |
|
| 396 |
$unwrapped = self::unwrapIfNeeded($root, $setter); |
| 397 |
$unwrapped->{$setter}(array_merge_recursive( |
| 398 |
$root->{$getter}(), |
| 399 |
$this->fixRelativePaths($autoload) |
| 400 |
)); |
| 401 |
} |
| 402 |
|
| 403 |
/** |
| 404 |
* Fix a collection of paths that are relative to this package to be |
| 405 |
* relative to the base package. |
| 406 |
* |
| 407 |
* @param array $paths |
| 408 |
* @return array |
| 409 |
*/ |
| 410 |
protected function fixRelativePaths(array $paths) |
| 411 |
{ |
| 412 |
$base = dirname($this->path); |
| 413 |
$base = ($base === '.') ? '' : "{$base}/"; |
| 414 |
|
| 415 |
array_walk_recursive( |
| 416 |
$paths, |
| 417 |
function (&$path) use ($base) { |
| 418 |
$path = "{$base}{$path}"; |
| 419 |
} |
| 420 |
); |
| 421 |
return $paths; |
| 422 |
} |
| 423 |
|
| 424 |
/** |
| 425 |
* Extract and merge stability flags from the given collection of |
| 426 |
* requires and merge them into a RootPackageInterface |
| 427 |
* |
| 428 |
* @param RootPackageInterface $root |
| 429 |
* @param array $requires |
| 430 |
*/ |
| 431 |
protected function mergeStabilityFlags( |
| 432 |
RootPackageInterface $root, |
| 433 |
array $requires |
| 434 |
) { |
| 435 |
$flags = $root->getStabilityFlags(); |
| 436 |
$sf = new StabilityFlags($flags, $root->getMinimumStability()); |
| 437 |
|
| 438 |
$unwrapped = self::unwrapIfNeeded($root, 'setStabilityFlags'); |
| 439 |
$unwrapped->setStabilityFlags(array_merge( |
| 440 |
$flags, |
| 441 |
$sf->extractAll($requires) |
| 442 |
)); |
| 443 |
} |
| 444 |
|
| 445 |
/** |
| 446 |
* Merge package links of the given type into a RootPackageInterface |
| 447 |
* |
| 448 |
* @param string $type 'conflict', 'replace' or 'provide' |
| 449 |
* @param RootPackageInterface $root |
| 450 |
*/ |
| 451 |
protected function mergePackageLinks($type, RootPackageInterface $root) |
| 452 |
{ |
| 453 |
$linkType = BasePackage::$supportedLinkTypes[$type]; |
| 454 |
$getter = 'get' . ucfirst($linkType['method']); |
| 455 |
$setter = 'set' . ucfirst($linkType['method']); |
| 456 |
|
| 457 |
$links = $this->package->{$getter}(); |
| 458 |
if (!empty($links)) { |
| 459 |
$unwrapped = self::unwrapIfNeeded($root, $setter); |
| 460 |
// @codeCoverageIgnoreStart |
| 461 |
if ($root !== $unwrapped) { |
| 462 |
$this->logger->warning( |
| 463 |
'This Composer version does not support ' . |
| 464 |
"'{$type}' merging for aliased packages." |
| 465 |
); |
| 466 |
} |
| 467 |
// @codeCoverageIgnoreEnd |
| 468 |
$unwrapped->{$setter}(array_merge( |
| 469 |
$root->{$getter}(), |
| 470 |
$this->replaceSelfVersionDependencies($type, $links, $root) |
| 471 |
)); |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
/** |
| 476 |
* Merge suggested packages into a RootPackageInterface |
| 477 |
* |
| 478 |
* @param RootPackageInterface $root |
| 479 |
*/ |
| 480 |
protected function mergeSuggests(RootPackageInterface $root) |
| 481 |
{ |
| 482 |
$suggests = $this->package->getSuggests(); |
| 483 |
if (!empty($suggests)) { |
| 484 |
$unwrapped = self::unwrapIfNeeded($root, 'setSuggests'); |
| 485 |
$unwrapped->setSuggests(array_merge( |
| 486 |
$root->getSuggests(), |
| 487 |
$suggests |
| 488 |
)); |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
/** |
| 493 |
* Merge extra config into a RootPackageInterface |
| 494 |
* |
| 495 |
* @param RootPackageInterface $root |
| 496 |
* @param PluginState $state |
| 497 |
*/ |
| 498 |
public function mergeExtra(RootPackageInterface $root, PluginState $state) |
| 499 |
{ |
| 500 |
$extra = $this->package->getExtra(); |
| 501 |
unset($extra['merge-plugin']); |
| 502 |
if (!$state->shouldMergeExtra() || empty($extra)) { |
| 503 |
return; |
| 504 |
} |
| 505 |
|
| 506 |
$rootExtra = $root->getExtra(); |
| 507 |
$unwrapped = self::unwrapIfNeeded($root, 'setExtra'); |
| 508 |
|
| 509 |
if ($state->replaceDuplicateLinks()) { |
| 510 |
$unwrapped->setExtra( |
| 511 |
self::mergeExtraArray($state->shouldMergeExtraDeep(), $rootExtra, $extra) |
| 512 |
); |
| 513 |
} else { |
| 514 |
if (!$state->shouldMergeExtraDeep()) { |
| 515 |
foreach (array_intersect( |
| 516 |
array_keys($extra), |
| 517 |
array_keys($rootExtra) |
| 518 |
) as $key) { |
| 519 |
$this->logger->info( |
| 520 |
"Ignoring duplicate <comment>{$key}</comment> in ". |
| 521 |
"<comment>{$this->path}</comment> extra config." |
| 522 |
); |
| 523 |
} |
| 524 |
} |
| 525 |
$unwrapped->setExtra( |
| 526 |
self::mergeExtraArray($state->shouldMergeExtraDeep(), $extra, $rootExtra) |
| 527 |
); |
| 528 |
} |
| 529 |
} |
| 530 |
|
| 531 |
/** |
| 532 |
* Merge scripts config into a RootPackageInterface |
| 533 |
* |
| 534 |
* @param RootPackageInterface $root |
| 535 |
* @param PluginState $state |
| 536 |
*/ |
| 537 |
public function mergeScripts(RootPackageInterface $root, PluginState $state) |
| 538 |
{ |
| 539 |
$scripts = $this->package->getScripts(); |
| 540 |
if (!$state->shouldMergeScripts() || empty($scripts)) { |
| 541 |
return; |
| 542 |
} |
| 543 |
|
| 544 |
$rootScripts = $root->getScripts(); |
| 545 |
$unwrapped = self::unwrapIfNeeded($root, 'setScripts'); |
| 546 |
|
| 547 |
if ($state->replaceDuplicateLinks()) { |
| 548 |
$unwrapped->setScripts( |
| 549 |
array_merge($rootScripts, $scripts) |
| 550 |
); |
| 551 |
} else { |
| 552 |
$unwrapped->setScripts( |
| 553 |
array_merge($scripts, $rootScripts) |
| 554 |
); |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
/** |
| 559 |
* Merges two arrays either via arrayMergeDeep or via array_merge. |
| 560 |
* |
| 561 |
* @param bool $mergeDeep |
| 562 |
* @param array $array1 |
| 563 |
* @param array $array2 |
| 564 |
* @return array |
| 565 |
*/ |
| 566 |
public static function mergeExtraArray($mergeDeep, $array1, $array2) |
| 567 |
{ |
| 568 |
if ($mergeDeep) { |
| 569 |
return NestedArray::mergeDeep($array1, $array2); |
| 570 |
} |
| 571 |
|
| 572 |
return array_merge($array1, $array2); |
| 573 |
} |
| 574 |
|
| 575 |
/** |
| 576 |
* Update Links with a 'self.version' constraint with the root package's |
| 577 |
* version. |
| 578 |
* |
| 579 |
* @param string $type Link type |
| 580 |
* @param array $links |
| 581 |
* @param RootPackageInterface $root |
| 582 |
* @return array |
| 583 |
*/ |
| 584 |
protected function replaceSelfVersionDependencies( |
| 585 |
$type, |
| 586 |
array $links, |
| 587 |
RootPackageInterface $root |
| 588 |
) { |
| 589 |
$linkType = BasePackage::$supportedLinkTypes[$type]; |
| 590 |
$version = $root->getVersion(); |
| 591 |
$prettyVersion = $root->getPrettyVersion(); |
| 592 |
$vp = $this->versionParser; |
| 593 |
|
| 594 |
$method = 'get' . ucfirst($linkType['method']); |
| 595 |
$packages = $root->$method(); |
| 596 |
|
| 597 |
return array_map( |
| 598 |
static function ($link) use ($linkType, $version, $prettyVersion, $vp, $packages) { |
| 599 |
if ('self.version' === $link->getPrettyConstraint()) { |
| 600 |
if (isset($packages[$link->getSource()])) { |
| 601 |
/** @var Link $package */ |
| 602 |
$package = $packages[$link->getSource()]; |
| 603 |
return new Link( |
| 604 |
$link->getSource(), |
| 605 |
$link->getTarget(), |
| 606 |
$vp->parseConstraints($package->getConstraint()->getPrettyString()), |
| 607 |
$linkType['description'], |
| 608 |
$package->getPrettyConstraint() |
| 609 |
); |
| 610 |
} |
| 611 |
|
| 612 |
return new Link( |
| 613 |
$link->getSource(), |
| 614 |
$link->getTarget(), |
| 615 |
$vp->parseConstraints($version), |
| 616 |
$linkType['description'], |
| 617 |
$prettyVersion |
| 618 |
); |
| 619 |
} |
| 620 |
return $link; |
| 621 |
}, |
| 622 |
$links |
| 623 |
); |
| 624 |
} |
| 625 |
|
| 626 |
/** |
| 627 |
* Get a full featured Package from a RootPackageInterface. |
| 628 |
* |
| 629 |
* In Composer versions before 599ad77 the RootPackageInterface only |
| 630 |
* defines a sub-set of operations needed by composer-merge-plugin and |
| 631 |
* RootAliasPackage only implemented those methods defined by the |
| 632 |
* interface. Most of the unimplemented methods in RootAliasPackage can be |
| 633 |
* worked around because the getter methods that are implemented proxy to |
| 634 |
* the aliased package which we can modify by unwrapping. The exception |
| 635 |
* being modifying the 'conflicts', 'provides' and 'replaces' collections. |
| 636 |
* We have no way to actually modify those collections unfortunately in |
| 637 |
* older versions of Composer. |
| 638 |
* |
| 639 |
* @param RootPackageInterface $root |
| 640 |
* @param string $method Method needed |
| 641 |
* @return RootPackageInterface|RootPackage |
| 642 |
*/ |
| 643 |
public static function unwrapIfNeeded( |
| 644 |
RootPackageInterface $root, |
| 645 |
$method = 'setExtra' |
| 646 |
) { |
| 647 |
// @codeCoverageIgnoreStart |
| 648 |
if ($root instanceof RootAliasPackage && |
| 649 |
!method_exists($root, $method) |
| 650 |
) { |
| 651 |
// Unwrap and return the aliased RootPackage. |
| 652 |
$root = $root->getAliasOf(); |
| 653 |
} |
| 654 |
// @codeCoverageIgnoreEnd |
| 655 |
return $root; |
| 656 |
} |
| 657 |
|
| 658 |
protected function mergeAliases(RootPackageInterface $root) |
| 659 |
{ |
| 660 |
$aliases = []; |
| 661 |
$unwrapped = self::unwrapIfNeeded($root, 'setAliases'); |
| 662 |
foreach (array('require', 'require-dev') as $linkType) { |
| 663 |
$linkInfo = BasePackage::$supportedLinkTypes[$linkType]; |
| 664 |
$method = 'get'.ucfirst($linkInfo['method']); |
| 665 |
$links = []; |
| 666 |
foreach ($unwrapped->$method() as $link) { |
| 667 |
$links[$link->getTarget()] = $link->getConstraint()->getPrettyString(); |
| 668 |
} |
| 669 |
$aliases = $this->extractAliases($links, $aliases); |
| 670 |
} |
| 671 |
$unwrapped->setAliases($aliases); |
| 672 |
} |
| 673 |
|
| 674 |
/** |
| 675 |
* Extract aliases from version constraints (dev-branch as 1.0.0). |
| 676 |
* |
| 677 |
* @param array $requires |
| 678 |
* @param array $aliases |
| 679 |
* @return array |
| 680 |
* @see RootPackageLoader::extractAliases() |
| 681 |
*/ |
| 682 |
protected function extractAliases(array $requires, array $aliases) |
| 683 |
{ |
| 684 |
foreach ($requires as $reqName => $reqVersion) { |
| 685 |
if (preg_match('{^([^,\s#]+)(?:#[^ ]+)? +as +([^,\s]+)$}', $reqVersion, $match)) { |
| 686 |
$aliases[] = [ |
| 687 |
'package' => strtolower($reqName), |
| 688 |
'version' => $this->versionParser->normalize($match[1], $reqVersion), |
| 689 |
'alias' => $match[2], |
| 690 |
'alias_normalized' => $this->versionParser->normalize($match[2], $reqVersion), |
| 691 |
]; |
| 692 |
} elseif (strpos($reqVersion, ' as ') !== false) { |
| 693 |
throw new UnexpectedValueException( |
| 694 |
'Invalid alias definition in "'.$reqName.'": "'.$reqVersion.'". ' |
| 695 |
. 'Aliases should be in the form "exact-version as other-exact-version".' |
| 696 |
); |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
return $aliases; |
| 701 |
} |
| 702 |
|
| 703 |
/** |
| 704 |
* Update the root packages reference information. |
| 705 |
* |
| 706 |
* @param RootPackageInterface $root |
| 707 |
*/ |
| 708 |
protected function mergeReferences(RootPackageInterface $root) |
| 709 |
{ |
| 710 |
// Merge source reference information for merged packages. |
| 711 |
// @see RootPackageLoader::load |
| 712 |
$references = []; |
| 713 |
$unwrapped = self::unwrapIfNeeded($root, 'setReferences'); |
| 714 |
foreach (['require', 'require-dev'] as $linkType) { |
| 715 |
$linkInfo = BasePackage::$supportedLinkTypes[$linkType]; |
| 716 |
$method = 'get'.ucfirst($linkInfo['method']); |
| 717 |
$links = []; |
| 718 |
foreach ($unwrapped->$method() as $link) { |
| 719 |
$links[$link->getTarget()] = $link->getConstraint()->getPrettyString(); |
| 720 |
} |
| 721 |
$references = $this->extractReferences($links, $references); |
| 722 |
} |
| 723 |
$unwrapped->setReferences($references); |
| 724 |
} |
| 725 |
|
| 726 |
/** |
| 727 |
* Extract vcs revision from version constraint (dev-master#abc123. |
| 728 |
* |
| 729 |
* @param array $requires |
| 730 |
* @param array $references |
| 731 |
* @return array |
| 732 |
* @see RootPackageLoader::extractReferences() |
| 733 |
*/ |
| 734 |
protected function extractReferences(array $requires, array $references) |
| 735 |
{ |
| 736 |
foreach ($requires as $reqName => $reqVersion) { |
| 737 |
$reqVersion = preg_replace('{^([^,\s@]+) as .+$}', '$1', $reqVersion); |
| 738 |
$stabilityName = VersionParser::parseStability($reqVersion); |
| 739 |
if ($stabilityName === 'dev' && |
| 740 |
preg_match('{^[^,\s@]+?#([a-f0-9]+)$}', $reqVersion, $match) |
| 741 |
) { |
| 742 |
$name = strtolower($reqName); |
| 743 |
$references[$name] = $match[1]; |
| 744 |
} |
| 745 |
} |
| 746 |
|
| 747 |
return $references; |
| 748 |
} |
| 749 |
} |
| 750 |
// vim:sw=4:ts=4:sts=4:et: |