2011-10-01 12:32:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Composer\Json;
|
|
|
|
|
|
|
|
use Composer\Repository\RepositoryManager;
|
2011-10-02 20:39:15 +00:00
|
|
|
use Composer\Composer;
|
2011-10-01 12:32:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads/writes json files.
|
|
|
|
*
|
|
|
|
* @author Konstantin Kudryashiv <ever.zet@gmail.com>
|
2011-10-31 13:43:41 +00:00
|
|
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
2011-10-01 12:32:03 +00:00
|
|
|
*/
|
|
|
|
class JsonFile
|
|
|
|
{
|
|
|
|
private $path;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes json file reader/parser.
|
|
|
|
*
|
|
|
|
* @param string $lockFile path to a lockfile
|
|
|
|
*/
|
|
|
|
public function __construct($path)
|
|
|
|
{
|
|
|
|
$this->path = $path;
|
|
|
|
}
|
|
|
|
|
2011-10-30 08:09:46 +00:00
|
|
|
public function getPath()
|
|
|
|
{
|
|
|
|
return $this->path;
|
|
|
|
}
|
|
|
|
|
2011-10-01 12:32:03 +00:00
|
|
|
/**
|
|
|
|
* Checks whether json file exists.
|
|
|
|
*
|
|
|
|
* @return Boolean
|
|
|
|
*/
|
|
|
|
public function exists()
|
|
|
|
{
|
|
|
|
return is_file($this->path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads json file.
|
|
|
|
*
|
|
|
|
* @param string $json path or json string
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function read()
|
|
|
|
{
|
2011-10-02 20:39:15 +00:00
|
|
|
$context = stream_context_create(array(
|
|
|
|
'http' => array('header' => 'User-Agent: Composer/'.Composer::VERSION."\r\n")
|
|
|
|
));
|
|
|
|
|
|
|
|
$json = file_get_contents($this->path, false, $context);
|
2011-10-22 20:18:44 +00:00
|
|
|
if (!$json) {
|
|
|
|
throw new \RuntimeException('Could not read '.$this->path.', you are probably offline');
|
|
|
|
}
|
2011-10-01 13:01:33 +00:00
|
|
|
|
|
|
|
return static::parseJson($json);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes json file.
|
|
|
|
*
|
|
|
|
* @param array $hash writes hash into json file
|
2012-01-17 00:33:02 +00:00
|
|
|
* @param Boolean $prettyPrint If true, output is pretty-printed
|
2011-10-01 13:01:33 +00:00
|
|
|
*/
|
2012-01-17 00:33:02 +00:00
|
|
|
public function write(array $hash, $prettyPrint = true)
|
2011-10-01 13:01:33 +00:00
|
|
|
{
|
2011-10-02 18:46:33 +00:00
|
|
|
$dir = dirname($this->path);
|
|
|
|
if (!is_dir($dir)) {
|
|
|
|
if (file_exists($dir)) {
|
|
|
|
throw new \UnexpectedValueException(
|
|
|
|
$dir.' exists and is not a directory.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!mkdir($dir, 0777, true)) {
|
|
|
|
throw new \UnexpectedValueException(
|
|
|
|
$dir.' does not exist and could not be created.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2012-01-17 00:33:02 +00:00
|
|
|
file_put_contents($this->path, $this->encode($hash, $prettyPrint));
|
2012-01-16 23:42:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-17 00:33:02 +00:00
|
|
|
* Encodes an array into (optionally pretty-printed) JSON
|
2012-01-16 23:42:30 +00:00
|
|
|
*
|
|
|
|
* Original code for this function can be found at:
|
|
|
|
* http://recursive-design.com/blog/2008/03/11/format-json-with-php/
|
|
|
|
*
|
|
|
|
* @param array $hash Data to encode into a formatted JSON string
|
2012-01-17 00:33:02 +00:00
|
|
|
* @param Boolean $prettyPrint If true, output is pretty-printed
|
2012-01-16 23:42:30 +00:00
|
|
|
* @return string Indented version of the original JSON string
|
|
|
|
*/
|
2012-01-17 00:33:02 +00:00
|
|
|
public function encode(array $hash, $prettyPrint = true)
|
2012-01-16 23:42:30 +00:00
|
|
|
{
|
2012-01-17 00:33:02 +00:00
|
|
|
if ($prettyPrint && defined('JSON_PRETTY_PRINT')) {
|
2012-01-16 23:42:30 +00:00
|
|
|
return json_encode($hash, JSON_PRETTY_PRINT);
|
|
|
|
}
|
|
|
|
|
|
|
|
$json = json_encode($hash);
|
|
|
|
|
2012-01-17 00:33:02 +00:00
|
|
|
if (!$prettyPrint) {
|
|
|
|
return $json;
|
|
|
|
}
|
|
|
|
|
2012-01-16 23:42:30 +00:00
|
|
|
$result = '';
|
|
|
|
$pos = 0;
|
|
|
|
$strLen = strlen($json);
|
|
|
|
$indentStr = ' ';
|
|
|
|
$newLine = "\n";
|
|
|
|
$prevChar = '';
|
|
|
|
$outOfQuotes = true;
|
|
|
|
|
|
|
|
for ($i = 0; $i <= $strLen; $i++) {
|
|
|
|
// Grab the next character in the string
|
|
|
|
$char = substr($json, $i, 1);
|
|
|
|
|
|
|
|
// Are we inside a quoted string?
|
2012-01-19 00:14:48 +00:00
|
|
|
if ('"' === $char && ('\\' !== $prevChar || '\\\\' === substr($json, $i-2, 2))) {
|
2012-01-16 23:42:30 +00:00
|
|
|
$outOfQuotes = !$outOfQuotes;
|
2012-01-17 00:33:02 +00:00
|
|
|
} elseif (':' === $char && $outOfQuotes) {
|
2012-01-16 23:42:30 +00:00
|
|
|
// Add a space after the : character
|
|
|
|
$char .= ' ';
|
2012-01-17 00:33:02 +00:00
|
|
|
} elseif (('}' === $char || ']' === $char) && $outOfQuotes) {
|
2012-01-16 23:42:30 +00:00
|
|
|
$pos--;
|
|
|
|
|
|
|
|
if ('{' !== $prevChar && '[' !== $prevChar) {
|
|
|
|
// If this character is the end of an element,
|
|
|
|
// output a new line and indent the next line
|
|
|
|
$result .= $newLine;
|
|
|
|
for ($j = 0; $j < $pos; $j++) {
|
|
|
|
$result .= $indentStr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Collapse empty {} and []
|
|
|
|
$result = rtrim($result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the character to the result string
|
|
|
|
$result .= $char;
|
|
|
|
|
|
|
|
// If the last character was the beginning of an element,
|
|
|
|
// output a new line and indent the next line
|
|
|
|
if ((',' === $char || '{' === $char || '[' === $char) && $outOfQuotes) {
|
|
|
|
$result .= $newLine;
|
|
|
|
|
|
|
|
if ('{' === $char || '[' === $char) {
|
|
|
|
$pos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ($j = 0; $j < $pos; $j++) {
|
|
|
|
$result .= $indentStr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$prevChar = $char;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
2011-10-01 13:01:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses json string and returns hash.
|
|
|
|
*
|
|
|
|
* @param string $json json string
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function parseJson($json)
|
|
|
|
{
|
2011-10-02 18:49:26 +00:00
|
|
|
$data = json_decode($json, true);
|
|
|
|
|
2011-10-02 19:04:37 +00:00
|
|
|
if (null === $data && 'null' !== strtolower($json)) {
|
2011-10-01 12:32:03 +00:00
|
|
|
switch (json_last_error()) {
|
|
|
|
case JSON_ERROR_NONE:
|
|
|
|
$msg = 'No error has occurred, is your composer.json file empty?';
|
|
|
|
break;
|
|
|
|
case JSON_ERROR_DEPTH:
|
|
|
|
$msg = 'The maximum stack depth has been exceeded';
|
|
|
|
break;
|
|
|
|
case JSON_ERROR_STATE_MISMATCH:
|
|
|
|
$msg = 'Invalid or malformed JSON';
|
|
|
|
break;
|
|
|
|
case JSON_ERROR_CTRL_CHAR:
|
|
|
|
$msg = 'Control character error, possibly incorrectly encoded';
|
|
|
|
break;
|
|
|
|
case JSON_ERROR_SYNTAX:
|
|
|
|
$msg = 'Syntax error';
|
2011-10-31 13:43:41 +00:00
|
|
|
$charOffset = 0;
|
2011-11-01 13:13:22 +00:00
|
|
|
if (preg_match('#["}\]]\s*(,)\s*[}\]]#', $json, $match, PREG_OFFSET_CAPTURE)) {
|
2011-10-31 13:43:41 +00:00
|
|
|
$msg .= ', extra comma';
|
2011-11-05 22:53:20 +00:00
|
|
|
} elseif (preg_match('#((?<=[^\\\\])\\\\(?!["\\\\/bfnrt]|u[a-f0-9]{4}))#i', $json, $match, PREG_OFFSET_CAPTURE)) {
|
2011-11-01 15:02:56 +00:00
|
|
|
$msg .= ', unescaped backslash (\\)';
|
2011-10-31 13:43:41 +00:00
|
|
|
} elseif (preg_match('#(["}\]]) *\r?\n *"#', $json, $match, PREG_OFFSET_CAPTURE)) {
|
|
|
|
$msg .= ', missing comma';
|
|
|
|
$charOffset = 1;
|
|
|
|
} elseif (preg_match('#^ *([a-z0-9_-]+) *:#mi', $json, $match, PREG_OFFSET_CAPTURE)) {
|
|
|
|
$msg .= ', you must use double quotes (") around keys';
|
2011-10-02 19:14:00 +00:00
|
|
|
} elseif (preg_match('#(\'.+?\' *:|: *\'.+?\')#', $json, $match, PREG_OFFSET_CAPTURE)) {
|
2011-10-31 13:43:41 +00:00
|
|
|
$msg .= ', use double quotes (") instead of single quotes (\')';
|
|
|
|
} elseif (preg_match('#(\[".*?":.*?\])#', $json, $match, PREG_OFFSET_CAPTURE)) {
|
|
|
|
$msg .= ', you must use the hash syntax (e.g. {"foo": "bar"}) instead of array syntax (e.g. ["foo", "bar"])';
|
|
|
|
}
|
|
|
|
if (isset($match[1][1])) {
|
|
|
|
$preError = substr($json, 0, $match[1][1]);
|
|
|
|
$msg .= ' on line '.(substr_count($preError, "\n")+1).', char '.abs(strrpos($preError, "\n") - strlen($preError) - $charOffset);
|
2011-10-02 19:14:00 +00:00
|
|
|
}
|
2011-10-01 12:32:03 +00:00
|
|
|
break;
|
|
|
|
case JSON_ERROR_UTF8:
|
|
|
|
$msg = 'Malformed UTF-8 characters, possibly incorrectly encoded';
|
|
|
|
break;
|
|
|
|
}
|
2011-10-31 13:43:41 +00:00
|
|
|
throw new \UnexpectedValueException('JSON Parse Error: '.$msg);
|
2011-10-01 12:32:03 +00:00
|
|
|
}
|
|
|
|
|
2011-10-02 18:49:26 +00:00
|
|
|
return $data;
|
2011-10-01 12:32:03 +00:00
|
|
|
}
|
|
|
|
}
|