Ini.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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-2014 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(
  68. sprintf('Error reading INI file "%s": %s', $filename, $message),
  69. $error
  70. );
  71. },
  72. E_WARNING
  73. );
  74. $ini = parse_ini_file($filename, true);
  75. restore_error_handler();
  76. return $this->process($ini);
  77. }
  78. /**
  79. * fromString(): defined by Reader interface.
  80. *
  81. * @param string $string
  82. * @return array|bool
  83. * @throws Exception\RuntimeException
  84. */
  85. public function fromString($string)
  86. {
  87. if (empty($string)) {
  88. return array();
  89. }
  90. $this->directory = null;
  91. set_error_handler(
  92. function ($error, $message = '', $file = '', $line = 0) {
  93. throw new Exception\RuntimeException(
  94. sprintf('Error reading INI string: %s', $message),
  95. $error
  96. );
  97. },
  98. E_WARNING
  99. );
  100. $ini = parse_ini_string($string, true);
  101. restore_error_handler();
  102. return $this->process($ini);
  103. }
  104. /**
  105. * Process data from the parsed ini file.
  106. *
  107. * @param array $data
  108. * @return array
  109. */
  110. protected function process(array $data)
  111. {
  112. $config = array();
  113. foreach ($data as $section => $value) {
  114. if (is_array($value)) {
  115. if (strpos($section, $this->nestSeparator) !== false) {
  116. $sections = explode($this->nestSeparator, $section);
  117. $config = array_merge_recursive($config, $this->buildNestedSection($sections, $value));
  118. } else {
  119. $config[$section] = $this->processSection($value);
  120. }
  121. } else {
  122. $this->processKey($section, $value, $config);
  123. }
  124. }
  125. return $config;
  126. }
  127. /**
  128. * Process a nested section
  129. *
  130. * @param array $sections
  131. * @param mixed $value
  132. * @return array
  133. */
  134. private function buildNestedSection($sections, $value)
  135. {
  136. if (count($sections) == 0) {
  137. return $this->processSection($value);
  138. }
  139. $nestedSection = array();
  140. $first = array_shift($sections);
  141. $nestedSection[$first] = $this->buildNestedSection($sections, $value);
  142. return $nestedSection;
  143. }
  144. /**
  145. * Process a section.
  146. *
  147. * @param array $section
  148. * @return array
  149. */
  150. protected function processSection(array $section)
  151. {
  152. $config = array();
  153. foreach ($section as $key => $value) {
  154. $this->processKey($key, $value, $config);
  155. }
  156. return $config;
  157. }
  158. /**
  159. * Process a key.
  160. *
  161. * @param string $key
  162. * @param string $value
  163. * @param array $config
  164. * @return array
  165. * @throws Exception\RuntimeException
  166. */
  167. protected function processKey($key, $value, array &$config)
  168. {
  169. if (strpos($key, $this->nestSeparator) !== false) {
  170. $pieces = explode($this->nestSeparator, $key, 2);
  171. if (!strlen($pieces[0]) || !strlen($pieces[1])) {
  172. throw new Exception\RuntimeException(sprintf('Invalid key "%s"', $key));
  173. } elseif (!isset($config[$pieces[0]])) {
  174. if ($pieces[0] === '0' && !empty($config)) {
  175. $config = array($pieces[0] => $config);
  176. } else {
  177. $config[$pieces[0]] = array();
  178. }
  179. } elseif (!is_array($config[$pieces[0]])) {
  180. throw new Exception\RuntimeException(
  181. sprintf('Cannot create sub-key for "%s", as key already exists', $pieces[0])
  182. );
  183. }
  184. $this->processKey($pieces[1], $value, $config[$pieces[0]]);
  185. } else {
  186. if ($key === '@include') {
  187. if ($this->directory === null) {
  188. throw new Exception\RuntimeException('Cannot process @include statement for a string config');
  189. }
  190. $reader = clone $this;
  191. $include = $reader->fromFile($this->directory . '/' . $value);
  192. $config = array_replace_recursive($config, $include);
  193. } else {
  194. $config[$key] = $value;
  195. }
  196. }
  197. }
  198. }