Ini.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Config\Reader;
  10. use Zend\Config\Exception;
  11. /**
  12. * INI config reader.
  13. */
  14. class Ini implements ReaderInterface
  15. {
  16. /**
  17. * Separator for nesting levels of configuration data identifiers.
  18. *
  19. * @var string
  20. */
  21. protected $nestSeparator = '.';
  22. /**
  23. * Directory of the file to process.
  24. *
  25. * @var string
  26. */
  27. protected $directory;
  28. /**
  29. * Set nest separator.
  30. *
  31. * @param string $separator
  32. * @return self
  33. */
  34. public function setNestSeparator($separator)
  35. {
  36. $this->nestSeparator = $separator;
  37. return $this;
  38. }
  39. /**
  40. * Get nest separator.
  41. *
  42. * @return string
  43. */
  44. public function getNestSeparator()
  45. {
  46. return $this->nestSeparator;
  47. }
  48. /**
  49. * fromFile(): defined by Reader interface.
  50. *
  51. * @see ReaderInterface::fromFile()
  52. * @param string $filename
  53. * @return array
  54. * @throws Exception\RuntimeException
  55. */
  56. public function fromFile($filename)
  57. {
  58. if (!is_file($filename) || !is_readable($filename)) {
  59. throw new Exception\RuntimeException(sprintf(
  60. "File '%s' doesn't exist or not readable",
  61. $filename
  62. ));
  63. }
  64. $this->directory = dirname($filename);
  65. set_error_handler(
  66. function ($error, $message = '', $file = '', $line = 0) use ($filename) {
  67. throw new Exception\RuntimeException(sprintf(
  68. 'Error reading INI file "%s": %s',
  69. $filename, $message
  70. ), $error);
  71. }, E_WARNING
  72. );
  73. $ini = parse_ini_file($filename, true);
  74. restore_error_handler();
  75. return $this->process($ini);
  76. }
  77. /**
  78. * fromString(): defined by Reader interface.
  79. *
  80. * @param string $string
  81. * @return array|bool
  82. * @throws Exception\RuntimeException
  83. */
  84. public function fromString($string)
  85. {
  86. if (empty($string)) {
  87. return array();
  88. }
  89. $this->directory = null;
  90. set_error_handler(
  91. function ($error, $message = '', $file = '', $line = 0) {
  92. throw new Exception\RuntimeException(sprintf(
  93. 'Error reading INI string: %s',
  94. $message
  95. ), $error);
  96. }, E_WARNING
  97. );
  98. $ini = parse_ini_string($string, true);
  99. restore_error_handler();
  100. return $this->process($ini);
  101. }
  102. /**
  103. * Process data from the parsed ini file.
  104. *
  105. * @param array $data
  106. * @return array
  107. */
  108. protected function process(array $data)
  109. {
  110. $config = array();
  111. foreach ($data as $section => $value) {
  112. if (is_array($value)) {
  113. if (strpos($section, $this->nestSeparator) !== false) {
  114. $sections = explode($this->nestSeparator, $section);
  115. $config = array_merge_recursive($config, $this->buildNestedSection($sections, $value));
  116. } else {
  117. $config[$section] = $this->processSection($value);
  118. }
  119. } else {
  120. $this->processKey($section, $value, $config);
  121. }
  122. }
  123. return $config;
  124. }
  125. /**
  126. * Process a nested section
  127. *
  128. * @param array $sections
  129. * @param mixed $value
  130. * @return array
  131. */
  132. private function buildNestedSection($sections, $value)
  133. {
  134. if (count($sections) == 0) {
  135. return $this->processSection($value);
  136. }
  137. $nestedSection = array();
  138. $first = array_shift($sections);
  139. $nestedSection[$first] = $this->buildNestedSection($sections, $value);
  140. return $nestedSection;
  141. }
  142. /**
  143. * Process a section.
  144. *
  145. * @param array $section
  146. * @return array
  147. */
  148. protected function processSection(array $section)
  149. {
  150. $config = array();
  151. foreach ($section as $key => $value) {
  152. $this->processKey($key, $value, $config);
  153. }
  154. return $config;
  155. }
  156. /**
  157. * Process a key.
  158. *
  159. * @param string $key
  160. * @param string $value
  161. * @param array $config
  162. * @return array
  163. * @throws Exception\RuntimeException
  164. */
  165. protected function processKey($key, $value, array &$config)
  166. {
  167. if (strpos($key, $this->nestSeparator) !== false) {
  168. $pieces = explode($this->nestSeparator, $key, 2);
  169. if (!strlen($pieces[0]) || !strlen($pieces[1])) {
  170. throw new Exception\RuntimeException(sprintf('Invalid key "%s"', $key));
  171. } elseif (!isset($config[$pieces[0]])) {
  172. if ($pieces[0] === '0' && !empty($config)) {
  173. $config = array($pieces[0] => $config);
  174. } else {
  175. $config[$pieces[0]] = array();
  176. }
  177. } elseif (!is_array($config[$pieces[0]])) {
  178. throw new Exception\RuntimeException(sprintf(
  179. 'Cannot create sub-key for "%s", as key already exists', $pieces[0]
  180. ));
  181. }
  182. $this->processKey($pieces[1], $value, $config[$pieces[0]]);
  183. } else {
  184. if ($key === '@include') {
  185. if ($this->directory === null) {
  186. throw new Exception\RuntimeException('Cannot process @include statement for a string config');
  187. }
  188. $reader = clone $this;
  189. $include = $reader->fromFile($this->directory . '/' . $value);
  190. $config = array_replace_recursive($config, $include);
  191. } else {
  192. $config[$key] = $value;
  193. }
  194. }
  195. }
  196. }