ArrayLoader.php 8.3 KB

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