JsonFile.php 4.9 KB

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