RootPackageLoader.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Package\Loader;
  12. use Composer\Package\Version\VersionParser;
  13. use Composer\Repository\RepositoryManager;
  14. use Composer\Util\ProcessExecutor;
  15. use Composer\Package\AliasPackage;
  16. /**
  17. * ArrayLoader built for the sole purpose of loading the root package
  18. *
  19. * Sets additional defaults and loads repositories
  20. *
  21. * @author Jordi Boggiano <j.boggiano@seld.be>
  22. */
  23. class RootPackageLoader extends ArrayLoader
  24. {
  25. private $manager;
  26. private $process;
  27. public function __construct(RepositoryManager $manager, VersionParser $parser = null, ProcessExecutor $process = null)
  28. {
  29. $this->manager = $manager;
  30. $this->process = $process ?: new ProcessExecutor();
  31. parent::__construct($parser);
  32. }
  33. public function load($config)
  34. {
  35. if (!isset($config['name'])) {
  36. $config['name'] = '__root__';
  37. }
  38. if (!isset($config['version'])) {
  39. $version = '1.0.0';
  40. // try to fetch current version from git branch
  41. if (0 === $this->process->execute('git branch --no-color --no-abbrev -v', $output)) {
  42. foreach ($this->process->splitLines($output) as $branch) {
  43. if ($branch && preg_match('{^(?:\* ) *(?:[^/ ]+?/)?(\S+) *[a-f0-9]+ .*$}', $branch, $match)) {
  44. $version = 'dev-'.$match[1];
  45. }
  46. }
  47. }
  48. $config['version'] = $version;
  49. } else {
  50. $version = $config['version'];
  51. }
  52. $package = parent::load($config);
  53. if (isset($config['require'])) {
  54. $aliases = array();
  55. foreach ($config['require'] as $reqName => $reqVersion) {
  56. if (preg_match('{^([^,\s]+) +as +([^,\s]+)$}', $reqVersion, $match)) {
  57. $aliases[] = array(
  58. 'package' => strtolower($reqName),
  59. 'version' => $this->versionParser->normalize($match[1]),
  60. 'alias' => $match[2],
  61. 'alias_normalized' => $this->versionParser->normalize($match[2]),
  62. );
  63. }
  64. }
  65. $package->setAliases($aliases);
  66. }
  67. if (isset($config['repositories'])) {
  68. foreach ($config['repositories'] as $index => $repo) {
  69. if (isset($repo['packagist']) && $repo['packagist'] === false) {
  70. continue;
  71. }
  72. if (!is_array($repo)) {
  73. throw new \UnexpectedValueException('Repository '.$index.' should be an array, '.gettype($repo).' given');
  74. }
  75. if (!isset($repo['type'])) {
  76. throw new \UnexpectedValueException('Repository '.$index.' ('.json_encode($repo).') must have a type defined');
  77. }
  78. $repository = $this->manager->createRepository($repo['type'], $repo);
  79. $this->manager->addRepository($repository);
  80. }
  81. $package->setRepositories($config['repositories']);
  82. }
  83. if (isset($config['extra']['branch-alias'][$version])
  84. && substr($config['extra']['branch-alias'][$version], -4) === '-dev'
  85. ) {
  86. $targetBranch = $config['extra']['branch-alias'][$version];
  87. $normalized = $this->versionParser->normalizeBranch(substr($targetBranch, 0, -4));
  88. $version = preg_replace('{(\.9{7})+}', '.x', $normalized);
  89. return new AliasPackage($package, $normalized, $version);
  90. }
  91. return $package;
  92. }
  93. }