1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4: * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
5: *
6: * Licensed under The MIT License
7: * For full copyright and license information, please see the LICENSE.txt
8: * Redistributions of files must retain the above copyright notice.
9: *
10: * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11: * @link https://cakephp.org CakePHP(tm) Project
12: * @since 3.0.0
13: * @license https://opensource.org/licenses/mit-license.php MIT License
14: */
15: namespace Cake\Core\Configure\Engine;
16:
17: use Cake\Core\Configure\ConfigEngineInterface;
18: use Cake\Core\Configure\FileConfigTrait;
19: use Cake\Core\Exception\Exception;
20:
21: /**
22: * JSON engine allows Configure to load configuration values from
23: * files containing JSON strings.
24: *
25: * An example JSON file would look like::
26: *
27: * ```
28: * {
29: * "debug": false,
30: * "App": {
31: * "namespace": "MyApp"
32: * },
33: * "Security": {
34: * "salt": "its-secret"
35: * }
36: * }
37: * ```
38: */
39: class JsonConfig implements ConfigEngineInterface
40: {
41:
42: use FileConfigTrait;
43:
44: /**
45: * File extension.
46: *
47: * @var string
48: */
49: protected $_extension = '.json';
50:
51: /**
52: * Constructor for JSON Config file reading.
53: *
54: * @param string|null $path The path to read config files from. Defaults to CONFIG.
55: */
56: public function __construct($path = null)
57: {
58: if ($path === null) {
59: $path = CONFIG;
60: }
61: $this->_path = $path;
62: }
63:
64: /**
65: * Read a config file and return its contents.
66: *
67: * Files with `.` in the name will be treated as values in plugins. Instead of
68: * reading from the initialized path, plugin keys will be located using Plugin::path().
69: *
70: * @param string $key The identifier to read from. If the key has a . it will be treated
71: * as a plugin prefix.
72: * @return array Parsed configuration values.
73: * @throws \Cake\Core\Exception\Exception When files don't exist or when
74: * files contain '..' (as this could lead to abusive reads) or when there
75: * is an error parsing the JSON string.
76: */
77: public function read($key)
78: {
79: $file = $this->_getFilePath($key, true);
80:
81: $values = json_decode(file_get_contents($file), true);
82: if (json_last_error() !== JSON_ERROR_NONE) {
83: throw new Exception(sprintf(
84: 'Error parsing JSON string fetched from config file "%s.json": %s',
85: $key,
86: json_last_error_msg()
87: ));
88: }
89: if (!is_array($values)) {
90: throw new Exception(sprintf(
91: 'Decoding JSON config file "%s.json" did not return an array',
92: $key
93: ));
94: }
95:
96: return $values;
97: }
98:
99: /**
100: * Converts the provided $data into a JSON string that can be used saved
101: * into a file and loaded later.
102: *
103: * @param string $key The identifier to write to. If the key has a . it will
104: * be treated as a plugin prefix.
105: * @param array $data Data to dump.
106: * @return bool Success
107: */
108: public function dump($key, array $data)
109: {
110: $filename = $this->_getFilePath($key);
111:
112: return file_put_contents($filename, json_encode($data, JSON_PRETTY_PRINT)) > 0;
113: }
114: }
115: