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