PackageRepository.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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\Repository;
  12. use Composer\Package\Loader\ArrayLoader;
  13. use Composer\Package\Loader\ValidatingArrayLoader;
  14. /**
  15. * Package repository.
  16. *
  17. * @author Jordi Boggiano <j.boggiano@seld.be>
  18. */
  19. class PackageRepository extends ArrayRepository
  20. {
  21. private $config;
  22. /**
  23. * Initializes filesystem repository.
  24. *
  25. * @param array $config package definition
  26. */
  27. public function __construct(array $config)
  28. {
  29. $this->config = $config['package'];
  30. // make sure we have an array of package definitions
  31. if (!is_numeric(key($this->config))) {
  32. $this->config = array($this->config);
  33. }
  34. }
  35. /**
  36. * Initializes repository (reads file, or remote address).
  37. */
  38. protected function initialize()
  39. {
  40. parent::initialize();
  41. $loader = new ValidatingArrayLoader(new ArrayLoader, false);
  42. foreach ($this->config as $package) {
  43. try {
  44. $package = $loader->load($package);
  45. } catch (\Exception $e) {
  46. throw new InvalidRepositoryException('A repository of type "package" contains an invalid package definition: '.$e->getMessage()."\n\nInvalid package definition:\n".json_encode($package));
  47. }
  48. $this->addPackage($package);
  49. }
  50. }
  51. }