PackageRepository.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. parent::__construct();
  30. $this->config = $config['package'];
  31. // make sure we have an array of package definitions
  32. if (!is_numeric(key($this->config))) {
  33. $this->config = array($this->config);
  34. }
  35. }
  36. /**
  37. * Initializes repository (reads file, or remote address).
  38. */
  39. protected function initialize()
  40. {
  41. parent::initialize();
  42. $loader = new ValidatingArrayLoader(new ArrayLoader(null, true), false);
  43. foreach ($this->config as $package) {
  44. try {
  45. $package = $loader->load($package);
  46. } catch (\Exception $e) {
  47. throw new InvalidRepositoryException('A repository of type "package" contains an invalid package definition: '.$e->getMessage()."\n\nInvalid package definition:\n".json_encode($package));
  48. }
  49. $this->addPackage($package);
  50. }
  51. }
  52. }