Exemplo n.º 1
0
 /**
  * Load configuration data from a ini file.
  *
  * If the ini file contains sections, the name of the configuration settings
  * will be the section name and the setting name with a hyphen in between,
  * e.g. the <code>hostname=...</code> setting inside
  * a <code>[database]</code> setting can be retrieved using
  * <code>AnewtConfig::get('database-hostname')</code>.
  *
  * \param $filename
  *   The filename of the ini file.
  */
 public static function load_ini_file($filename)
 {
     assert('is_string($filename);');
     if (!is_readable($filename)) {
         throw new Exception('The ini file does not exists or is not readable.');
     }
     $parsed = parse_ini_file($filename, true);
     if ($parsed === false) {
         throw new Exception('The ini file could not be parsed.');
     }
     $config = array();
     foreach ($parsed as $name => $value) {
         /* The value can be either a string (for top level ini settings), or
          * an array (for ini settings in sections) */
         if (is_string($value)) {
             $config[$name] = $value;
         } elseif (is_assoc_array($value)) {
             $section = $name;
             foreach ($value as $name => $value) {
                 $key = sprintf('%s-%s', $section, $name);
                 $config[$key] = $value;
             }
         } else {
             assert('false; // not reached;');
         }
     }
     /* Cast integer and boolean values to the correct type */
     foreach ($config as $name => &$value) {
         if (preg_match('/^-?[0-9]+$/', $value)) {
             $value = (int) $value;
         } elseif (preg_match('/^(1|true|yes|on)$/', $value)) {
             $value = true;
         } elseif (preg_match('/^(0|false|no|off)$/', $value)) {
             $value = false;
         }
     }
     AnewtConfig::seed($config);
 }
Exemplo n.º 2
0
<?php

error_reporting(E_ALL | E_STRICT);
require_once '../anewt.lib.php';
anewt_include('config');
AnewtConfig::set('foo', 'bar');
AnewtConfig::set('sampleint', 2);
assert('AnewtConfig::get("foo") === "bar"');
assert('AnewtConfig::getdefault("foo", "baz") === "bar"');
assert('AnewtConfig::getdefault("nonexistent", "baz") === "baz"');
assert('AnewtConfig::get("sampleint") === 2');
assert('AnewtConfig::getdefault("nonexistentint", 3) === 3');
AnewtConfig::seed(array('test' => 'foo', 'foo' => 'baz'));
assert('AnewtConfig::get("test") === "foo"');
assert('AnewtConfig::get("foo") === "baz"');
AnewtConfig::delete('foo');
assert('AnewtConfig::is_set("blah") === false');
assert('AnewtConfig::is_set("foo") === false');
assert('AnewtConfig::is_set("test") === true');
$export = AnewtConfig::to_array();
assert('array_has_key($export, "sampleint")');
assert('array_has_key($export, "test")');
assert('array_has_key($export, "foo") === false');