Source for file ConfigAdapterIni.php

Documentation is available at ConfigAdapterIni.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  7. 7:  * @license    http://nettephp.com/license  Nette license
  8. 8:  * @link       http://nettephp.com
  9. 9:  * @category   Nette
  10. 10:  * @package    Nette\Config
  11. 11:  */
  12. 12:  
  13. 13:  
  14. 14:  
  15. 15: /**
  16. 16:  * Reading and writing INI files.
  17. 17:  *
  18. 18:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  19. 19:  * @package    Nette\Config
  20. 20:  */
  21. 21: final class ConfigAdapterIni implements IConfigAdapter
  22. 22: {
  23. 23:  
  24. 24:     /** @var string  key nesting separator (key1> key2> key3) */
  25. 25:     public static $keySeparator '.';
  26. 26:  
  27. 27:     /** @var string  section inheriting separator (section < parent) */
  28. 28:     public static $sectionSeparator ' < ';
  29. 29:  
  30. 30:     /** @var string  raw section marker */
  31. 31:     public static $rawSection '!';
  32. 32:  
  33. 33:  
  34. 34:  
  35. 35:     /**
  36. 36:      * Static class - cannot be instantiated.
  37. 37:      */
  38. 38:     final public function __construct()
  39. 39:     {
  40. 40:         throw new LogicException("Cannot instantiate static class " get_class($this));
  41. 41:     }
  42. 42:  
  43. 43:  
  44. 44:  
  45. 45:     /**
  46. 46:      * Reads configuration from INI file.
  47. 47:      * @param  string  file name
  48. 48:      * @param  string  section to load
  49. 49:      * @return array 
  50. 50:      * @throws InvalidStateException
  51. 51:      */
  52. 52:     public static function load($file$section NULL)
  53. 53:     {
  54. 54:         if (!is_file($file|| !is_readable($file)) {
  55. 55:             throw new FileNotFoundException("File '$file' is missing or is not readable.");
  56. 56:         }
  57. 57:  
  58. 58:         Tools::tryError();
  59. 59:         $ini parse_ini_file($fileTRUE);
  60. 60:         if (Tools::catchError($msg)) {
  61. 61:             throw new Exception($msg);
  62. 62:         }
  63. 63:  
  64. 64:         $separator trim(self::$sectionSeparator);
  65. 65:         $data array();
  66. 66:         foreach ($ini as $secName => $secData{
  67. 67:             // is section?
  68. 68:             if (is_array($secData)) {
  69. 69:                 if (substr($secName-1=== self::$rawSection{
  70. 70:                     $secName substr($secName0-1);
  71. 71:  
  72. 72:                 elseif (self::$keySeparator{
  73. 73:                     // process key separators (key1> key2> key3)
  74. 74:                     $tmp array();
  75. 75:                     foreach ($secData as $key => $val{
  76. 76:                         $cursor $tmp;
  77. 77:                         foreach (explode(self::$keySeparator$keyas $part{
  78. 78:                             if (!isset($cursor[$part]|| is_array($cursor[$part])) {
  79. 79:                                 $cursor $cursor[$part];
  80. 80:                             else {
  81. 81:                                 throw new InvalidStateException("Invalid key '$key' in section [$secName] in '$file'.");
  82. 82:                             }
  83. 83:                         }
  84. 84:                         $cursor $val;
  85. 85:                     }
  86. 86:                     $secData $tmp;
  87. 87:                 }
  88. 88:  
  89. 89:                 // process extends sections like [staging < production] (with special support for separator ':')
  90. 90:                 $parts $separator explode($separatorstrtr($secName':'$separator)) array($secName);
  91. 91:                 if (count($parts1{
  92. 92:                     $parent trim($parts[1]);
  93. 93:                     $cursor $data;
  94. 94:                     foreach (self::$keySeparator explode(self::$keySeparator$parentarray($parentas $part{
  95. 95:                         if (isset($cursor[$part]&& is_array($cursor[$part])) {
  96. 96:                             $cursor $cursor[$part];
  97. 97:                         else {
  98. 98:                             throw new InvalidStateException("Missing parent section [$parent] in '$file'.");
  99. 99:                         }
  100. 100:                     }
  101. 101:                     $secData ArrayTools::mergeTree($secData$cursor);
  102. 102:                 }
  103. 103:  
  104. 104:                 $secName trim($parts[0]);
  105. 105:                 if ($secName === ''{
  106. 106:                     throw new InvalidStateException("Invalid empty section name in '$file'.");
  107. 107:                 }
  108. 108:             }
  109. 109:  
  110. 110:             if (self::$keySeparator{
  111. 111:                 $cursor $data;
  112. 112:                 foreach (explode(self::$keySeparator$secNameas $part{
  113. 113:                     if (!isset($cursor[$part]|| is_array($cursor[$part])) {
  114. 114:                         $cursor $cursor[$part];
  115. 115:                     else {
  116. 116:                         throw new InvalidStateException("Invalid section [$secName] in '$file'.");
  117. 117:                     }
  118. 118:                 }
  119. 119:             else {
  120. 120:                 $cursor $data[$secName];
  121. 121:             }
  122. 122:  
  123. 123:             if (is_array($secData&& is_array($cursor)) {
  124. 124:                 $secData ArrayTools::mergeTree($secData$cursor);
  125. 125:             }
  126. 126:  
  127. 127:             $cursor $secData;
  128. 128:         }
  129. 129:  
  130. 130:         if ($section === NULL{
  131. 131:             return $data;
  132. 132:  
  133. 133:         elseif (!isset($data[$section]|| !is_array($data[$section])) {
  134. 134:             throw new InvalidStateException("There is not section [$section] in '$file'.");
  135. 135:  
  136. 136:         else {
  137. 137:             return $data[$section];
  138. 138:         }
  139. 139:     }
  140. 140:  
  141. 141:  
  142. 142:  
  143. 143:     /**
  144. 144:      * Write INI file.
  145. 145:      * @param  Config to save
  146. 146:      * @param  string  file
  147. 147:      * @param  string  section name
  148. 148:      * @return void 
  149. 149:      */
  150. 150:     public static function save($config$file$section NULL)
  151. 151:     {
  152. 152:         $output array();
  153. 153:         $output['; generated by Nette';// at ' . @strftime('%c');
  154. 154:         $output['';
  155. 155:  
  156. 156:         if ($section === NULL{
  157. 157:             foreach ($config as $secName => $secData{
  158. 158:                 if (!(is_array($secData|| $secData instanceof Traversable)) {
  159. 159:                     throw new InvalidStateException("Invalid section '$section'.");
  160. 160:                 }
  161. 161:  
  162. 162:                 $output["[$secName]";
  163. 163:                 self::build($secData$output'');
  164. 164:                 $output['';
  165. 165:             }
  166. 166:  
  167. 167:         else {
  168. 168:             $output["[$section]";
  169. 169:             self::build($config$output'');
  170. 170:             $output['';
  171. 171:         }
  172. 172:  
  173. 173:         if (!file_put_contents($fileimplode(PHP_EOL$output))) {
  174. 174:             throw new IOException("Cannot write file '$file'.");
  175. 175:         }
  176. 176:     }
  177. 177:  
  178. 178:  
  179. 179:  
  180. 180:     /**
  181. 181:      * Recursive builds INI list.
  182. 182:      * @param  array|\Traversable
  183. 183:      * @param  array 
  184. 184:      * @param  string 
  185. 185:      * @return void 
  186. 186:      */
  187. 187:     private static function build($input$output$prefix)
  188. 188:     {
  189. 189:         foreach ($input as $key => $val{
  190. 190:             if (is_array($val|| $val instanceof Traversable{
  191. 191:                 self::build($val$output$prefix $key self::$keySeparator);
  192. 192:  
  193. 193:             elseif (is_bool($val)) {
  194. 194:                 $output["$prefix$key = ($val 'true' 'false');
  195. 195:  
  196. 196:             elseif (is_numeric($val)) {
  197. 197:                 $output["$prefix$key = $val";
  198. 198:  
  199. 199:             elseif (is_string($val)) {
  200. 200:                 $output["$prefix$key = \"$val\"";
  201. 201:  
  202. 202:             else {
  203. 203:                 throw new InvalidArgumentException("The '$prefix$key' item must be scalar or array, gettype($val." given.");
  204. 204:             }
  205. 205:         }
  206. 206:     }
  207. 207: