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