JsonManipulator.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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\PlatformRepository;
  13. /**
  14. * @author Jordi Boggiano <j.boggiano@seld.be>
  15. */
  16. class JsonManipulator
  17. {
  18. private static $RECURSE_BLOCKS;
  19. private static $RECURSE_ARRAYS;
  20. private static $JSON_VALUE;
  21. private static $JSON_STRING;
  22. private $contents;
  23. private $newline;
  24. private $indent;
  25. public function __construct($contents)
  26. {
  27. if (!self::$RECURSE_BLOCKS) {
  28. self::$RECURSE_BLOCKS = '(?:[^{}]*+|\{(?:[^{}]*+|\{(?:[^{}]*+|\{(?:[^{}]*+|\{[^{}]*+\})*\})*\})*\})*';
  29. self::$RECURSE_ARRAYS = '(?:[^\]]*+|\[(?:[^\]]*+|\[(?:[^\]]*+|\[(?:[^\]]*+|\[[^\]]*+\])*\])*\])*\]|'.self::$RECURSE_BLOCKS.')*';
  30. self::$JSON_STRING = '"(?:[^\0-\x09\x0a-\x1f\\\\"]+|\\\\["bfnrt/\\\\]|\\\\u[a-fA-F0-9]{4})*+"';
  31. self::$JSON_VALUE = '(?:[0-9.]+|null|true|false|'.self::$JSON_STRING.'|\['.self::$RECURSE_ARRAYS.'\]|\{'.self::$RECURSE_BLOCKS.'\})';
  32. }
  33. $contents = trim($contents);
  34. if ($contents === '') {
  35. $contents = '{}';
  36. }
  37. if (!$this->pregMatch('#^\{(.*)\}$#s', $contents)) {
  38. throw new \InvalidArgumentException('The json file must be an object ({})');
  39. }
  40. $this->newline = false !== strpos($contents, "\r\n") ? "\r\n" : "\n";
  41. $this->contents = $contents === '{}' ? '{' . $this->newline . '}' : $contents;
  42. $this->detectIndenting();
  43. }
  44. public function getContents()
  45. {
  46. return $this->contents . $this->newline;
  47. }
  48. public function addLink($type, $package, $constraint, $sortPackages = false)
  49. {
  50. $decoded = JsonFile::parseJson($this->contents);
  51. // no link of that type yet
  52. if (!isset($decoded[$type])) {
  53. return $this->addMainKey($type, array($package => $constraint));
  54. }
  55. $regex = '{^(\s*\{\s*(?:'.self::$JSON_STRING.'\s*:\s*'.self::$JSON_VALUE.'\s*,\s*)*?)'.
  56. '('.preg_quote(JsonFile::encode($type)).'\s*:\s*)('.self::$JSON_VALUE.')(.*)}s';
  57. if (!$this->pregMatch($regex, $this->contents, $matches)) {
  58. return false;
  59. }
  60. $links = $matches[3];
  61. if (isset($decoded[$type][$package])) {
  62. // update existing link
  63. $packageRegex = str_replace('/', '\\\\?/', preg_quote($package));
  64. $links = preg_replace_callback('{"'.$packageRegex.'"(\s*:\s*)'.self::$JSON_STRING.'}i', function ($m) use ($package, $constraint) {
  65. return JsonFile::encode($package) . $m[1] . '"' . $constraint . '"';
  66. }, $links);
  67. } else {
  68. if ($this->pregMatch('#^\s*\{\s*\S+.*?(\s*\}\s*)$#s', $links, $match)) {
  69. // link missing but non empty links
  70. $links = preg_replace(
  71. '{'.preg_quote($match[1]).'$}',
  72. // addcslashes is used to double up backslashes/$ since preg_replace resolves them as back references otherwise, see #1588
  73. addcslashes(',' . $this->newline . $this->indent . $this->indent . JsonFile::encode($package).': '.JsonFile::encode($constraint) . $match[1], '\\$'),
  74. $links
  75. );
  76. } else {
  77. // links empty
  78. $links = '{' . $this->newline .
  79. $this->indent . $this->indent . JsonFile::encode($package).': '.JsonFile::encode($constraint) . $this->newline .
  80. $this->indent . '}';
  81. }
  82. }
  83. if (true === $sortPackages) {
  84. $requirements = json_decode($links, true);
  85. $this->sortPackages($requirements);
  86. $links = $this->format($requirements);
  87. }
  88. $this->contents = $matches[1] . $matches[2] . $links . $matches[4];
  89. return true;
  90. }
  91. /**
  92. * Sorts packages by importance (platform packages first, then PHP dependencies) and alphabetically.
  93. *
  94. * @link https://getcomposer.org/doc/02-libraries.md#platform-packages
  95. *
  96. * @param array $packages
  97. */
  98. private function sortPackages(array &$packages = array())
  99. {
  100. $prefix = function ($requirement) {
  101. if (preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $requirement)) {
  102. return preg_replace(
  103. array(
  104. '/^php/',
  105. '/^hhvm/',
  106. '/^ext/',
  107. '/^lib/',
  108. '/^\D/',
  109. ),
  110. array(
  111. '0-$0',
  112. '1-$0',
  113. '2-$0',
  114. '3-$0',
  115. '4-$0',
  116. ),
  117. $requirement
  118. );
  119. }
  120. return '5-'.$requirement;
  121. };
  122. uksort($packages, function ($a, $b) use ($prefix) {
  123. return strnatcmp($prefix($a), $prefix($b));
  124. });
  125. }
  126. public function addRepository($name, $config)
  127. {
  128. return $this->addSubNode('repositories', $name, $config);
  129. }
  130. public function removeRepository($name)
  131. {
  132. return $this->removeSubNode('repositories', $name);
  133. }
  134. public function addConfigSetting($name, $value)
  135. {
  136. return $this->addSubNode('config', $name, $value);
  137. }
  138. public function removeConfigSetting($name)
  139. {
  140. return $this->removeSubNode('config', $name);
  141. }
  142. public function addSubNode($mainNode, $name, $value)
  143. {
  144. $decoded = JsonFile::parseJson($this->contents);
  145. $subName = null;
  146. if (in_array($mainNode, array('config', 'repositories')) && false !== strpos($name, '.')) {
  147. list($name, $subName) = explode('.', $name, 2);
  148. }
  149. // no main node yet
  150. if (!isset($decoded[$mainNode])) {
  151. if ($subName !== null) {
  152. $this->addMainKey($mainNode, array($name => array($subName => $value)));
  153. } else {
  154. $this->addMainKey($mainNode, array($name => $value));
  155. }
  156. return true;
  157. }
  158. // main node content not match-able
  159. $nodeRegex = '{^(\s*\{\s*(?:'.self::$JSON_STRING.'\s*:\s*'.self::$JSON_VALUE.'\s*,\s*)*?)'.
  160. '('.preg_quote(JsonFile::encode($mainNode)).'\s*:\s*\{)('.self::$RECURSE_BLOCKS.')(\})(.*)}s';
  161. try {
  162. if (!$this->pregMatch($nodeRegex, $this->contents, $match)) {
  163. return false;
  164. }
  165. } catch (\RuntimeException $e) {
  166. if ($e->getCode() === PREG_BACKTRACK_LIMIT_ERROR) {
  167. return false;
  168. }
  169. throw $e;
  170. }
  171. $children = $match[3];
  172. // invalid match due to un-regexable content, abort
  173. if (!@json_decode('{'.$children.'}')) {
  174. return false;
  175. }
  176. $that = $this;
  177. // child exists
  178. if ($this->pregMatch('{("'.preg_quote($name).'"\s*:\s*)('.self::$JSON_VALUE.')(,?)}', $children, $matches)) {
  179. $children = preg_replace_callback('{("'.preg_quote($name).'"\s*:\s*)('.self::$JSON_VALUE.')(,?)}', function ($matches) use ($name, $subName, $value, $that) {
  180. if ($subName !== null) {
  181. $curVal = json_decode($matches[2], true);
  182. $curVal[$subName] = $value;
  183. $value = $curVal;
  184. }
  185. return $matches[1] . $that->format($value, 1) . $matches[3];
  186. }, $children);
  187. } elseif ($this->pregMatch('#[^\s](\s*)$#', $children, $match)) {
  188. if ($subName !== null) {
  189. $value = array($subName => $value);
  190. }
  191. // child missing but non empty children
  192. $children = preg_replace(
  193. '#'.$match[1].'$#',
  194. addcslashes(',' . $this->newline . $this->indent . $this->indent . JsonFile::encode($name).': '.$this->format($value, 1) . $match[1], '\\$'),
  195. $children
  196. );
  197. } else {
  198. if ($subName !== null) {
  199. $value = array($subName => $value);
  200. }
  201. // children present but empty
  202. $children = $this->newline . $this->indent . $this->indent . JsonFile::encode($name).': '.$this->format($value, 1) . $children;
  203. }
  204. $this->contents = preg_replace_callback($nodeRegex, function ($m) use ($children) {
  205. return $m[1] . $m[2] . $children . $m[4] . $m[5];
  206. }, $this->contents);
  207. return true;
  208. }
  209. public function removeSubNode($mainNode, $name)
  210. {
  211. $decoded = JsonFile::parseJson($this->contents);
  212. // no node or empty node
  213. if (empty($decoded[$mainNode])) {
  214. return true;
  215. }
  216. // no node content match-able
  217. $nodeRegex = '{^(\s*\{\s*(?:'.self::$JSON_STRING.'\s*:\s*'.self::$JSON_VALUE.'\s*,\s*)*?)'.
  218. '('.preg_quote(JsonFile::encode($mainNode)).'\s*:\s*\{)('.self::$RECURSE_BLOCKS.')(\})(.*)}s';
  219. try {
  220. if (!$this->pregMatch($nodeRegex, $this->contents, $match)) {
  221. return false;
  222. }
  223. } catch (\RuntimeException $e) {
  224. if ($e->getCode() === PREG_BACKTRACK_LIMIT_ERROR) {
  225. return false;
  226. }
  227. throw $e;
  228. }
  229. $children = $match[3];
  230. // invalid match due to un-regexable content, abort
  231. if (!@json_decode('{'.$children.'}', true)) {
  232. return false;
  233. }
  234. $subName = null;
  235. if (in_array($mainNode, array('config', 'repositories')) && false !== strpos($name, '.')) {
  236. list($name, $subName) = explode('.', $name, 2);
  237. }
  238. // no node to remove
  239. if (!isset($decoded[$mainNode][$name]) || ($subName && !isset($decoded[$mainNode][$name][$subName]))) {
  240. return true;
  241. }
  242. // try and find a match for the subkey
  243. if ($this->pregMatch('{"'.preg_quote($name).'"\s*:}i', $children)) {
  244. // find best match for the value of "name"
  245. if (preg_match_all('{"'.preg_quote($name).'"\s*:\s*(?:'.self::$JSON_VALUE.')}', $children, $matches)) {
  246. $bestMatch = '';
  247. foreach ($matches[0] as $match) {
  248. if (strlen($bestMatch) < strlen($match)) {
  249. $bestMatch = $match;
  250. }
  251. }
  252. $childrenClean = preg_replace('{,\s*'.preg_quote($bestMatch).'}i', '', $children, -1, $count);
  253. if (1 !== $count) {
  254. $childrenClean = preg_replace('{'.preg_quote($bestMatch).'\s*,?\s*}i', '', $childrenClean, -1, $count);
  255. if (1 !== $count) {
  256. return false;
  257. }
  258. }
  259. }
  260. } else {
  261. $childrenClean = $children;
  262. }
  263. // no child data left, $name was the only key in
  264. if (!trim($childrenClean)) {
  265. $this->contents = preg_replace($nodeRegex, '$1$2'.$this->newline.$this->indent.'$4$5', $this->contents);
  266. // we have a subname, so we restore the rest of $name
  267. if ($subName !== null) {
  268. $curVal = json_decode('{'.$children.'}', true);
  269. unset($curVal[$name][$subName]);
  270. $this->addSubNode($mainNode, $name, $curVal[$name]);
  271. }
  272. return true;
  273. }
  274. $that = $this;
  275. $this->contents = preg_replace_callback($nodeRegex, function ($matches) use ($that, $name, $subName, $childrenClean) {
  276. if ($subName !== null) {
  277. $curVal = json_decode('{'.$matches[3].'}', true);
  278. unset($curVal[$name][$subName]);
  279. $childrenClean = substr($that->format($curVal, 0), 1, -1);
  280. }
  281. return $matches[1] . $matches[2] . $childrenClean . $matches[4] . $matches[5];
  282. }, $this->contents);
  283. return true;
  284. }
  285. public function addMainKey($key, $content)
  286. {
  287. $decoded = JsonFile::parseJson($this->contents);
  288. $content = $this->format($content);
  289. // key exists already
  290. $regex = '{^(\s*\{\s*(?:'.self::$JSON_STRING.'\s*:\s*'.self::$JSON_VALUE.'\s*,\s*)*?)'.
  291. '('.preg_quote(JsonFile::encode($key)).'\s*:\s*'.self::$JSON_VALUE.')(.*)}s';
  292. if (isset($decoded[$key]) && $this->pregMatch($regex, $this->contents, $matches)) {
  293. // invalid match due to un-regexable content, abort
  294. if (!@json_decode('{'.$matches[2].'}')) {
  295. return false;
  296. }
  297. $this->contents = $matches[1] . JsonFile::encode($key).': '.$content . $matches[3];
  298. return true;
  299. }
  300. // append at the end of the file and keep whitespace
  301. if ($this->pregMatch('#[^{\s](\s*)\}$#', $this->contents, $match)) {
  302. $this->contents = preg_replace(
  303. '#'.$match[1].'\}$#',
  304. addcslashes(',' . $this->newline . $this->indent . JsonFile::encode($key). ': '. $content . $this->newline . '}', '\\$'),
  305. $this->contents
  306. );
  307. return true;
  308. }
  309. // append at the end of the file
  310. $this->contents = preg_replace(
  311. '#\}$#',
  312. addcslashes($this->indent . JsonFile::encode($key). ': '.$content . $this->newline . '}', '\\$'),
  313. $this->contents
  314. );
  315. return true;
  316. }
  317. public function format($data, $depth = 0)
  318. {
  319. if (is_array($data)) {
  320. reset($data);
  321. if (is_numeric(key($data))) {
  322. foreach ($data as $key => $val) {
  323. $data[$key] = $this->format($val, $depth + 1);
  324. }
  325. return '['.implode(', ', $data).']';
  326. }
  327. $out = '{' . $this->newline;
  328. $elems = array();
  329. foreach ($data as $key => $val) {
  330. $elems[] = str_repeat($this->indent, $depth + 2) . JsonFile::encode($key). ': '.$this->format($val, $depth + 1);
  331. }
  332. return $out . implode(','.$this->newline, $elems) . $this->newline . str_repeat($this->indent, $depth + 1) . '}';
  333. }
  334. return JsonFile::encode($data);
  335. }
  336. protected function detectIndenting()
  337. {
  338. if ($this->pregMatch('{^([ \t]+)"}m', $this->contents, $match)) {
  339. $this->indent = $match[1];
  340. } else {
  341. $this->indent = ' ';
  342. }
  343. }
  344. protected function pregMatch($re, $str, &$matches = array())
  345. {
  346. $count = preg_match($re, $str, $matches);
  347. if ($count === false) {
  348. switch (preg_last_error()) {
  349. case PREG_NO_ERROR:
  350. throw new \RuntimeException('Failed to execute regex: PREG_NO_ERROR', PREG_NO_ERROR);
  351. case PREG_INTERNAL_ERROR:
  352. throw new \RuntimeException('Failed to execute regex: PREG_INTERNAL_ERROR', PREG_INTERNAL_ERROR);
  353. case PREG_BACKTRACK_LIMIT_ERROR:
  354. throw new \RuntimeException('Failed to execute regex: PREG_BACKTRACK_LIMIT_ERROR', PREG_BACKTRACK_LIMIT_ERROR);
  355. case PREG_RECURSION_LIMIT_ERROR:
  356. throw new \RuntimeException('Failed to execute regex: PREG_RECURSION_LIMIT_ERROR', PREG_RECURSION_LIMIT_ERROR);
  357. case PREG_BAD_UTF8_ERROR:
  358. throw new \RuntimeException('Failed to execute regex: PREG_BAD_UTF8_ERROR', PREG_BAD_UTF8_ERROR);
  359. case PREG_BAD_UTF8_OFFSET_ERROR:
  360. throw new \RuntimeException('Failed to execute regex: PREG_BAD_UTF8_OFFSET_ERROR', PREG_BAD_UTF8_OFFSET_ERROR);
  361. case 6: // PREG_JIT_STACKLIMIT_ERROR
  362. if (PHP_VERSION_ID > 70000) {
  363. throw new \RuntimeException('Failed to execute regex: PREG_JIT_STACKLIMIT_ERROR', 6);
  364. }
  365. // fallthrough
  366. default:
  367. throw new \RuntimeException('Failed to execute regex: Unknown error');
  368. }
  369. }
  370. return $count;
  371. }
  372. }