ArrayLoader.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. */
  16. class ArrayLoader
  17. {
  18. protected $supportedLinkTypes = array(
  19. 'require' => 'requires',
  20. 'conflict' => 'conflicts',
  21. 'provide' => 'provides',
  22. 'replace' => 'replaces',
  23. 'recommend' => 'recommends',
  24. 'suggest' => 'suggests',
  25. );
  26. public function load($config)
  27. {
  28. $this->validateConfig($config);
  29. $versionParser = new Package\Version\VersionParser();
  30. $version = $versionParser->parse($config['version']);
  31. $package = new Package\MemoryPackage($config['name'], $version['version'], $version['type']);
  32. $package->setType(isset($config['type']) ? $config['type'] : 'library');
  33. if (isset($config['extra'])) {
  34. $package->setExtra($config['extra']);
  35. }
  36. if (isset($config['license'])) {
  37. $package->setLicense($config['license']);
  38. }
  39. if (isset($config['source'])) {
  40. if (!isset($config['source']['type']) || !isset($config['source']['url'])) {
  41. throw new \UnexpectedValueException(sprintf(
  42. "package source should be specified as {\"type\": ..., \"url\": ...},\n%s given",
  43. json_encode($config['source'])
  44. ));
  45. }
  46. $package->setSourceType($config['source']['type']);
  47. $package->setSourceUrl($config['source']['url']);
  48. }
  49. if (isset($config['dist'])) {
  50. if (!isset($config['dist']['type'])
  51. || !isset($config['dist']['url'])
  52. || !isset($config['dist']['shasum'])) {
  53. throw new \UnexpectedValueException(sprintf(
  54. "package dist should be specified as ".
  55. "{\"type\": ..., \"url\": ..., \"shasum\": ...},\n%s given",
  56. json_encode($config['source'])
  57. ));
  58. }
  59. $package->setDistType($config['dist']['type']);
  60. $package->setDistUrl($config['dist']['url']);
  61. $package->setDistSha1Checksum($config['dist']['shasum']);
  62. }
  63. foreach ($this->supportedLinkTypes as $type => $description) {
  64. if (isset($config[$type])) {
  65. $method = 'set'.ucfirst($description);
  66. $package->{$method}(
  67. $this->loadLinksFromConfig($package->getName(), $description, $config['require'])
  68. );
  69. }
  70. }
  71. return $package;
  72. }
  73. private function loadLinksFromConfig($srcPackageName, $description, array $linksSpecs)
  74. {
  75. $links = array();
  76. foreach ($linksSpecs as $packageName => $version) {
  77. $name = strtolower($packageName);
  78. preg_match('#^([>=<~]*)([\d.]+.*)$#', $version, $match);
  79. if (!$match[1]) {
  80. $match[1] = '=';
  81. }
  82. $constraint = new Package\LinkConstraint\VersionConstraint($match[1], $match[2]);
  83. $links[] = new Package\Link($srcPackageName, $packageName, $constraint, $description);
  84. }
  85. return $links;
  86. }
  87. private function validateConfig(array $config)
  88. {
  89. if (!isset($config['name'])) {
  90. throw new \UnexpectedValueException('name is required for package');
  91. }
  92. if (!isset($config['version'])) {
  93. throw new \UnexpectedValueException('version is required for package');
  94. }
  95. }
  96. }