Page MenuHomePhorge

No OneTemporary

Size
3 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/Kolab/FreeBusy/Config.php b/lib/Kolab/FreeBusy/Config.php
index 205a438..7b2ff5f 100644
--- a/lib/Kolab/FreeBusy/Config.php
+++ b/lib/Kolab/FreeBusy/Config.php
@@ -1,125 +1,152 @@
<?php
namespace Kolab\FreeBusy;
/**
* Wrapper class for service configuration
*/
class Config
{
private static $instance;
private $basedir = '.';
private $data = array();
private $valid = false;
/**
* Singelton getter
*
* @param string Path to load config from
*/
public static function getInstance($dir = null)
{
if (!isset(self::$instance)) {
self::$instance = new Config($dir);
}
if ($dir && !self::$instance->valid) {
self::$instance->load($configdir . '/config.ini');
}
return self::$instance;
}
/**
* Default constructor
*/
function __construct($configdir = null)
{
if ($configdir) {
$this->basedir = $configdir;
$this->load($configdir . '/config.ini');
}
}
/**
* Load config from the given .ini file
*/
private function load($inifile)
{
if ($raw = parse_ini_file($inifile, true)) {
$config['directories'] = array();
foreach ($raw as $section => $values) {
// check for known sections
if (in_array($section, array('httpauth','trustednetworks','log'))) {
$config[$section] = $values;
}
else if (strpos($section, 'directory') === 0 || isset($values['fbsource'])) {
$sect = preg_replace('/^directory\s*/', '', $section);
$key = strlen($sect) ? $sect : count($config['directories']);
$config['directories'][$key] = $values;
}
+ else if (!empty($values) && is_array($values)) {
+ $config[$section] = $values;
+ }
}
$this->register($config);
$this->valid = !empty($this->data['directories']);
}
else {
trigger_error("Failed to parse configuration from $inifile", E_USER_ERROR);
}
}
/**
* Dump the hierarchical structure of config options into a flat list with keys delimited by dots
*/
private function register($config, $prefix = '')
{
+ // merge the new config values over existing data
if (empty($prefix)) {
$this->data = array_replace_recursive($this->data, $config);
}
else if (is_array($config)) {
$pkey = rtrim($prefix, '.');
$this->data[$pkey] = is_array($this->data[$pkey]) ? array_replace_recursive((array)$this->data[$pkey], $config) : $config;
}
foreach ((array)$config as $key => $val) {
if (is_array($val)) {
$this->register($val, "$prefix$key.");
}
else {
$this->data[$prefix.$key] = $val;
}
}
+
+ // resolve references in config options (e.g. %(foo.bar))
+ if (empty($prefix)) {
+ array_walk_recursive($this->data, array($this, 'resolve_reference'));
+ }
+ }
+
+ /**
+ * Callback to resolve references in the given config option value
+ */
+ private function resolve_reference(&$value, $key)
+ {
+ if (is_string($value)) {
+ $value = preg_replace_callback('/%[({]([\w.]+)[})]/i', array($this, 'replace_reference'), $value);
+ }
+ }
+
+ /**
+ * Callback function to replace the given reference with the read config value
+ */
+ private function replace_reference($m)
+ {
+ return $this->data[$m[1]];
}
/**
* Magic getter for direct read-only access to config options
*/
public function __get($name)
{
return $this->data[$name];
}
/**
* Common getter for config options with fallback in default values
*
* @param string Config option name
* @param mixed Default value if option isn't set in config
* @return mixed Config option value
*/
public function get($name, $default = null)
{
return array_key_exists($name, $this->data) ? $this->data[$name] : $default;
}
/**
* Determines whether we have a valid configuration loaded
*
* @return boolean True if valid, False otherwise
*/
public function isValid()
{
return !empty($this->data);
}
}

File Metadata

Mime Type
text/x-diff
Expires
Sun, Feb 1, 9:12 AM (1 d, 10 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
426656
Default Alt Text
(3 KB)

Event Timeline