RootPackageLoader.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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\BasePackage;
  13. use Composer\Config;
  14. use Composer\Factory;
  15. use Composer\Package\Version\VersionParser;
  16. use Composer\Repository\RepositoryManager;
  17. use Composer\Util\ProcessExecutor;
  18. /**
  19. * ArrayLoader built for the sole purpose of loading the root package
  20. *
  21. * Sets additional defaults and loads repositories
  22. *
  23. * @author Jordi Boggiano <j.boggiano@seld.be>
  24. */
  25. class RootPackageLoader extends ArrayLoader
  26. {
  27. private $manager;
  28. private $config;
  29. private $process;
  30. public function __construct(RepositoryManager $manager, Config $config, VersionParser $parser = null, ProcessExecutor $process = null)
  31. {
  32. $this->manager = $manager;
  33. $this->config = $config;
  34. $this->process = $process ?: new ProcessExecutor();
  35. parent::__construct($parser);
  36. }
  37. public function load(array $config, $class = 'Composer\Package\RootPackage')
  38. {
  39. if (!isset($config['name'])) {
  40. $config['name'] = '__root__';
  41. }
  42. if (!isset($config['version'])) {
  43. // override with env var if available
  44. if (getenv('COMPOSER_ROOT_VERSION')) {
  45. $version = getenv('COMPOSER_ROOT_VERSION');
  46. } else {
  47. $version = $this->guessVersion($config);
  48. }
  49. if (!$version) {
  50. $version = '1.0.0';
  51. }
  52. $config['version'] = $version;
  53. } else {
  54. $version = $config['version'];
  55. }
  56. $package = parent::load($config, $class);
  57. $aliases = array();
  58. $stabilityFlags = array();
  59. $references = array();
  60. foreach (array('require', 'require-dev') as $linkType) {
  61. if (isset($config[$linkType])) {
  62. $linkInfo = BasePackage::$supportedLinkTypes[$linkType];
  63. $method = 'get'.ucfirst($linkInfo['method']);
  64. $links = array();
  65. foreach ($package->$method() as $link) {
  66. $links[$link->getTarget()] = $link->getConstraint()->getPrettyString();
  67. }
  68. $aliases = $this->extractAliases($links, $aliases);
  69. $stabilityFlags = $this->extractStabilityFlags($links, $stabilityFlags);
  70. $references = $this->extractReferences($links, $references);
  71. }
  72. }
  73. $package->setAliases($aliases);
  74. $package->setStabilityFlags($stabilityFlags);
  75. $package->setReferences($references);
  76. if (isset($config['minimum-stability'])) {
  77. $package->setMinimumStability(VersionParser::normalizeStability($config['minimum-stability']));
  78. }
  79. $repos = Factory::createDefaultRepositories(null, $this->config, $this->manager);
  80. foreach ($repos as $repo) {
  81. $this->manager->addRepository($repo);
  82. }
  83. $package->setRepositories($this->config->getRepositories());
  84. return $package;
  85. }
  86. private function extractAliases(array $requires, array $aliases)
  87. {
  88. foreach ($requires as $reqName => $reqVersion) {
  89. if (preg_match('{^([^,\s]+) +as +([^,\s]+)$}', $reqVersion, $match)) {
  90. $aliases[] = array(
  91. 'package' => strtolower($reqName),
  92. 'version' => $this->versionParser->normalize($match[1], $reqVersion),
  93. 'alias' => $match[2],
  94. 'alias_normalized' => $this->versionParser->normalize($match[2], $reqVersion),
  95. );
  96. }
  97. }
  98. return $aliases;
  99. }
  100. private function extractStabilityFlags(array $requires, array $stabilityFlags)
  101. {
  102. $stabilities = BasePackage::$stabilities;
  103. foreach ($requires as $reqName => $reqVersion) {
  104. // parse explicit stability flags
  105. if (preg_match('{^[^,\s]*?@('.implode('|', array_keys($stabilities)).')$}i', $reqVersion, $match)) {
  106. $name = strtolower($reqName);
  107. $stability = $stabilities[VersionParser::normalizeStability($match[1])];
  108. if (isset($stabilityFlags[$name]) && $stabilityFlags[$name] > $stability) {
  109. continue;
  110. }
  111. $stabilityFlags[$name] = $stability;
  112. continue;
  113. }
  114. // infer flags for requirements that have an explicit -dev or -beta version specified for example
  115. $reqVersion = preg_replace('{^([^,\s@]+) as .+$}', '$1', $reqVersion);
  116. if (preg_match('{^[^,\s@]+$}', $reqVersion) && 'stable' !== ($stabilityName = VersionParser::parseStability($reqVersion))) {
  117. $name = strtolower($reqName);
  118. $stability = $stabilities[$stabilityName];
  119. if (isset($stabilityFlags[$name]) && $stabilityFlags[$name] > $stability) {
  120. continue;
  121. }
  122. $stabilityFlags[$name] = $stability;
  123. }
  124. }
  125. return $stabilityFlags;
  126. }
  127. private function extractReferences(array $requires, array $references)
  128. {
  129. foreach ($requires as $reqName => $reqVersion) {
  130. $reqVersion = preg_replace('{^([^,\s@]+) as .+$}', '$1', $reqVersion);
  131. if (preg_match('{^[^,\s@]+?#([a-f0-9]+)$}', $reqVersion, $match) && 'dev' === ($stabilityName = VersionParser::parseStability($reqVersion))) {
  132. $name = strtolower($reqName);
  133. $references[$name] = $match[1];
  134. }
  135. }
  136. return $references;
  137. }
  138. private function guessVersion(array $config)
  139. {
  140. if (function_exists('proc_open')) {
  141. $version = $this->guessGitVersion($config);
  142. if (null !== $version) {
  143. return $version;
  144. }
  145. return $this->guessHgVersion($config);
  146. }
  147. }
  148. private function guessGitVersion(array $config)
  149. {
  150. // try to fetch current version from git branch
  151. if (0 === $this->process->execute('git branch --no-color --no-abbrev -v', $output)) {
  152. $branches = array();
  153. $isFeatureBranch = false;
  154. $version = null;
  155. // find current branch and collect all branch names
  156. foreach ($this->process->splitLines($output) as $branch) {
  157. if ($branch && preg_match('{^(?:\* ) *(?:[^/ ]+?/)?(\S+|\(no branch\)) *([a-f0-9]+) .*$}', $branch, $match)) {
  158. if ($match[1] === '(no branch)') {
  159. $version = 'dev-'.$match[2];
  160. $isFeatureBranch = true;
  161. } else {
  162. $version = $this->versionParser->normalizeBranch($match[1]);
  163. $isFeatureBranch = 0 === strpos($version, 'dev-');
  164. if ('9999999-dev' === $version) {
  165. $version = 'dev-'.$match[1];
  166. }
  167. }
  168. }
  169. if ($branch && !preg_match('{^ *[^/]+/HEAD }', $branch)) {
  170. if (preg_match('{^(?:\* )? *(?:[^/ ]+?/)?(\S+) *([a-f0-9]+) .*$}', $branch, $match)) {
  171. $branches[] = $match[1];
  172. }
  173. }
  174. }
  175. if (!$isFeatureBranch) {
  176. return $version;
  177. }
  178. // try to find the best (nearest) version branch to assume this feature's version
  179. $version = $this->guessFeatureVersion($config, $version, $branches, 'git rev-list %candidate%..%branch%');
  180. return $version;
  181. }
  182. }
  183. private function guessHgVersion(array $config)
  184. {
  185. // try to fetch current version from hg branch
  186. if (0 === $this->process->execute('hg branch', $output)) {
  187. $branch = trim($output);
  188. $version = $this->versionParser->normalizeBranch($branch);
  189. $isFeatureBranch = 0 === strpos($version, 'dev-');
  190. if ('9999999-dev' === $version) {
  191. $version = 'dev-'.$branch;
  192. }
  193. if (!$isFeatureBranch) {
  194. return $version;
  195. }
  196. // re-use the HgDriver to fetch branches (this properly includes bookmarks)
  197. $config = array('url' => getcwd());
  198. $driver = new HgDriver($config, new NullIO(), $this->config, $this->process);
  199. $branches = array_keys($driver->getBranches());
  200. // try to find the best (nearest) version branch to assume this feature's version
  201. $version = $this->guessFeatureVersion($config, $version, $branches, 'hg log -r "not ancestors(\'%candidate%\') and ancestors(\'%branch%\')" --template "{node}\\n"');
  202. return $version;
  203. }
  204. }
  205. private function guessFeatureVersion(array $config, $version, array $branches, $scmCmdline)
  206. {
  207. // ignore feature branches if they have no branch-alias or self.version is used
  208. // and find the branch they came from to use as a version instead
  209. if ((isset($config['extra']['branch-alias']) && !isset($config['extra']['branch-alias'][$version]))
  210. || strpos(json_encode($config), '"self.version"')
  211. ) {
  212. $branch = preg_replace('{^dev-}', '', $version);
  213. $length = PHP_INT_MAX;
  214. foreach ($branches as $candidate) {
  215. // do not compare against other feature branches
  216. if ($candidate === $branch || !preg_match('{^(master|trunk|default|develop|\d+\..+)$}', $candidate, $match)) {
  217. continue;
  218. }
  219. $cmdLine = str_replace(array('%candidate%', '%branch%'), array($candidate, $branch), $scmCmdline);
  220. if (0 !== $this->process->execute($cmdLine, $output)) {
  221. continue;
  222. }
  223. if (strlen($output) < $length) {
  224. $length = strlen($output);
  225. $version = $this->versionParser->normalizeBranch($candidate);
  226. if ('9999999-dev' === $version) {
  227. $version = 'dev-'.$match[1];
  228. }
  229. }
  230. }
  231. }
  232. return $version;
  233. }
  234. }