ArrayLoader.php 9.9 KB

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