ArrayLoader.php 8.2 KB

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