PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Configure/IniReader.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 137 lines | 49 code | 6 blank | 82 comment | 11 complexity | 290544a32233d7e9bfb39a3a171ac3e9 MD5 | raw file
  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.Configure
  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. In addition to using `.` you
  31. * can use standard ini section notation to create nested structures:
  32. *
  33. * {{{
  34. * [section]
  35. * key = value
  36. * }}}
  37. *
  38. * Once loaded into Configure, the above would be accessed using:
  39. *
  40. * `Configure::read('section.key');
  41. *
  42. * You can combine `.` separated values with sections to create more deeply
  43. * nested structures.
  44. *
  45. * IniReader also manipulates how the special ini values of
  46. * 'yes', 'no', 'on', 'off', 'null' are handled. These values will be
  47. * converted to their boolean equivalents.
  48. *
  49. * @package Cake.Configure
  50. * @see http://php.net/parse_ini_file
  51. */
  52. class IniReader implements ConfigReaderInterface {
  53. /**
  54. * The path to read ini files from.
  55. *
  56. * @var array
  57. */
  58. protected $_path;
  59. /**
  60. * The section to read, if null all sections will be read.
  61. *
  62. * @var string
  63. */
  64. protected $_section;
  65. /**
  66. * Build and construct a new ini file parser. The parser can be used to read
  67. * ini files that are on the filesystem.
  68. *
  69. * @param string $path Path to load ini config files from.
  70. * @param string $section Only get one section, leave null to parse and fetch
  71. * all sections in the ini file.
  72. */
  73. public function __construct($path, $section = null) {
  74. $this->_path = $path;
  75. $this->_section = $section;
  76. }
  77. /**
  78. * Read an ini file and return the results as an array.
  79. *
  80. * @param string $file Name of the file to read. The chosen file
  81. * must be on the reader's path.
  82. * @return array
  83. * @throws ConfigureException
  84. */
  85. public function read($file) {
  86. $filename = $this->_path . $file;
  87. if (!file_exists($filename)) {
  88. $filename .= '.ini';
  89. if (!file_exists($filename)) {
  90. throw new ConfigureException(__d('cake_dev', 'Could not load configuration files: %s or %s', substr($filename, 0, -4), $filename));
  91. }
  92. }
  93. $contents = parse_ini_file($filename, true);
  94. if (!empty($this->_section) && isset($contents[$this->_section])) {
  95. $values = $this->_parseNestedValues($contents[$this->_section]);
  96. } else {
  97. $values = array();
  98. foreach ($contents as $section => $attribs) {
  99. if (is_array($attribs)) {
  100. $values[$section] = $this->_parseNestedValues($attribs);
  101. } else {
  102. $parse = $this->_parseNestedValues(array($attribs));
  103. $values[$section] = array_shift($parse);
  104. }
  105. }
  106. }
  107. return $values;
  108. }
  109. /**
  110. * parses nested values out of keys.
  111. *
  112. * @param array $values Values to be exploded.
  113. * @return array Array of values exploded
  114. */
  115. protected function _parseNestedValues($values) {
  116. foreach ($values as $key => $value) {
  117. if ($value === '1') {
  118. $value = true;
  119. }
  120. if ($value === '') {
  121. $value = false;
  122. }
  123. if (strpos($key, '.') !== false) {
  124. $values = Set::insert($values, $key, $value);
  125. } else {
  126. $values[$key] = $value;
  127. }
  128. }
  129. return $values;
  130. }
  131. }