From 7f6c0436d72fab559ae1d27bc9789d8c016aef6a Mon Sep 17 00:00:00 2001 From: Vova Zolotoy Date: Wed, 10 Aug 2011 14:45:15 +0200 Subject: [PATCH] config --- johana.js | 50 +------- system/prototypes/johana/config.js | 193 +++++++++++++++-------------- system/prototypes/johana/core.js | 8 +- 3 files changed, 108 insertions(+), 143 deletions(-) diff --git a/johana.js b/johana.js index a4c664b..b0939ca 100644 --- a/johana.js +++ b/johana.js @@ -38,52 +38,4 @@ require(SYSPATH + 'prototypes/johana/autoload'); // Bootstrap the application require(APPPATH + 'bootstrap'); -Johana.conf.attach(new JohanaConfigFile()); - -//console.log(new ConfigFile()); -console.log(Johana.config('vova')); - - - -//console.log(new ConfigFile()); -//console.log(Johana.config('vova')); -function parent() -{ -// this.name = 'parent'; -// this.log = function() -// { -// console.log('parent log'); -// }; -}; -parent.prototype.parentProp = 'parent prop'; -parent.prototype.log = function() -{ - console.log('parent log'); -}; - -function child() -{ - console.log(this); -// this.log = function() -// { -// this.super_.log(); -// -// console.log('child log'); -// }; -// this.name = 'child'; -}; -require('util').inherits(child, parent); - - -child.prototype.log = function() -{ - console.log('child log', child.super_.log()); -}; - - - -console.log(parent, child.super_); -new parent().log(); -new child().log(); - - +Johana.conf.attach(new ConfigFile()); diff --git a/system/prototypes/johana/config.js b/system/prototypes/johana/config.js index 6109326..17b1f2e 100644 --- a/system/prototypes/johana/config.js +++ b/system/prototypes/johana/config.js @@ -1,5 +1,5 @@ /** - * Wrapper for configuration arrays. Multiple configuration readers can be + * Wrapper for configuration objects. Multiple configuration readers can be * attached to allow loading configuration from files, database, etc. * * @package Johana @@ -13,113 +13,126 @@ JohanaConfig = function() /** * @var Array Configuration readers */ - var _readers = []; + this._readers = []; +}; - /** - * Attach a configuration reader. By default, the reader will be added as - * the first used reader. However, if the reader should be used only when - * all other readers fail, use `false` for the second parameter. - * - * config.attach(reader); // Try first - * config.attach(reader, false); // Try last - * - * @param Object ConfigReader instance - * @param Boolean add the reader as the first used object - * @return this - */ - this.attach = function(reader, first) +/** + * Attach a configuration reader. By default, the reader will be added as + * the first used reader. However, if the reader should be used only when + * all other readers fail, use `false` for the second parameter. + * + * config.attach(reader); // Try first + * config.attach(reader, false); // Try last + * + * @param Object ConfigReader instance + * @param Boolean add the reader as the first used object + * @return this + */ +JohanaConfig.prototype.attach = function(reader, first) +{ + if (first === true) + { + // Place the config reader at the top of the stack + this._readers.unshift(reader); + } + else { - first = first || true; + // Place the reader at the bottom of the stack + this._readers.push(reader); + } - if (first === true) - { - // Place the log reader at the top of the stack - _readers.unshift(reader); - } - else - { - // Place the reader at the bottom of the stack - _readers.push(reader); - } + return this; +}; - return this; - }; +/** + * Detach a configuration reader. + * + * config.detach(reader); + * + * @param Object ConfigReader instance + * @return this + */ +JohanaConfig.prototype.detach = function(reader) +{ + var key = this._readers.indexOf(reader); - /** - * Detach a configuration reader. - * - * config.detach(reader); - * - * @param Object ConfigReader instance - * @return this - */ - this.detach = function(reader) + if (key !== -1) { - var key = _readers.indexOf(reader); + // Remove the reader + this._readers.splice(key, 1); + } - if (key !== -1) - { - // Remove the writer - delete _readers[key]; - } + return this; +}; - return this; - }; +/** + * Load a configuration group. Searches the readers in order until the + * group is found. If the group does not exist, an empty configuration + * array will be loaded using the first reader. + * + * array = config.load(name); + * + * @param String configuration group name + * @return ConfigReader + * @throws Error + */ +JohanaConfig.prototype.load = function(group) +{ + if (this._readers.length === 0) + { + throw new Error('No configuration readers attached'); + } - /** - * Load a configuration group. Searches the readers in order until the - * group is found. If the group does not exist, an empty configuration - * array will be loaded using the first reader. - * - * array = config.load(name); - * - * @param String configuration group name - * @return ConfigReader - * @throws Error - */ - this.load = function(group) + for (var reader in this._readers) { - if (_readers.length === 0) - { - throw new Error('No configuration readers attached'); - } + var config = this._readers[reader].load(group); - for (var reader in _readers) + if (config) { - var config = _readers[reader].load(group); - - if (config) - { - // Found a reader for this configuration group - return config; - } + // Found a reader for this configuration group + return config; } + } - // Reset the iterator - var config = _readers.slice(0, 1)[0]; + // Reset the iterator + var config = this._readers.slice(0, 1)[0]; - // Load the reader as an empty object - return config.load(group, {}); - }; + // Load the reader as an empty object + return config.load(group, {}); +}; - /** - * Copy one configuration group to all of the other readers. - * - * config.copy(name); - * - * @param String configuration group name - * @return this - */ - this.copy = function(group) +/** + * Copy one configuration group to all of the other readers. + * + * config.copy(name); + * + * @param String configuration group name + * @return this + */ +JohanaConfig.prototype.copy = function(group) +{ + // Load the configuration group + var config = this.load(group); + + for (var reader in this._readers) { - // Load the configuration group - var config = this.load(group); + if (config instanceof this._readers[reader].constructor) + { + // Do not copy the config to the same group + continue; + } - // TODO: + // Load the configuration object + var object = this._readers[reader].load(group, {}); - return this; - }; + for (var key in config) + { + // Copy each value in the config + object.set(key, config[key]); + } + } + return this; }; /** @@ -130,7 +143,7 @@ var _instance = null; /** * Get the singleton instance of Config. * - * config = Config.instance(); + * var config = Config.instance(); * * @return Config */ @@ -145,4 +158,4 @@ JohanaConfig.instance = function() return _instance; }; -module.exports = JohanaConfig; +module.exports = JohanaConfig; // End diff --git a/system/prototypes/johana/core.js b/system/prototypes/johana/core.js index af877c1..0e58bc3 100644 --- a/system/prototypes/johana/core.js +++ b/system/prototypes/johana/core.js @@ -587,14 +587,14 @@ JohanaCore.config = function(group) { var path = false; - var dot = group.indexOf('.'); + var dotIndex = group.indexOf('.'); - if (dot !== -1) + if (dotIndex !== -1) { // Split the config group and path - var path = group.substring(dot + 1); + var path = group.substring(dotIndex + 1); - group = group.substring(0, dot); + group = group.substring(0, dotIndex); } if (_configCache[group] === undefined)