ArrayLoader.php 14 KB

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