Skip to content

Instantly share code, notes, and snippets.

@hatyuki
Created October 15, 2009 07:08
Show Gist options
  • Save hatyuki/210741 to your computer and use it in GitHub Desktop.
Save hatyuki/210741 to your computer and use it in GitHub Desktop.
<?php // vim: ts=4 sts=4 sw=4
class SimpleConfigLoader
{
private $config = array( );
private $section = null;
private $loaded = array( );
function __construct ($path, $section=null)
{
if (!is_dir($path) ) {
throw new Exception("no such directory: $path");
}
$path = realpath($path).'/*.ini';
foreach ( glob($path, GLOB_MARK) as $file) {
if ( !is_file($file) ) { continue; }
$ini = parse_ini_file($file, true);
if ($ini !== false) {
$type = basename($file, '.ini');
$this->config[$type] = $ini;
$loaded[ ] = $file;
}
}
if ( !is_null($section) ) {
$this->section = $section;
}
}
function __call ($name, $ext_sec)
{
if ( !array_key_exists($name, $this->config) ) {
throw new Exception("config '$name' is not available");
}
$ext_sec = array_shift($ext_sec);
if ( isset($ext_sec) ) {
$this->section = $ext_sec;
}
if ( is_null($this->section) ) {
return $this->config[$name];
}
if ( !array_key_exists($this->section, $this->config[$name]) ) {
throw new Exception("section '$this->section' is not available");
}
return $this->config[$name][$this->section];
}
function getLoadedInis ( )
{
return $this->loaded;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment