/vendor/phing/phing/classes/phing/tasks/ext/inifile/IniFileConfig.php

https://gitlab.com/Isaki/le331.fr · PHP · 187 lines · 101 code · 10 blank · 76 comment · 28 complexity · 77a6f7134c44fab56a223cf440c7d0ab MD5 · raw file

  1. <?php
  2. /**
  3. * INI file modification task for Phing, the PHP build tool.
  4. *
  5. * Based on http://ant-contrib.sourceforge.net/tasks/tasks/inifile.html
  6. *
  7. * PHP version 5
  8. *
  9. * @category Tasks
  10. * @package phing.tasks.ext
  11. * @author Ken Guest <kguest@php.net>
  12. * @license LGPL v3 or later http://www.gnu.org/licenses/lgpl.html
  13. * @link http://www.phing.info/
  14. */
  15. /**
  16. * Class for reading/writing ini config file
  17. *
  18. * This preserves comments etc, unlike parse_ini_file and is based heavily on
  19. * a solution provided at:
  20. * stackoverflow.com/questions/9594238/good-php-classes-that-manipulate-ini-files
  21. *
  22. * @category Tasks
  23. * @package phing.tasks.ext
  24. * @author Ken Guest <kguest@php.net>
  25. * @license LGPL v3 or later http://www.gnu.org/licenses/lgpl.html
  26. * @link http://www.phing.info/
  27. */
  28. class IniFileConfig
  29. {
  30. /**
  31. * Lines of ini file
  32. *
  33. * @var array
  34. */
  35. protected $lines = array();
  36. /**
  37. * Read ini file
  38. *
  39. * @param string $file filename
  40. *
  41. * @return void
  42. */
  43. public function read($file)
  44. {
  45. $this->lines = array();
  46. $section = '';
  47. foreach (file($file) as $line) {
  48. if (preg_match('/^\s*(;.*)?$/', $line)) {
  49. // comment or whitespace
  50. $this->lines[] = array(
  51. 'type' => 'comment',
  52. 'data' => $line,
  53. 'section' => $section
  54. );
  55. } elseif (preg_match('/^\s?\[(.*)\]/', $line, $match)) {
  56. // section
  57. $section = $match[1];
  58. $this->lines[] = array(
  59. 'type' => 'section',
  60. 'data' => $line,
  61. 'section' => $section
  62. );
  63. } elseif (preg_match('/^\s*(.*?)\s*=\s*(.*?)\s*$/', $line, $match)) {
  64. // entry
  65. $this->lines[] = array(
  66. 'type' => 'entry',
  67. 'data' => $line,
  68. 'section' => $section,
  69. 'key' => $match[1],
  70. 'value' => $match[2]
  71. );
  72. }
  73. }
  74. }
  75. /**
  76. * Get value of given key in specified section
  77. *
  78. * @param string $section Section
  79. * @param string $key Key
  80. *
  81. * @return void
  82. */
  83. public function get($section, $key)
  84. {
  85. foreach ($this->lines as $line) {
  86. if ($line['type'] != 'entry') {
  87. continue;
  88. }
  89. if ($line['section'] != $section) {
  90. continue;
  91. }
  92. if ($line['key'] != $key) {
  93. continue;
  94. }
  95. return $line['value'];
  96. }
  97. throw new RuntimeException('Missing Section or Key');
  98. }
  99. /**
  100. * Set key to value in specified section
  101. *
  102. * @param string $section Section
  103. * @param string $key Key
  104. * @param string $value Value
  105. *
  106. * @return void
  107. */
  108. public function set($section, $key, $value)
  109. {
  110. foreach ($this->lines as &$line) {
  111. if ($line['type'] != 'entry') {
  112. continue;
  113. }
  114. if ($line['section'] != $section) {
  115. continue;
  116. }
  117. if ($line['key'] != $key) {
  118. continue;
  119. }
  120. $line['value'] = $value;
  121. $line['data'] = $key . " = " . $value . PHP_EOL;
  122. return;
  123. }
  124. throw new RuntimeException('Missing Section or Key');
  125. }
  126. /**
  127. * Remove key/section from file.
  128. *
  129. * If key is not specified, then the entire section will be removed.
  130. *
  131. * @param string $section Section to manipulate/remove
  132. * @param string $key Name of key to remove, might be null/empty
  133. *
  134. * @return void
  135. */
  136. public function remove($section, $key)
  137. {
  138. if ($section == '') {
  139. throw new RuntimeException("Section not set.");
  140. }
  141. if (is_null($key) || ($key == '')) {
  142. // remove entire section
  143. foreach ($this->lines as $linenum => $line) {
  144. if ($line['section'] == $section) {
  145. unset($this->lines[$linenum]);
  146. }
  147. }
  148. } else {
  149. foreach ($this->lines as $linenum => $line) {
  150. if (($line['section'] == $section)
  151. && (isset($line['key']))
  152. && ($line['key'] == $key)
  153. ) {
  154. unset($this->lines[$linenum]);
  155. }
  156. }
  157. }
  158. }
  159. /**
  160. * Write contents out to file
  161. *
  162. * @param string $file filename
  163. *
  164. * @return void
  165. */
  166. public function write($file)
  167. {
  168. if (file_exists($file) && !is_writable($file)) {
  169. throw new RuntimeException("$file is not writable");
  170. }
  171. $fp = fopen($file, 'w');
  172. foreach ($this->lines as $line) {
  173. fwrite($fp, $line['data']);
  174. }
  175. fclose($fp);
  176. }
  177. }