IniReader.php 6.1 KB

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