ArrayLoader.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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\Repository\RepositoryManager;
  14. /**
  15. * @author Konstantin Kudryashiv <ever.zet@gmail.com>
  16. * @author Jordi Boggiano <j.boggiano@seld.be>
  17. */
  18. class ArrayLoader
  19. {
  20. protected $supportedLinkTypes = array(
  21. 'require' => 'requires',
  22. 'conflict' => 'conflicts',
  23. 'provide' => 'provides',
  24. 'replace' => 'replaces',
  25. 'recommend' => 'recommends',
  26. 'suggest' => 'suggests',
  27. );
  28. protected $versionParser;
  29. private $manager;
  30. public function __construct(RepositoryManager $manager, $parser = null)
  31. {
  32. $this->manager = $manager;
  33. $this->versionParser = $parser;
  34. if (!$parser) {
  35. $this->versionParser = new Package\Version\VersionParser;
  36. }
  37. }
  38. public function load($config)
  39. {
  40. $prettyVersion = isset($config['version']) ? $config['version'] : '0.0.0';
  41. // handle already normalized versions
  42. if (isset($config['version_normalized'])) {
  43. $version = $config['version_normalized'];
  44. } else {
  45. $version = $this->versionParser->normalize($prettyVersion);
  46. }
  47. $package = new Package\MemoryPackage(isset($config['name']) ? $config['name'] : '__app__', $version, $prettyVersion);
  48. $package->setType(isset($config['type']) ? $config['type'] : 'library');
  49. if (isset($config['target-dir'])) {
  50. $package->setTargetDir($config['target-dir']);
  51. }
  52. if (isset($config['repositories'])) {
  53. $repositories = array();
  54. foreach ($config['repositories'] as $repoName => $repo) {
  55. if (false === $repo && 'packagist' === $repoName) {
  56. continue;
  57. }
  58. if (!is_array($repo)) {
  59. throw new \UnexpectedValueException('Repository '.$repoName.' in '.$package->getPrettyName().' '.$package->getVersion().' should be an array, '.gettype($repo).' given');
  60. }
  61. $repository = $this->manager->createRepository(key($repo), current($repo));
  62. $this->manager->addRepository($repository);
  63. }
  64. $package->setRepositories($config['repositories']);
  65. }
  66. if (isset($config['extra']) && is_array($config['extra'])) {
  67. $package->setExtra($config['extra']);
  68. }
  69. if (isset($config['bin']) && is_array($config['bin'])) {
  70. foreach ($config['bin'] as $key => $bin) {
  71. $config['bin'][$key]= ltrim($bin, '/');
  72. }
  73. $package->setBinaries($config['bin']);
  74. }
  75. if (!empty($config['description']) && is_string($config['description'])) {
  76. $package->setDescription($config['description']);
  77. }
  78. if (!empty($config['homepage']) && is_string($config['homepage'])) {
  79. $package->setHomepage($config['homepage']);
  80. }
  81. if (!empty($config['keywords'])) {
  82. $package->setKeywords(is_array($config['keywords']) ? $config['keywords'] : array($config['keywords']));
  83. }
  84. if (!empty($config['license'])) {
  85. $package->setLicense(is_array($config['license']) ? $config['license'] : array($config['license']));
  86. }
  87. if (!empty($config['time'])) {
  88. try {
  89. $package->setReleaseDate(new \DateTime($config['time']));
  90. } catch (\Exception $e) {
  91. }
  92. }
  93. if (!empty($config['authors']) && is_array($config['authors'])) {
  94. $package->setAuthors($config['authors']);
  95. }
  96. if (isset($config['installation-source'])) {
  97. $package->setInstallationSource($config['installation-source']);
  98. }
  99. if (isset($config['source'])) {
  100. if (!isset($config['source']['type']) || !isset($config['source']['url'])) {
  101. throw new \UnexpectedValueException(sprintf(
  102. "package source should be specified as {\"type\": ..., \"url\": ...},\n%s given",
  103. json_encode($config['source'])
  104. ));
  105. }
  106. $package->setSourceType($config['source']['type']);
  107. $package->setSourceUrl($config['source']['url']);
  108. $package->setSourceReference($config['source']['reference']);
  109. }
  110. if (isset($config['dist'])) {
  111. if (!isset($config['dist']['type'])
  112. || !isset($config['dist']['url'])) {
  113. throw new \UnexpectedValueException(sprintf(
  114. "package dist should be specified as ".
  115. "{\"type\": ..., \"url\": ..., \"reference\": ..., \"shasum\": ...},\n%s given",
  116. json_encode($config['dist'])
  117. ));
  118. }
  119. $package->setDistType($config['dist']['type']);
  120. $package->setDistUrl($config['dist']['url']);
  121. $package->setDistReference(isset($config['dist']['reference']) ? $config['dist']['reference'] : null);
  122. $package->setDistSha1Checksum(isset($config['dist']['shasum']) ? $config['dist']['shasum'] : null);
  123. }
  124. foreach ($this->supportedLinkTypes as $type => $description) {
  125. if (isset($config[$type])) {
  126. $method = 'set'.ucfirst($description);
  127. $package->{$method}(
  128. $this->loadLinksFromConfig($package, $description, $config[$type])
  129. );
  130. }
  131. }
  132. if (isset($config['autoload'])) {
  133. $package->setAutoload($config['autoload']);
  134. }
  135. return $package;
  136. }
  137. private function loadLinksFromConfig($package, $description, array $linksSpecs)
  138. {
  139. $links = array();
  140. foreach ($linksSpecs as $packageName => $constraint) {
  141. if ('self.version' === $constraint) {
  142. $constraint = $package->getVersion();
  143. }
  144. $parsedConstraint = $this->versionParser->parseConstraints($constraint);
  145. $links[] = new Package\Link($package->getName(), $packageName, $parsedConstraint, $description, $constraint);
  146. }
  147. return $links;
  148. }
  149. }