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 https://nette.org
13: * @copyright Copyright (c) 2004, 2009 David Grudl
14: * @license https://nette.org/license Nette license
15: * @link https://nette.org
17: * @package Nette\Config
23: require_once dirname(__FILE__) .
'/../Config/IConfigAdapter.php';
28: * Reading and writing INI files.
30: * @author David Grudl
31: * @copyright Copyright (c) 2004, 2009 David Grudl
32: * @package Nette\Config
37: /** @var string key nesting separator (key1> key2> key3) */
40: /** @var string section inheriting separator (section < parent) */
43: /** @var string raw section marker */
49: * Static class - cannot be instantiated.
53: throw new LogicException("Cannot instantiate static class " .
get_class($this));
59: * Reads configuration from INI file.
60: * @param string file name
61: * @param string section to load
63: * @throws InvalidStateException
65: public static function load($file, $section =
NULL)
74: throw new Exception($msg);
77: $separator =
trim(self::$sectionSeparator);
79: foreach ($ini as $secName =>
$secData) {
81: if (is_array($secData)) {
82: if (substr($secName, -
1) ===
self::$rawSection) {
83: $secName =
substr($secName, 0, -
1);
85: } elseif (self::$keySeparator) {
86: // process key separators (key1> key2> key3)
88: foreach ($secData as $key =>
$val) {
90: foreach (explode(self::$keySeparator, $key) as $part) {
91: if (!isset($cursor[$part]) ||
is_array($cursor[$part])) {
92: $cursor =
& $cursor[$part];
102: // process extends sections like [staging < production] (with special support for separator ':')
103: $parts =
$separator ?
explode($separator, strtr($secName, ':', $separator)) :
array($secName);
107: foreach (self::$keySeparator ?
explode(self::$keySeparator, $parent) :
array($parent) as $part) {
108: if (isset($cursor[$part]) &&
is_array($cursor[$part])) {
109: $cursor =
& $cursor[$part];
118: if ($secName ===
'') {
123: if (self::$keySeparator) {
125: foreach (explode(self::$keySeparator, $secName) as $part) {
126: if (!isset($cursor[$part]) ||
is_array($cursor[$part])) {
127: $cursor =
& $cursor[$part];
133: $cursor =
& $data[$secName];
143: if ($section ===
NULL) {
146: } elseif (!isset($data[$section]) ||
!is_array($data[$section])) {
150: return $data[$section];
158: * @param Config to save
159: * @param string file
160: * @param string section name
163: public static function save($config, $file, $section =
NULL)
166: $output[] =
'; generated by Nette';// at ' . @strftime('%c');
169: if ($section ===
NULL) {
170: foreach ($config as $secName =>
$secData) {
171: if (!(is_array($secData) ||
$secData instanceof
Traversable)) {
175: $output[] =
"[$secName]";
176: self::build($secData, $output, '');
181: $output[] =
"[$section]";
182: self::build($config, $output, '');
194: * Recursive builds INI list.
195: * @param array|\Traversable
200: private static function build($input, & $output, $prefix)
202: foreach ($input as $key =>
$val) {
204: self::build($val, $output, $prefix .
$key .
self::$keySeparator);
206: } elseif (is_bool($val)) {
207: $output[] =
"$prefix$key = " .
($val ?
'true' :
'false');
210: $output[] =
"$prefix$key = $val";
213: $output[] =
"$prefix$key = \"$val\"";
216: throw new InvalidArgumentException("The '$prefix$key' item must be scalar or array, " .
gettype($val) .
" given.");