IniReader.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * IniReader
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, 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-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package cake.libs.config
  16. * @since CakePHP(tm) v 2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. /**
  20. * Ini file configuration parser. Since IniReader uses parse_ini_file underneath,
  21. * you should be aware that this class shares the same behavior, especially with
  22. * regards to boolean and null values.
  23. *
  24. * In addition to the native parse_ini_file features, IniReader also allows you
  25. * to create nested array structures through usage of . delimited names. This allows
  26. * you to create nested arrays structures in an ini config file. For example:
  27. *
  28. * `db.password = secret` would turn into `array('db' => array('password' => 'secret'))`
  29. *
  30. * You can nest properties as deeply as needed using .'s. IniReader also manipulates
  31. * how the special ini values of 'yes', 'no', 'on', 'off', 'null' are handled.
  32. * These values will be converted to their boolean equivalents.
  33. *
  34. * @package cake.config
  35. * @see http://php.net/parse_ini_file
  36. */
  37. class IniReader implements ConfigReaderInterface {
  38. /**
  39. * The path to read ini files from.
  40. *
  41. * @var array
  42. */
  43. protected $_path;
  44. /**
  45. * The section to read, if null all sections will be read.
  46. *
  47. * @var string
  48. */
  49. protected $_section;
  50. /**
  51. * Build and construct a new ini file parser. The parser can be used to read
  52. * ini files that are on the filesystem.
  53. *
  54. * @param string $path Path to load ini config files from.
  55. * @param string $section Only get one section.
  56. */
  57. public function __construct($path, $section = null) {
  58. $this->_path = $path;
  59. $this->_section = $section;
  60. }
  61. /**
  62. * Read an ini file and return the results as an array.
  63. *
  64. * @param string $file Name of the file to read.
  65. * @return array
  66. */
  67. public function read($file) {
  68. $filename = $this->_path . $file;
  69. if (!file_exists($filename)) {
  70. $filename .= '.ini';
  71. if (!file_exists($filename)) {
  72. throw new ConfigureException(__d('cake_dev', 'Could not load configuration files: %s or %s', substr($filename, 0, -4), $filename));
  73. }
  74. }
  75. $contents = parse_ini_file($filename, true);
  76. if (!empty($this->_section) && isset($contents[$this->_section])) {
  77. $values = $this->_parseNestedValues($contents[$this->_section]);
  78. } else {
  79. $values = array();
  80. foreach ($contents as $section => $attribs) {
  81. if (is_array($attribs)) {
  82. $values[$section] = $this->_parseNestedValues($attribs);
  83. } else {
  84. $parse = $this->_parseNestedValues(array($attribs));
  85. $values[$section] = array_shift($parse);
  86. }
  87. }
  88. }
  89. return $values;
  90. }
  91. /**
  92. * parses nested values out of keys.
  93. *
  94. * @param array $values Values to be exploded.
  95. * @return array Array of values exploded
  96. */
  97. protected function _parseNestedValues($values) {
  98. foreach ($values as $key => $value) {
  99. if ($value === '1') {
  100. $value = true;
  101. }
  102. if ($value === '') {
  103. $value = false;
  104. }
  105. if (strpos($key, '.') !== false) {
  106. $values = Set::insert($values, $key, $value);
  107. } else {
  108. $values[$key] = $value;
  109. }
  110. }
  111. return $values;
  112. }
  113. }