Packages

  • Nette
    • Application
      • Application\Diagnostics
      • Application\Responses
      • Application\Routers
      • Application\UI
    • Caching
      • Caching\Storages
    • ComponentModel
    • Config
    • Database
      • Database\Diagnostics
      • Database\Drivers
      • Database\Reflection
      • Database\Table
    • DI
    • Diagnostics
    • Forms
      • Forms\Controls
      • Forms\Rendering
    • Http
    • Iterators
    • Latte
      • Latte\Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • None
  • PHP

Classes

  • NConfig
  • NIniAdapter
  • NNeonAdapter

Interfaces

  • IConfigAdapter
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  *
  6:  * Copyright (c) 2004, 2011 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 NIniAdapter 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 NStaticClassException;
 41:     }
 42: 
 43: 
 44: 
 45:     /**
 46:      * Reads configuration from INI file.
 47:      * @param  string  file name
 48:      * @return array
 49:      * @throws InvalidStateException
 50:      */
 51:     public static function load($file)
 52:     {
 53:         if (!is_file($file) || !is_readable($file)) {
 54:             throw new FileNotFoundException("File '$file' is missing or is not readable.");
 55:         }
 56: 
 57:         NDebugger::tryError();
 58:         $ini = parse_ini_file($file, TRUE);
 59:         if (NDebugger::catchError($e)) {
 60:             throw new InvalidStateException('parse_ini_file(): ' . $e->getMessage(), 0, $e);
 61:         }
 62: 
 63:         $separator = trim(self::$sectionSeparator);
 64:         $data = array();
 65:         foreach ($ini as $secName => $secData) {
 66:             // is section?
 67:             if (is_array($secData)) {
 68:                 if (substr($secName, -1) === self::$rawSection) {
 69:                     $secName = substr($secName, 0, -1);
 70: 
 71:                 } elseif (self::$keySeparator) {
 72:                     // process key separators (key1> key2> key3)
 73:                     $tmp = array();
 74:                     foreach ($secData as $key => $val) {
 75:                         $cursor = & $tmp;
 76:                         foreach (explode(self::$keySeparator, $key) as $part) {
 77:                             if (!isset($cursor[$part]) || is_array($cursor[$part])) {
 78:                                 $cursor = & $cursor[$part];
 79:                             } else {
 80:                                 throw new InvalidStateException("Invalid key '$key' in section [$secName] in file '$file'.");
 81:                             }
 82:                         }
 83:                         $cursor = $val;
 84:                     }
 85:                     $secData = $tmp;
 86:                 }
 87: 
 88:                 // process extends sections like [staging < production] (with special support for separator ':')
 89:                 $parts = $separator ? explode($separator, strtr($secName, ':', $separator)) : array($secName);
 90:                 if (count($parts) > 1) {
 91:                     $parent = trim($parts[1]);
 92:                     if (!isset($data[$parent]) || !is_array($data[$parent])) {
 93:                         throw new InvalidStateException("Missing parent section [$parent] in file '$file'.");
 94:                     }
 95:                     $secData = array_reverse(NArrays::mergeTree(array_reverse($secData, TRUE), array_reverse($data[$parent], TRUE)), TRUE);
 96:                     $secName = trim($parts[0]);
 97:                     if ($secName === '') {
 98:                         throw new InvalidStateException("Invalid empty section name in file '$file'.");
 99:                     }
100:                 }
101:             }
102: 
103:             if (self::$keySeparator) {
104:                 $cursor = & $data;
105:                 foreach (explode(self::$keySeparator, $secName) as $part) {
106:                     if (!isset($cursor[$part]) || is_array($cursor[$part])) {
107:                         $cursor = & $cursor[$part];
108:                     } else {
109:                         throw new InvalidStateException("Invalid section [$secName] in file '$file'.");
110:                     }
111:                 }
112:             } else {
113:                 $cursor = & $data[$secName];
114:             }
115: 
116:             if (is_array($secData) && is_array($cursor)) {
117:                 $secData = NArrays::mergeTree($secData, $cursor);
118:             }
119: 
120:             $cursor = $secData;
121:         }
122: 
123:         return $data;
124:     }
125: 
126: 
127: 
128:     /**
129:      * Write INI file.
130:      * @param  mixed
131:      * @param  string  file
132:      * @return void
133:      */
134:     public static function save($config, $file)
135:     {
136:         $output = array();
137:         $output[] = '; generated by Nette';// at ' . @strftime('%c');
138:         $output[] = '';
139: 
140:         foreach ($config as $secName => $secData) {
141:             if (!(is_array($secData) || $secData instanceof Traversable)) {
142:                 throw new InvalidStateException("Invalid section '$secName'.");
143:             }
144: 
145:             $output[] = "[$secName]";
146:             self::build($secData, $output, '');
147:             $output[] = '';
148:         }
149: 
150:         if (!file_put_contents($file, implode(PHP_EOL, $output))) {
151:             throw new IOException("Cannot write file '$file'.");
152:         }
153:     }
154: 
155: 
156: 
157:     /**
158:      * Recursive builds INI list.
159:      * @param  array|Traversable
160:      * @param  array
161:      * @param  string
162:      * @return void
163:      */
164:     private static function build($input, & $output, $prefix)
165:     {
166:         foreach ($input as $key => $val) {
167:             if (is_array($val) || $val instanceof Traversable) {
168:                 self::build($val, $output, $prefix . $key . self::$keySeparator);
169: 
170:             } elseif (is_bool($val)) {
171:                 $output[] = "$prefix$key = " . ($val ? 'true' : 'false');
172: 
173:             } elseif (is_numeric($val)) {
174:                 $output[] = "$prefix$key = $val";
175: 
176:             } elseif (is_string($val)) {
177:                 $output[] = "$prefix$key = \"$val\"";
178: 
179:             } else {
180:                 throw new InvalidArgumentException("The '$prefix$key' item must be scalar or array, " . gettype($val) ." given.");
181:             }
182:         }
183:     }
184: 
185: }
186: 
Nette Framework 2.0beta1 (for PHP 5.2) API API documentation generated by ApiGen 2.3.0