JsonFile.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\Json;
  12. use Composer\Repository\RepositoryManager;
  13. use Composer\Composer;
  14. /**
  15. * Reads/writes json files.
  16. *
  17. * @author Konstantin Kudryashiv <ever.zet@gmail.com>
  18. * @author Jordi Boggiano <j.boggiano@seld.be>
  19. */
  20. class JsonFile
  21. {
  22. private $path;
  23. /**
  24. * Initializes json file reader/parser.
  25. *
  26. * @param string $lockFile path to a lockfile
  27. */
  28. public function __construct($path)
  29. {
  30. $this->path = $path;
  31. }
  32. public function getPath()
  33. {
  34. return $this->path;
  35. }
  36. /**
  37. * Checks whether json file exists.
  38. *
  39. * @return Boolean
  40. */
  41. public function exists()
  42. {
  43. return is_file($this->path);
  44. }
  45. /**
  46. * Reads json file.
  47. *
  48. * @param string $json path or json string
  49. *
  50. * @return array
  51. */
  52. public function read()
  53. {
  54. $context = stream_context_create(array(
  55. 'http' => array('header' => 'User-Agent: Composer/'.Composer::VERSION."\r\n")
  56. ));
  57. $json = file_get_contents($this->path, false, $context);
  58. if (!$json) {
  59. throw new \RuntimeException('Could not read '.$this->path.', you are probably offline');
  60. }
  61. return static::parseJson($json);
  62. }
  63. /**
  64. * Writes json file.
  65. *
  66. * @param array $hash writes hash into json file
  67. */
  68. public function write(array $hash)
  69. {
  70. $dir = dirname($this->path);
  71. if (!is_dir($dir)) {
  72. if (file_exists($dir)) {
  73. throw new \UnexpectedValueException(
  74. $dir.' exists and is not a directory.'
  75. );
  76. }
  77. if (!mkdir($dir, 0777, true)) {
  78. throw new \UnexpectedValueException(
  79. $dir.' does not exist and could not be created.'
  80. );
  81. }
  82. }
  83. file_put_contents($this->path, json_encode($hash));
  84. }
  85. /**
  86. * Parses json string and returns hash.
  87. *
  88. * @param string $json json string
  89. *
  90. * @return array
  91. */
  92. public static function parseJson($json)
  93. {
  94. $data = json_decode($json, true);
  95. if (null === $data && 'null' !== strtolower($json)) {
  96. switch (json_last_error()) {
  97. case JSON_ERROR_NONE:
  98. $msg = 'No error has occurred, is your composer.json file empty?';
  99. break;
  100. case JSON_ERROR_DEPTH:
  101. $msg = 'The maximum stack depth has been exceeded';
  102. break;
  103. case JSON_ERROR_STATE_MISMATCH:
  104. $msg = 'Invalid or malformed JSON';
  105. break;
  106. case JSON_ERROR_CTRL_CHAR:
  107. $msg = 'Control character error, possibly incorrectly encoded';
  108. break;
  109. case JSON_ERROR_SYNTAX:
  110. $msg = 'Syntax error';
  111. $charOffset = 0;
  112. if (preg_match('#["}\]]\s*(,)\s*\}#', $json, $match, PREG_OFFSET_CAPTURE)) {
  113. $msg .= ', extra comma';
  114. } elseif (preg_match('#(["}\]]) *\r?\n *"#', $json, $match, PREG_OFFSET_CAPTURE)) {
  115. $msg .= ', missing comma';
  116. $charOffset = 1;
  117. } elseif (preg_match('#^ *([a-z0-9_-]+) *:#mi', $json, $match, PREG_OFFSET_CAPTURE)) {
  118. $msg .= ', you must use double quotes (") around keys';
  119. } elseif (preg_match('#(\'.+?\' *:|: *\'.+?\')#', $json, $match, PREG_OFFSET_CAPTURE)) {
  120. $msg .= ', use double quotes (") instead of single quotes (\')';
  121. } elseif (preg_match('#(\[".*?":.*?\])#', $json, $match, PREG_OFFSET_CAPTURE)) {
  122. $msg .= ', you must use the hash syntax (e.g. {"foo": "bar"}) instead of array syntax (e.g. ["foo", "bar"])';
  123. }
  124. if (isset($match[1][1])) {
  125. $preError = substr($json, 0, $match[1][1]);
  126. $msg .= ' on line '.(substr_count($preError, "\n")+1).', char '.abs(strrpos($preError, "\n") - strlen($preError) - $charOffset);
  127. }
  128. break;
  129. case JSON_ERROR_UTF8:
  130. $msg = 'Malformed UTF-8 characters, possibly incorrectly encoded';
  131. break;
  132. }
  133. throw new \UnexpectedValueException('JSON Parse Error: '.$msg);
  134. }
  135. return $data;
  136. }
  137. }