JsonLoader.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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\Json\JsonFile;
  13. /**
  14. * @author Konstantin Kudryashiv <ever.zet@gmail.com>
  15. */
  16. class JsonLoader
  17. {
  18. private $loader;
  19. public function __construct(LoaderInterface $loader)
  20. {
  21. $this->loader = $loader;
  22. }
  23. /**
  24. * @param string|JsonFile $json A filename, json string or JsonFile instance to load the package from
  25. * @return \Composer\Package\PackageInterface
  26. */
  27. public function load($json)
  28. {
  29. if ($json instanceof JsonFile) {
  30. $config = $json->read();
  31. } elseif (file_exists($json)) {
  32. $config = JsonFile::parseJson(file_get_contents($json), $json);
  33. } elseif (is_string($json)) {
  34. $config = JsonFile::parseJson($json);
  35. } else {
  36. throw new \InvalidArgumentException(sprintf(
  37. "JsonLoader: Unknown \$json parameter %s. Please report at https://github.com/composer/composer/issues/new.",
  38. gettype($json)
  39. ));
  40. }
  41. return $this->loader->load($config);
  42. }
  43. }