RootPackageLoader.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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\Version\VersionParser;
  13. use Composer\Repository\RepositoryManager;
  14. /**
  15. * ArrayLoader built for the sole purpose of loading the root package
  16. *
  17. * Sets additional defaults and loads repositories
  18. *
  19. * @author Jordi Boggiano <j.boggiano@seld.be>
  20. */
  21. class RootPackageLoader extends ArrayLoader
  22. {
  23. private $manager;
  24. public function __construct(RepositoryManager $manager, VersionParser $parser = null)
  25. {
  26. $this->manager = $manager;
  27. parent::__construct($parser);
  28. }
  29. public function load($config)
  30. {
  31. if (!isset($config['name'])) {
  32. $config['name'] = '__root__';
  33. }
  34. if (!isset($config['version'])) {
  35. $config['version'] = '1.0.0';
  36. }
  37. $package = parent::load($config);
  38. if (isset($config['require'])) {
  39. $aliases = array();
  40. foreach ($config['require'] as $reqName => $reqVersion) {
  41. if (preg_match('{^([^,\s]+) +as +([^,\s]+)$}', $reqVersion, $match)) {
  42. $aliases[] = array(
  43. 'package' => strtolower($reqName),
  44. 'version' => $this->versionParser->normalize($match[1]),
  45. 'alias' => $this->versionParser->normalize($match[2]),
  46. );
  47. }
  48. }
  49. $package->setAliases($aliases);
  50. }
  51. if (isset($config['repositories'])) {
  52. foreach ($config['repositories'] as $index => $repo) {
  53. if (isset($repo['packagist']) && $repo['packagist'] === false) {
  54. continue;
  55. }
  56. if (!is_array($repo)) {
  57. throw new \UnexpectedValueException('Repository '.$index.' should be an array, '.gettype($repo).' given');
  58. }
  59. if (!isset($repo['type'])) {
  60. throw new \UnexpectedValueException('Repository '.$index.' must have a type defined');
  61. }
  62. $repository = $this->manager->createRepository($repo['type'], $repo);
  63. $this->manager->addRepository($repository);
  64. }
  65. $package->setRepositories($config['repositories']);
  66. }
  67. return $package;
  68. }
  69. }