Source for file ConfigAdapterIni.php
Documentation is available at ConfigAdapterIni.php
6: * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
8: * This source file is subject to the "Nette license" that is bundled
9: * with this package in the file license.txt.
11: * For more information please see http://nettephp.com
13: * @copyright Copyright (c) 2004, 2009 David Grudl
14: * @license http://nettephp.com/license Nette license
15: * @link http://nettephp.com
17: * @package Nette\Config
22: require_once dirname(__FILE__) .
'/../Config/IConfigAdapter.php';
27: * Reading and writing INI files.
29: * @author David Grudl
30: * @copyright Copyright (c) 2004, 2009 David Grudl
31: * @package Nette\Config
36: /** @var string key nesting separator (key1> key2> key3) */
37: public static $keySeparator =
'.';
39: /** @var string section inheriting separator (section < parent) */
40: public static $sectionSeparator =
' < ';
42: /** @var string raw section marker */
43: public static $rawSection =
'!';
48: * Static class - cannot be instantiated.
52: throw new LogicException("Cannot instantiate static class " .
get_class($this));
58: * Reads configuration from INI file.
59: * @param string file name
60: * @param string section to load
62: * @throws InvalidStateException
64: public static function load($file, $section =
NULL)
73: throw new Exception($msg);
76: $separator =
trim(self::$sectionSeparator);
78: foreach ($ini as $secName =>
$secData) {
80: if (is_array($secData)) {
81: if (substr($secName, -
1) ===
self::$rawSection) {
82: $secName =
substr($secName, 0, -
1);
84: } elseif (self::$keySeparator) {
85: // process key separators (key1> key2> key3)
87: foreach ($secData as $key =>
$val) {
89: foreach (explode(self::$keySeparator, $key) as $part) {
90: if (!isset($cursor[$part]) ||
is_array($cursor[$part])) {
91: $cursor =
& $cursor[$part];
101: // process extends sections like [staging < production] (with special support for separator ':')
102: $parts =
$separator ?
explode($separator, strtr($secName, ':', $separator)) :
array($secName);
106: foreach (self::$keySeparator ?
explode(self::$keySeparator, $parent) :
array($parent) as $part) {
107: if (isset($cursor[$part]) &&
is_array($cursor[$part])) {
108: $cursor =
& $cursor[$part];
117: if ($secName ===
'') {
122: if (self::$keySeparator) {
124: foreach (explode(self::$keySeparator, $secName) as $part) {
125: if (!isset($cursor[$part]) ||
is_array($cursor[$part])) {
126: $cursor =
& $cursor[$part];
132: $cursor =
& $data[$secName];
142: if ($section ===
NULL) {
145: } elseif (!isset($data[$section]) ||
!is_array($data[$section])) {
149: return $data[$section];
157: * @param Config to save
158: * @param string file
159: * @param string section name
162: public static function save($config, $file, $section =
NULL)
165: $output[] =
'; generated by Nette';// at ' . @strftime('%c');
168: if ($section ===
NULL) {
169: foreach ($config as $secName =>
$secData) {
170: if (!(is_array($secData) ||
$secData instanceof
Traversable)) {
174: $output[] =
"[$secName]";
175: self::build($secData, $output, '');
180: $output[] =
"[$section]";
181: self::build($config, $output, '');
193: * Recursive builds INI list.
194: * @param array|\Traversable
199: private static function build($input, & $output, $prefix)
201: foreach ($input as $key =>
$val) {
203: self::build($val, $output, $prefix .
$key .
self::$keySeparator);
205: } elseif (is_bool($val)) {
206: $output[] =
"$prefix$key = " .
($val ?
'true' :
'false');
209: $output[] =
"$prefix$key = $val";
212: $output[] =
"$prefix$key = \"$val\"";
215: throw new InvalidArgumentException("The '$prefix$key' item must be scalar or array, " .
gettype($val) .
" given.");