1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Yosymfony config-loader. |
5
|
|
|
* |
6
|
|
|
* (c) YoSymfony <http://github.com/yosymfony> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Yosymfony\ConfigLoader\Loaders; |
13
|
|
|
|
14
|
|
|
use Yosymfony\ConfigLoader\ConfigFileLoader; |
15
|
|
|
use Yosymfony\ConfigLoader\Repository; |
16
|
|
|
use Yosymfony\ConfigLoader\RepositoryInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* JSON file loader |
20
|
|
|
* |
21
|
|
|
* @author Victor Puertas <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class JsonLoader extends ConfigFileLoader |
24
|
|
|
{ |
25
|
|
|
public const TYPE = "json"; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
* |
30
|
|
|
* @throws RuntimeException If JSON parse error |
31
|
|
|
*/ |
32
|
|
|
public function load(string $resource, string $type = null) : RepositoryInterface |
33
|
|
|
{ |
34
|
|
|
$resourceContent = $resource; |
35
|
|
|
|
36
|
|
|
if (empty($type)) { |
37
|
|
|
$file = $this->getLocation($resource); |
38
|
|
|
$resourceContent = $this->readFile($file); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$parsedResource = $this->parseJson($resourceContent); |
42
|
|
|
$errorMsg = $this->getLastErrorMessage(json_last_error()); |
43
|
|
|
|
44
|
|
|
if ($errorMsg) { |
|
|
|
|
45
|
|
|
$msg = $type ? sprintf('JSON parse error: %s.', $errorMsg) : sprintf('JSON parse error: %s at %s', $errorMsg, $resource); |
46
|
|
|
|
47
|
|
|
throw new \RuntimeException($msg); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$repository = new Repository($parsedResource ?? []); |
51
|
|
|
|
52
|
|
|
return $this->parseImports($repository, $resource); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function supports(string $resource, string $type = null) : bool |
59
|
|
|
{ |
60
|
|
|
return $type === self::TYPE || $this->hasResourceExtension($resource, 'json'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return mixed |
65
|
|
|
*/ |
66
|
|
|
private function parseJson(string $resource) |
67
|
|
|
{ |
68
|
|
|
return json_decode($resource, true); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function getLastErrorMessage(int $errorCode) : ?string |
72
|
|
|
{ |
73
|
|
|
$errors = [ |
74
|
|
|
JSON_ERROR_NONE => null, |
75
|
|
|
JSON_ERROR_DEPTH => 'Maximum stack depth exceeded', |
76
|
|
|
JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch', |
77
|
|
|
JSON_ERROR_CTRL_CHAR => 'Unexpected control character found', |
78
|
|
|
JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON', |
79
|
|
|
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded', |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
if (array_key_exists($errorCode, $errors)) { |
83
|
|
|
return $errors[$errorCode]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return sprintf('Unknown error code: "%s"', $errorCode); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: