1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4: * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
11: * @link http://cakephp.org CakePHP(tm) Project
12: * @since 2.0.0
13: * @license http://www.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\Utility\Hash;
20:
21: /**
22: * Ini file configuration engine.
23: *
24: * Since IniConfig uses parse_ini_file underneath, you should be aware that this
25: * class shares the same behavior, especially with regards to boolean and null values.
26: *
27: * In addition to the native `parse_ini_file` features, IniConfig also allows you
28: * to create nested array structures through usage of `.` delimited names. This allows
29: * you to create nested arrays structures in an ini config file. For example:
30: *
31: * `db.password = secret` would turn into `['db' => ['password' => 'secret']]`
32: *
33: * You can nest properties as deeply as needed using `.`'s. In addition to using `.` you
34: * can use standard ini section notation to create nested structures:
35: *
36: * ```
37: * [section]
38: * key = value
39: * ```
40: *
41: * Once loaded into Configure, the above would be accessed using:
42: *
43: * `Configure::read('section.key');
44: *
45: * You can combine `.` separated values with sections to create more deeply
46: * nested structures.
47: *
48: * IniConfig also manipulates how the special ini values of
49: * 'yes', 'no', 'on', 'off', 'null' are handled. These values will be
50: * converted to their boolean equivalents.
51: *
52: * @see http://php.net/parse_ini_file
53: */
54: class IniConfig implements ConfigEngineInterface
55: {
56:
57: use FileConfigTrait;
58:
59: /**
60: * File extension.
61: *
62: * @var string
63: */
64: protected $_extension = '.ini';
65:
66: /**
67: * The section to read, if null all sections will be read.
68: *
69: * @var string
70: */
71: protected $_section;
72:
73: /**
74: * Build and construct a new ini file parser. The parser can be used to read
75: * ini files that are on the filesystem.
76: *
77: * @param string|null $path Path to load ini config files from. Defaults to CONFIG.
78: * @param string|null $section Only get one section, leave null to parse and fetch
79: * all sections in the ini file.
80: */
81: public function __construct($path = null, $section = null)
82: {
83: if ($path === null) {
84: $path = CONFIG;
85: }
86: $this->_path = $path;
87: $this->_section = $section;
88: }
89:
90: /**
91: * Read an ini file and return the results as an array.
92: *
93: * @param string $key The identifier to read from. If the key has a . it will be treated
94: * as a plugin prefix. The chosen file must be on the engine's path.
95: * @return array Parsed configuration values.
96: * @throws \Cake\Core\Exception\Exception when files don't exist.
97: * Or when files contain '..' as this could lead to abusive reads.
98: */
99: public function read($key)
100: {
101: $file = $this->_getFilePath($key, true);
102:
103: $contents = parse_ini_file($file, true);
104: if (!empty($this->_section) && isset($contents[$this->_section])) {
105: $values = $this->_parseNestedValues($contents[$this->_section]);
106: } else {
107: $values = [];
108: foreach ($contents as $section => $attribs) {
109: if (is_array($attribs)) {
110: $values[$section] = $this->_parseNestedValues($attribs);
111: } else {
112: $parse = $this->_parseNestedValues([$attribs]);
113: $values[$section] = array_shift($parse);
114: }
115: }
116: }
117: return $values;
118: }
119:
120: /**
121: * parses nested values out of keys.
122: *
123: * @param array $values Values to be exploded.
124: * @return array Array of values exploded
125: */
126: protected function _parseNestedValues($values)
127: {
128: foreach ($values as $key => $value) {
129: if ($value === '1') {
130: $value = true;
131: }
132: if ($value === '') {
133: $value = false;
134: }
135: unset($values[$key]);
136: if (strpos($key, '.') !== false) {
137: $values = Hash::insert($values, $key, $value);
138: } else {
139: $values[$key] = $value;
140: }
141: }
142: return $values;
143: }
144:
145: /**
146: * Dumps the state of Configure data into an ini formatted string.
147: *
148: * @param string $key The identifier to write to. If the key has a . it will be treated
149: * as a plugin prefix.
150: * @param array $data The data to convert to ini file.
151: * @return bool Success.
152: */
153: public function dump($key, array $data)
154: {
155: $result = [];
156: foreach ($data as $k => $value) {
157: $isSection = false;
158: if ($k[0] !== '[') {
159: $result[] = "[$k]";
160: $isSection = true;
161: }
162: if (is_array($value)) {
163: $kValues = Hash::flatten($value, '.');
164: foreach ($kValues as $k2 => $v) {
165: $result[] = "$k2 = " . $this->_value($v);
166: }
167: }
168: if ($isSection) {
169: $result[] = '';
170: }
171: }
172: $contents = trim(implode("\n", $result));
173:
174: $filename = $this->_getFilePath($key);
175: return file_put_contents($filename, $contents) > 0;
176: }
177:
178: /**
179: * Converts a value into the ini equivalent
180: *
181: * @param mixed $value Value to export.
182: * @return string String value for ini file.
183: */
184: protected function _value($value)
185: {
186: if ($value === null) {
187: return 'null';
188: }
189: if ($value === true) {
190: return 'true';
191: }
192: if ($value === false) {
193: return 'false';
194: }
195: return (string)$value;
196: }
197: }
198: