ArrayLoader.php 11 KB

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