ArrayLoader.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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;
  13. use Composer\Package\AliasPackage;
  14. use Composer\Package\Link;
  15. use Composer\Package\RootAliasPackage;
  16. use Composer\Package\RootPackageInterface;
  17. use Composer\Package\Version\VersionParser;
  18. /**
  19. * @author Konstantin Kudryashiv <ever.zet@gmail.com>
  20. * @author Jordi Boggiano <j.boggiano@seld.be>
  21. */
  22. class ArrayLoader implements LoaderInterface
  23. {
  24. protected $versionParser;
  25. protected $loadOptions;
  26. public function __construct(VersionParser $parser = null, $loadOptions = false)
  27. {
  28. if (!$parser) {
  29. $parser = new VersionParser;
  30. }
  31. $this->versionParser = $parser;
  32. $this->loadOptions = $loadOptions;
  33. }
  34. public function load(array $config, $class = 'Composer\Package\CompletePackage')
  35. {
  36. if (!isset($config['name'])) {
  37. throw new \UnexpectedValueException('Unknown package has no name defined ('.json_encode($config).').');
  38. }
  39. if (!isset($config['version'])) {
  40. throw new \UnexpectedValueException('Package '.$config['name'].' has no version defined.');
  41. }
  42. // handle already normalized versions
  43. if (isset($config['version_normalized'])) {
  44. $version = $config['version_normalized'];
  45. } else {
  46. $version = $this->versionParser->normalize($config['version']);
  47. }
  48. $package = new $class($config['name'], $version, $config['version']);
  49. $package->setType(isset($config['type']) ? strtolower($config['type']) : 'library');
  50. if (isset($config['target-dir'])) {
  51. $package->setTargetDir($config['target-dir']);
  52. }
  53. if (isset($config['extra']) && is_array($config['extra'])) {
  54. $package->setExtra($config['extra']);
  55. }
  56. if (isset($config['bin'])) {
  57. if (!is_array($config['bin'])) {
  58. throw new \UnexpectedValueException('Package '.$config['name'].'\'s bin key should be an array, '.gettype($config['bin']).' given.');
  59. }
  60. foreach ($config['bin'] as $key => $bin) {
  61. $config['bin'][$key] = ltrim($bin, '/');
  62. }
  63. $package->setBinaries($config['bin']);
  64. }
  65. if (isset($config['installation-source'])) {
  66. $package->setInstallationSource($config['installation-source']);
  67. }
  68. if (isset($config['source'])) {
  69. if (!isset($config['source']['type']) || !isset($config['source']['url']) || !isset($config['source']['reference'])) {
  70. throw new \UnexpectedValueException(sprintf(
  71. "Package %s's source key should be specified as {\"type\": ..., \"url\": ..., \"reference\": ...},\n%s given.",
  72. $config['name'],
  73. json_encode($config['source'])
  74. ));
  75. }
  76. $package->setSourceType($config['source']['type']);
  77. $package->setSourceUrl($config['source']['url']);
  78. $package->setSourceReference($config['source']['reference']);
  79. if (isset($config['source']['mirrors'])) {
  80. $package->setSourceMirrors($config['source']['mirrors']);
  81. }
  82. }
  83. if (isset($config['dist'])) {
  84. if (!isset($config['dist']['type'])
  85. || !isset($config['dist']['url'])) {
  86. throw new \UnexpectedValueException(sprintf(
  87. "Package %s's dist key should be specified as ".
  88. "{\"type\": ..., \"url\": ..., \"reference\": ..., \"shasum\": ...},\n%s given.",
  89. $config['name'],
  90. json_encode($config['dist'])
  91. ));
  92. }
  93. $package->setDistType($config['dist']['type']);
  94. $package->setDistUrl($config['dist']['url']);
  95. $package->setDistReference(isset($config['dist']['reference']) ? $config['dist']['reference'] : null);
  96. $package->setDistSha1Checksum(isset($config['dist']['shasum']) ? $config['dist']['shasum'] : null);
  97. if (isset($config['dist']['mirrors'])) {
  98. $package->setDistMirrors($config['dist']['mirrors']);
  99. }
  100. }
  101. foreach (Package\BasePackage::$supportedLinkTypes as $type => $opts) {
  102. if (isset($config[$type])) {
  103. $method = 'set'.ucfirst($opts['method']);
  104. $package->{$method}(
  105. $this->parseLinks(
  106. $package->getName(),
  107. $package->getPrettyVersion(),
  108. $opts['description'],
  109. $config[$type]
  110. )
  111. );
  112. }
  113. }
  114. if (isset($config['suggest']) && is_array($config['suggest'])) {
  115. foreach ($config['suggest'] as $target => $reason) {
  116. if ('self.version' === trim($reason)) {
  117. $config['suggest'][$target] = $package->getPrettyVersion();
  118. }
  119. }
  120. $package->setSuggests($config['suggest']);
  121. }
  122. if (isset($config['autoload'])) {
  123. $package->setAutoload($config['autoload']);
  124. }
  125. if (isset($config['autoload-dev'])) {
  126. $package->setDevAutoload($config['autoload-dev']);
  127. }
  128. if (isset($config['include-path'])) {
  129. $package->setIncludePaths($config['include-path']);
  130. }
  131. if (!empty($config['time'])) {
  132. $time = preg_match('/^\d++$/D', $config['time']) ? '@'.$config['time'] : $config['time'];
  133. try {
  134. $date = new \DateTime($time, new \DateTimeZone('UTC'));
  135. $package->setReleaseDate($date);
  136. } catch (\Exception $e) {
  137. }
  138. }
  139. if (!empty($config['notification-url'])) {
  140. $package->setNotificationUrl($config['notification-url']);
  141. }
  142. if (!empty($config['archive']['exclude'])) {
  143. $package->setArchiveExcludes($config['archive']['exclude']);
  144. }
  145. if ($package instanceof Package\CompletePackageInterface) {
  146. if (isset($config['scripts']) && is_array($config['scripts'])) {
  147. foreach ($config['scripts'] as $event => $listeners) {
  148. $config['scripts'][$event] = (array) $listeners;
  149. }
  150. $package->setScripts($config['scripts']);
  151. }
  152. if (!empty($config['description']) && is_string($config['description'])) {
  153. $package->setDescription($config['description']);
  154. }
  155. if (!empty($config['homepage']) && is_string($config['homepage'])) {
  156. $package->setHomepage($config['homepage']);
  157. }
  158. if (!empty($config['keywords']) && is_array($config['keywords'])) {
  159. $package->setKeywords($config['keywords']);
  160. }
  161. if (!empty($config['license'])) {
  162. $package->setLicense(is_array($config['license']) ? $config['license'] : array($config['license']));
  163. }
  164. if (!empty($config['authors']) && is_array($config['authors'])) {
  165. $package->setAuthors($config['authors']);
  166. }
  167. if (isset($config['support'])) {
  168. $package->setSupport($config['support']);
  169. }
  170. if (isset($config['abandoned'])) {
  171. $package->setAbandoned($config['abandoned']);
  172. }
  173. }
  174. if ($aliasNormalized = $this->getBranchAlias($config)) {
  175. if ($package instanceof RootPackageInterface) {
  176. $package = new RootAliasPackage($package, $aliasNormalized, preg_replace('{(\.9{7})+}', '.x', $aliasNormalized));
  177. } else {
  178. $package = new AliasPackage($package, $aliasNormalized, preg_replace('{(\.9{7})+}', '.x', $aliasNormalized));
  179. }
  180. }
  181. if ($this->loadOptions && isset($config['transport-options'])) {
  182. $package->setTransportOptions($config['transport-options']);
  183. }
  184. return $package;
  185. }
  186. /**
  187. * @param string $source source package name
  188. * @param string $sourceVersion source package version (pretty version ideally)
  189. * @param string $description link description (e.g. requires, replaces, ..)
  190. * @param array $links array of package name => constraint mappings
  191. * @return Link[]
  192. */
  193. public function parseLinks($source, $sourceVersion, $description, $links)
  194. {
  195. $res = array();
  196. foreach ($links as $target => $constraint) {
  197. if ('self.version' === $constraint) {
  198. $parsedConstraint = $this->versionParser->parseConstraints($sourceVersion);
  199. } else {
  200. $parsedConstraint = $this->versionParser->parseConstraints($constraint);
  201. }
  202. $res[strtolower($target)] = new Link($source, $target, $parsedConstraint, $description, $constraint);
  203. }
  204. return $res;
  205. }
  206. /**
  207. * Retrieves a branch alias (dev-master => 1.0.x-dev for example) if it exists
  208. *
  209. * @param array $config the entire package config
  210. * @return string|null normalized version of the branch alias or null if there is none
  211. */
  212. public function getBranchAlias(array $config)
  213. {
  214. if (('dev-' !== substr($config['version'], 0, 4) && '-dev' !== substr($config['version'], -4))
  215. || !isset($config['extra']['branch-alias'])
  216. || !is_array($config['extra']['branch-alias'])
  217. ) {
  218. return;
  219. }
  220. foreach ($config['extra']['branch-alias'] as $sourceBranch => $targetBranch) {
  221. // ensure it is an alias to a -dev package
  222. if ('-dev' !== substr($targetBranch, -4)) {
  223. continue;
  224. }
  225. // normalize without -dev and ensure it's a numeric branch that is parseable
  226. $validatedTargetBranch = $this->versionParser->normalizeBranch(substr($targetBranch, 0, -4));
  227. if ('-dev' !== substr($validatedTargetBranch, -4)) {
  228. continue;
  229. }
  230. // ensure that it is the current branch aliasing itself
  231. if (strtolower($config['version']) !== strtolower($sourceBranch)) {
  232. continue;
  233. }
  234. // If using numeric aliases ensure the alias is a valid subversion
  235. if (($sourcePrefix = $this->versionParser->parseNumericAliasPrefix($sourceBranch))
  236. && ($targetPrefix = $this->versionParser->parseNumericAliasPrefix($targetBranch))
  237. && (stripos($targetPrefix, $sourcePrefix) !== 0)
  238. ) {
  239. continue;
  240. }
  241. return $validatedTargetBranch;
  242. }
  243. }
  244. }