ArrayLoader.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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)
  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 Package\MemoryPackage($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['scripts']) && is_array($config['scripts'])) {
  60. foreach ($config['scripts'] as $event => $listeners) {
  61. $config['scripts'][$event]= (array) $listeners;
  62. }
  63. $package->setScripts($config['scripts']);
  64. }
  65. if (!empty($config['description']) && is_string($config['description'])) {
  66. $package->setDescription($config['description']);
  67. }
  68. if (!empty($config['homepage']) && is_string($config['homepage'])) {
  69. $package->setHomepage($config['homepage']);
  70. }
  71. if (!empty($config['keywords']) && is_array($config['keywords'])) {
  72. $package->setKeywords($config['keywords']);
  73. }
  74. if (!empty($config['license'])) {
  75. $package->setLicense(is_array($config['license']) ? $config['license'] : array($config['license']));
  76. }
  77. if (!empty($config['time'])) {
  78. try {
  79. $date = new \DateTime($config['time']);
  80. $date->setTimezone(new \DateTimeZone('UTC'));
  81. $package->setReleaseDate($date);
  82. } catch (\Exception $e) {
  83. }
  84. }
  85. if (!empty($config['authors']) && is_array($config['authors'])) {
  86. $package->setAuthors($config['authors']);
  87. }
  88. if (isset($config['installation-source'])) {
  89. $package->setInstallationSource($config['installation-source']);
  90. }
  91. if (isset($config['source'])) {
  92. if (!isset($config['source']['type']) || !isset($config['source']['url'])) {
  93. throw new \UnexpectedValueException(sprintf(
  94. "package source should be specified as {\"type\": ..., \"url\": ...},\n%s given",
  95. json_encode($config['source'])
  96. ));
  97. }
  98. $package->setSourceType($config['source']['type']);
  99. $package->setSourceUrl($config['source']['url']);
  100. $package->setSourceReference($config['source']['reference']);
  101. }
  102. if (isset($config['dist'])) {
  103. if (!isset($config['dist']['type'])
  104. || !isset($config['dist']['url'])) {
  105. throw new \UnexpectedValueException(sprintf(
  106. "package dist should be specified as ".
  107. "{\"type\": ..., \"url\": ..., \"reference\": ..., \"shasum\": ...},\n%s given",
  108. json_encode($config['dist'])
  109. ));
  110. }
  111. $package->setDistType($config['dist']['type']);
  112. $package->setDistUrl($config['dist']['url']);
  113. $package->setDistReference(isset($config['dist']['reference']) ? $config['dist']['reference'] : null);
  114. $package->setDistSha1Checksum(isset($config['dist']['shasum']) ? $config['dist']['shasum'] : null);
  115. }
  116. if ($aliasNormalized = $this->getBranchAlias($config)) {
  117. $package->setAlias($aliasNormalized);
  118. $package->setPrettyAlias(preg_replace('{(\.9{7})+}', '.x', $aliasNormalized));
  119. }
  120. foreach (Package\BasePackage::$supportedLinkTypes as $type => $opts) {
  121. if (isset($config[$type])) {
  122. $method = 'set'.ucfirst($opts['method']);
  123. $package->{$method}(
  124. $this->loadLinksFromConfig($package, $opts['description'], $config[$type])
  125. );
  126. }
  127. }
  128. if (isset($config['suggest']) && is_array($config['suggest'])) {
  129. foreach ($config['suggest'] as $target => $reason) {
  130. if ('self.version' === trim($reason)) {
  131. $config['suggest'][$target] = $package->getPrettyVersion();
  132. }
  133. }
  134. $package->setSuggests($config['suggest']);
  135. }
  136. if (isset($config['autoload'])) {
  137. $package->setAutoload($config['autoload']);
  138. }
  139. if (isset($config['include-path'])) {
  140. $package->setIncludePaths($config['include-path']);
  141. }
  142. if (isset($config['support'])) {
  143. $package->setSupport($config['support']);
  144. }
  145. return $package;
  146. }
  147. /**
  148. * Retrieves a branch alias (dev-master => 1.0.x-dev for example) if it exists
  149. *
  150. * @param array $config the entire package config
  151. * @return string|null normalized version of the branch alias or null if there is none
  152. */
  153. public function getBranchAlias(array $config)
  154. {
  155. if ('dev-' !== substr($config['version'], 0, 4)
  156. || !isset($config['extra']['branch-alias'])
  157. || !is_array($config['extra']['branch-alias'])
  158. ) {
  159. return;
  160. }
  161. foreach ($config['extra']['branch-alias'] as $sourceBranch => $targetBranch) {
  162. // ensure it is an alias to a -dev package
  163. if ('-dev' !== substr($targetBranch, -4)) {
  164. continue;
  165. }
  166. // normalize without -dev and ensure it's a numeric branch that is parseable
  167. $validatedTargetBranch = $this->versionParser->normalizeBranch(substr($targetBranch, 0, -4));
  168. if ('-dev' !== substr($validatedTargetBranch, -4)) {
  169. continue;
  170. }
  171. // ensure that it is the current branch aliasing itself
  172. if (strtolower($config['version']) !== strtolower($sourceBranch)) {
  173. continue;
  174. }
  175. return $validatedTargetBranch;
  176. }
  177. }
  178. private function loadLinksFromConfig($package, $description, array $linksSpecs)
  179. {
  180. $links = array();
  181. foreach ($linksSpecs as $packageName => $constraint) {
  182. if ('self.version' === $constraint) {
  183. $parsedConstraint = $this->versionParser->parseConstraints($package->getPrettyVersion());
  184. } else {
  185. $parsedConstraint = $this->versionParser->parseConstraints($constraint);
  186. }
  187. $links[] = new Package\Link($package->getName(), $packageName, $parsedConstraint, $description, $constraint);
  188. }
  189. return $links;
  190. }
  191. }