summaryrefslogtreecommitdiffstats
path: root/system/classes/config.php
diff options
context:
space:
mode:
authorDracony <draconyster@gmail.com>2013-01-10 16:25:50 +0200
committerDracony <draconyster@gmail.com>2013-01-10 16:25:50 +0200
commit4161d1d639dee2df73a30f4872e5280205e5da2d (patch)
tree0e791ba6b4a7853fb66bbf947ab49880e47c07cf /system/classes/config.php
parenta5d20f2082ac7de829996d8df23af6bbc27a91d5 (diff)
downloadPHPixie-4161d1d639dee2df73a30f4872e5280205e5da2d.zip
PHPixie-4161d1d639dee2df73a30f4872e5280205e5da2d.tar.gz
PHPixie-4161d1d639dee2df73a30f4872e5280205e5da2d.tar.bz2
New format for config files.
Writing of config files now supported
Diffstat (limited to 'system/classes/config.php')
-rw-r--r--system/classes/config.php195
1 files changed, 145 insertions, 50 deletions
diff --git a/system/classes/config.php b/system/classes/config.php
index b0550b4..7d32f69 100644
--- a/system/classes/config.php
+++ b/system/classes/config.php
@@ -1,51 +1,146 @@
-<?php
-
-/**
- * Handles retrieving of the configuration options.
- * You can add any configuration values to your /application/config.php file
- * as associative array and get those values using the get() method.
- */
-class Config {
-
- /**
- * Array of configuration options
- * @var array
- * @access public
- * @static
- */
- public static $data=array();
-
- /**
- * Retrieves a configuration value. You can use a dot notation
- * to access properties in nested arrays like this:
- * <code>
- * Config::get('database.default.user');
- * </code>
- *
- * @param string $key Configuration key to retrieve.
- * @param string $default Default value to return if the key is not found.
- * @return mixed Configuration value
- * @access public
- * @throws Exception If default value is not specified and the key is not found
- * @static
- */
- public static function get() {
- $p = func_get_args();
- $keys = explode('.', $p[0]);
- $group=Config::$data;
- for ($i = 0; $i < count($keys); $i++) {
- if ($i == count($keys) - 1) {
- if (isset($group[$keys[$i]]))
- return $group[$keys[$i]];
- break;
- }
- $group = Misc::arr($group, $keys[$i], null);
- if (!is_array($group))
- break;
- }
- if (isset($p[1]))
- return $p[1];
- throw new Exception("Configuration not set for {$p[0]}.");
- }
-
+<?php
+
+/**
+ * Handles retrieving of the configuration options.
+ * You can add configuration files to /application/config folder
+ * and later access them via the get() method.
+ * @package Core
+ */
+class Config {
+
+ /**
+ * Array of configuration files and values loaded from them
+ * @var array
+ * @access protected
+ * @static
+ */
+ protected static $groups = array();
+
+ /**
+ * Loads a group configuration file it has not been loaded before
+ * and returns its options.
+ *
+ * @param string $name Name of the configuration group to load
+ * @return array Array of options for this group
+ * @access public
+ * @throws Exception If a configuration file for this group does not exist
+ * @static
+ */
+ public static function get_group($name) {
+
+ if (!isset(Config::$groups[$name])) {
+
+ $file = Misc::find_file('config', $name);
+
+ if (!$file)
+ throw new Exception("Configuration file {$name}.php was not found.");
+
+ Config::load_group($name,$file);
+ }
+
+ return Config::$groups[$name]['options'];
+ }
+
+ /**
+ * Loads group from file
+ *
+ * @param string $name Name to assign the loaded group
+ * @param string $file File to load
+ * @access public
+ * @static
+ */
+ public static function load_group($name, $file) {
+
+ Config::$groups[$name] = array(
+ 'file' => $file,
+ 'options' => include($file)
+ );
+ }
+
+ /**
+ * Retrieves a configuration value. You can use a dot notation
+ * to access properties in group arrays. The first part of the key
+ * specifies the configuration file from which options should be loaded from
+ * <code>
+ * //Loads ['default']['user'] option
+ * //from database.php configuration file
+ * Config::get('database.default.user');
+ * </code>
+ *
+ * @param string $key Configuration key to retrieve.
+ * @param string $default Default value to return if the key is not found.
+ * @return mixed Configuration value
+ * @access public
+ * @throws Exception If default value is not specified and the key is not found
+ * @static
+ */
+ public static function get() {
+ $p = func_get_args();
+
+ $keys = explode('.', $p[0]);
+ $group_name = array_shift($keys);
+ $group = Config::get_group($group_name);
+
+ for ($i = 0; $i < count($keys); $i++) {
+ if ($i == count($keys) - 1) {
+ if (isset($group[$keys[$i]]))
+ return $group[$keys[$i]];
+ break;
+ }
+ $group = Misc::arr($group, $keys[$i], null);
+ if (!is_array($group))
+ break;
+ }
+
+ if (isset($p[1]))
+ return $p[1];
+
+ throw new Exception("Configuration not set for {$p[0]}.");
+ }
+
+ /**
+ * Sets a configuration option.
+ *
+ * @param string $key Configuration key to set
+ * @param string $value Value to set for this option
+ * @access public
+ * @static
+ */
+ public static function set($key,$value){
+ $keys = explode('.', $key);
+ $group_name = array_shift($keys);
+ $group = Config::get_group($group_name);
+ $subgroup = & $group;
+
+ foreach($keys as $i => $key) {
+
+ if ($i == count($keys) - 1) {
+
+ $subgroup[$key] = $value;
+
+ } else {
+
+ if(!isset($subgroup[$key])||!is_array($subgroup[$key]))
+ $subgroup[$key]=array();
+ $subgroup = & $subgroup[$key];
+
+ }
+ }
+
+ Config::$groups[$group_name]['options'] = $group;
+ }
+
+ /**
+ * Writes a configuration group back to the file it was loaded from
+ *
+ * @param string $group Name of the group to write
+ * @access public
+ * @static
+ */
+ public static function write($group){
+ Config::get_group($group);
+ $group=Config::$groups[$group];
+ file_put_contents($group['file'],"<?php\r\nreturn ".var_export($group['options'],true).";");
+ }
+
} \ No newline at end of file