Source for file ConfigAdapterIni.php
Documentation is available at ConfigAdapterIni.php
6: * @copyright Copyright (c) 2004, 2010 David Grudl
7: * @license http://nettephp.com/license Nette license
8: * @link http://nettephp.com
10: * @package Nette\Config
16: * Reading and writing INI files.
18: * @copyright Copyright (c) 2004, 2010 David Grudl
19: * @package Nette\Config
24: /** @var string key nesting separator (key1> key2> key3) */
25: public static $keySeparator =
'.';
27: /** @var string section inheriting separator (section < parent) */
28: public static $sectionSeparator =
' < ';
30: /** @var string raw section marker */
31: public static $rawSection =
'!';
36: * Static class - cannot be instantiated.
40: throw new LogicException("Cannot instantiate static class " .
get_class($this));
46: * Reads configuration from INI file.
47: * @param string file name
48: * @param string section to load
50: * @throws InvalidStateException
52: public static function load($file, $section =
NULL)
61: throw new Exception($msg);
64: $separator =
trim(self::$sectionSeparator);
66: foreach ($ini as $secName =>
$secData) {
68: if (is_array($secData)) {
69: if (substr($secName, -
1) ===
self::$rawSection) {
70: $secName =
substr($secName, 0, -
1);
72: } elseif (self::$keySeparator) {
73: // process key separators (key1> key2> key3)
75: foreach ($secData as $key =>
$val) {
77: foreach (explode(self::$keySeparator, $key) as $part) {
78: if (!isset($cursor[$part]) ||
is_array($cursor[$part])) {
79: $cursor =
& $cursor[$part];
89: // process extends sections like [staging < production] (with special support for separator ':')
90: $parts =
$separator ?
explode($separator, strtr($secName, ':', $separator)) :
array($secName);
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];
105: if ($secName ===
'') {
110: if (self::$keySeparator) {
112: foreach (explode(self::$keySeparator, $secName) as $part) {
113: if (!isset($cursor[$part]) ||
is_array($cursor[$part])) {
114: $cursor =
& $cursor[$part];
120: $cursor =
& $data[$secName];
130: if ($section ===
NULL) {
133: } elseif (!isset($data[$section]) ||
!is_array($data[$section])) {
137: return $data[$section];
145: * @param Config to save
146: * @param string file
147: * @param string section name
150: public static function save($config, $file, $section =
NULL)
153: $output[] =
'; generated by Nette';// at ' . @strftime('%c');
156: if ($section ===
NULL) {
157: foreach ($config as $secName =>
$secData) {
158: if (!(is_array($secData) ||
$secData instanceof
Traversable)) {
162: $output[] =
"[$secName]";
163: self::build($secData, $output, '');
168: $output[] =
"[$section]";
169: self::build($config, $output, '');
181: * Recursive builds INI list.
182: * @param array|\Traversable
187: private static function build($input, & $output, $prefix)
189: foreach ($input as $key =>
$val) {
191: self::build($val, $output, $prefix .
$key .
self::$keySeparator);
193: } elseif (is_bool($val)) {
194: $output[] =
"$prefix$key = " .
($val ?
'true' :
'false');
197: $output[] =
"$prefix$key = $val";
200: $output[] =
"$prefix$key = \"$val\"";
203: throw new InvalidArgumentException("The '$prefix$key' item must be scalar or array, " .
gettype($val) .
" given.");