IniWriter.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. */
  8. namespace Piwik\Ini;
  9. /**
  10. * Writes INI configuration.
  11. */
  12. class IniWriter
  13. {
  14. /**
  15. * Writes an array configuration to a INI file.
  16. *
  17. * The array provided must be multidimensional, indexed by section names:
  18. *
  19. * ```
  20. * array(
  21. * 'Section 1' => array(
  22. * 'value1' => 'hello',
  23. * 'value2' => 'world',
  24. * ),
  25. * 'Section 2' => array(
  26. * 'value3' => 'foo',
  27. * )
  28. * );
  29. * ```
  30. *
  31. * @param string $filename
  32. * @param array $config
  33. * @param string $header Optional header to insert at the top of the file.
  34. * @throws IniWritingException
  35. */
  36. public function writeToFile($filename, array $config, $header = '')
  37. {
  38. $ini = $this->writeToString($config, $header);
  39. if (!file_put_contents($filename, $ini)) {
  40. throw new IniWritingException(sprintf('Impossible to write to file %s', $filename));
  41. }
  42. }
  43. /**
  44. * Writes an array configuration to a INI string and returns it.
  45. *
  46. * The array provided must be multidimensional, indexed by section names:
  47. *
  48. * ```
  49. * array(
  50. * 'Section 1' => array(
  51. * 'value1' => 'hello',
  52. * 'value2' => 'world',
  53. * ),
  54. * 'Section 2' => array(
  55. * 'value3' => 'foo',
  56. * )
  57. * );
  58. * ```
  59. *
  60. * @param array $config
  61. * @param string $header Optional header to insert at the top of the file.
  62. * @return string
  63. * @throws IniWritingException
  64. */
  65. public function writeToString(array $config, $header = '')
  66. {
  67. $ini = $header;
  68. $sectionNames = array_keys($config);
  69. foreach ($sectionNames as $sectionName) {
  70. $section = $config[$sectionName];
  71. // no point in writing empty sections
  72. if (empty($section)) {
  73. continue;
  74. }
  75. if (! is_array($section)) {
  76. throw new IniWritingException(sprintf("Section \"%s\" doesn't contain an array of values", $sectionName));
  77. }
  78. $ini .= "[$sectionName]\n";
  79. foreach ($section as $option => $value) {
  80. if (is_numeric($option)) {
  81. $option = $sectionName;
  82. $value = array($value);
  83. }
  84. if (is_array($value)) {
  85. foreach ($value as $currentValue) {
  86. $ini .= $option . '[] = ' . $this->encodeValue($currentValue) . "\n";
  87. }
  88. } else {
  89. $ini .= $option . ' = ' . $this->encodeValue($value) . "\n";
  90. }
  91. }
  92. $ini .= "\n";
  93. }
  94. return $ini;
  95. }
  96. private function encodeValue($value)
  97. {
  98. if (is_bool($value)) {
  99. return (int) $value;
  100. }
  101. if (is_string($value)) {
  102. return "\"$value\"";
  103. }
  104. return $value;
  105. }
  106. }