-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathJsonConfigDriver.php
More file actions
43 lines (35 loc) · 1.33 KB
/
JsonConfigDriver.php
File metadata and controls
43 lines (35 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
namespace Igorw\Silex;
class JsonConfigDriver implements ConfigDriver
{
public function load($filename)
{
$config = $this->parseJson($filename);
if (JSON_ERROR_NONE !== json_last_error()) {
$jsonError = $this->getJsonError(json_last_error());
throw new \RuntimeException(
sprintf('Invalid JSON provided "%s" in "%s"', $jsonError, $filename));
}
return $config ?: array();
}
public function supports($filename)
{
return (bool) preg_match('#\.json(\.dist)?$#', $filename);
}
private function parseJson($filename)
{
$json = file_get_contents($filename);
return empty($json) ? array() : json_decode($json, true);
}
private function getJsonError($code)
{
$errorMessages = array(
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON',
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
JSON_ERROR_SYNTAX => 'Syntax error',
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded',
);
return isset($errorMessages[$code]) ? $errorMessages[$code] : 'Unknown';
}
}