Permalink
Browse files

config

  • Loading branch information...
1 parent a07242b commit 7f6c0436d72fab559ae1d27bc9789d8c016aef6a @vovazolotoy vovazolotoy committed Aug 10, 2011
Showing with 108 additions and 143 deletions.
  1. +1 −49 johana.js
  2. +103 −90 system/prototypes/johana/config.js
  3. +4 −4 system/prototypes/johana/core.js
View
@@ -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());
@@ -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
@@ -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)

0 comments on commit 7f6c043

Please sign in to comment.