ArrayLoader.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. /**
  14. * @author Konstantin Kudryashiv <ever.zet@gmail.com>
  15. * @author Jordi Boggiano <j.boggiano@seld.be>
  16. */
  17. class ArrayLoader
  18. {
  19. protected $supportedLinkTypes = array(
  20. 'require' => 'requires',
  21. 'conflict' => 'conflicts',
  22. 'provide' => 'provides',
  23. 'replace' => 'replaces',
  24. 'recommend' => 'recommends',
  25. 'suggest' => 'suggests',
  26. );
  27. protected $versionParser;
  28. public function __construct($parser = null)
  29. {
  30. $this->versionParser = $parser;
  31. if (!$parser) {
  32. $this->versionParser = new Package\Version\VersionParser;
  33. }
  34. }
  35. public function load($config)
  36. {
  37. $version = $this->versionParser->normalize(isset($config['version']) ? $config['version'] : '0.0.0');
  38. $package = new Package\MemoryPackage(isset($config['name']) ? $config['name'] : '__app__', $version);
  39. $package->setType(isset($config['type']) ? $config['type'] : 'library');
  40. if (isset($config['installAs'])) {
  41. $package->setInstallAs($config['installAs']);
  42. }
  43. if (isset($config['extra'])) {
  44. $package->setExtra($config['extra']);
  45. }
  46. if (isset($config['license'])) {
  47. $package->setLicense($config['license']);
  48. }
  49. if (isset($config['source'])) {
  50. if (!isset($config['source']['type']) || !isset($config['source']['url'])) {
  51. throw new \UnexpectedValueException(sprintf(
  52. "package source should be specified as {\"type\": ..., \"url\": ...},\n%s given",
  53. json_encode($config['source'])
  54. ));
  55. }
  56. $package->setSourceType($config['source']['type']);
  57. $package->setSourceUrl($config['source']['url']);
  58. $package->setSourceReference($config['source']['reference']);
  59. }
  60. if (isset($config['dist'])) {
  61. if (!isset($config['dist']['type'])
  62. || !isset($config['dist']['url'])
  63. || !isset($config['dist']['shasum'])) {
  64. throw new \UnexpectedValueException(sprintf(
  65. "package dist should be specified as ".
  66. "{\"type\": ..., \"url\": ..., \"shasum\": ...},\n%s given",
  67. json_encode($config['source'])
  68. ));
  69. }
  70. $package->setDistType($config['dist']['type']);
  71. $package->setDistUrl($config['dist']['url']);
  72. $package->setDistReference($config['dist']['reference']);
  73. $package->setDistSha1Checksum($config['dist']['shasum']);
  74. }
  75. foreach ($this->supportedLinkTypes as $type => $description) {
  76. if (isset($config[$type])) {
  77. $method = 'set'.ucfirst($description);
  78. $package->{$method}(
  79. $this->loadLinksFromConfig($package->getName(), $description, $config[$type])
  80. );
  81. }
  82. }
  83. if (isset($config['autoload'])) {
  84. $package->setAutoload($config['autoload']);
  85. }
  86. return $package;
  87. }
  88. private function loadLinksFromConfig($srcPackageName, $description, array $linksSpecs)
  89. {
  90. $links = array();
  91. foreach ($linksSpecs as $packageName => $constraint) {
  92. $name = strtolower($packageName);
  93. $constraint = $this->versionParser->parseConstraints($constraint);
  94. $links[] = new Package\Link($srcPackageName, $packageName, $constraint, $description);
  95. }
  96. return $links;
  97. }
  98. }