Packages

  • Nette
    • Application
    • Caching
    • Collections
    • Config
    • Forms
    • IO
    • Loaders
    • Mail
    • Reflection
    • Security
    • Templates
    • Web
  • None
  • PHP

Classes

  • NConfig
  • NConfigAdapterIni

Interfaces

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