Permalink
Browse files

config

  • Loading branch information...
1 parent 0b13b7d commit 0208322c3197f2b89b43fa4ce5595e665634792d @vovazolotoy vovazolotoy committed Aug 1, 2011
Showing with 204 additions and 3 deletions.
  1. +3 −0 johana.js
  2. +4 −0 system/prototypes/config.js
  3. +145 −0 system/prototypes/johana/config.js
  4. +52 −3 system/prototypes/johana/core.js
View
3 johana.js
@@ -38,3 +38,6 @@ require(SYSPATH + 'prototypes/johana/autoload');
// Bootstrap the application
require(APPPATH + 'bootstrap');
+
+//console.log(Johana.config('bootstrap'));
+
View
4 system/prototypes/config.js
@@ -0,0 +1,4 @@
+
+Config = JohanaConfig;
+
+module.exports = Config;
View
145 system/prototypes/johana/config.js
@@ -0,0 +1,145 @@
+/**
+ * Wrapper for configuration arrays. Multiple configuration readers can be
+ * attached to allow loading configuration from files, database, etc.
+ *
+ * @package Johana
+ * @category Configuration
+ * @author Johana Team
+ * @copyright (c) 2011 Johana Team
+ * @license http://johanaframework.org/license
+ */
+JohanaConfig = function()
+{
+ /**
+ * @var Array Configuration readers
+ */
+ var _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)
+ {
+ first = first || true;
+
+ 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;
+ };
+
+ /**
+ * Detach a configuration reader.
+ *
+ * config.detach(reader);
+ *
+ * @param Object ConfigReader instance
+ * @return this
+ */
+ this.detach = function(reader)
+ {
+ var key = _readers.indexOf(reader);
+
+ if (key !== -1)
+ {
+ // Remove the writer
+ delete _readers[key];
+ }
+
+ 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
+ */
+ this.load = function(group)
+ {
+ for (var reader in _readers)
+ {
+ var config = _readers[reader].load(group);
+
+ if (config)
+ {
+ // Found a reader for this configuration group
+ return config;
+ }
+ }
+
+ if (_readers.length === 0)
+ {
+ throw new Error('No configuration readers attached');
+ }
+
+ // Load the reader as an empty array
+ 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)
+ {
+ // Load the configuration group
+ var config = this.load(group);
+
+ // TODO:
+
+ return this;
+ };
+
+};
+
+/**
+ * @var JohanaConfig Singleton static instance
+ */
+var _instance = null;
+
+/**
+ * Get the singleton instance of Config.
+ *
+ * config = Config.instance();
+ *
+ * @return Config
+ */
+JohanaConfig.instance = function()
+{
+ if (_instance === null)
+ {
+ // Create a new instance
+ _instance = new Config();
+ }
+
+ return _instance;
+};
+
+module.exports = JohanaConfig;
View
55 system/prototypes/johana/core.js
@@ -102,7 +102,7 @@ JohanaCore.log;
/**
* @var Config config object
*/
-JohanaCore.config;
+JohanaCore._config;
/**
* @var Boolean Has [Johana.init] been called?
@@ -271,8 +271,8 @@ JohanaCore.init = function(settings)
// // Load the logger
// Johana.log = Log.instance();
//
-// // Load the config
-// Johana.config = Config.instance();
+ // Load the config
+ Johana._config = Config.instance();
};
/**
@@ -565,4 +565,53 @@ JohanaCore.findFile = function(dir, file, ext, list)
return found;
};
+/**
+ * Returns the configuration array for the requested group. See
+ * [configuration files](johana/files/config) for more information.
+ *
+ * // Get all the configuration in config/database.js
+ * var config = Johana.config('database');
+ *
+ * // Get only the default connection configuration
+ * var default = Johana.config('database.default')
+ *
+ * // Get only the hostname of the default connection
+ * var host = Johana.config('database.default.connection.hostname')
+ *
+ * @param String group name
+ * @return Config
+ */
+var _configCache = {};
+
+JohanaCore.config = function(group)
+{
+ var path = false;
+
+ var dot = group.indexOf('.');
+
+ if (dot !== -1)
+ {
+ // Split the config group and path
+ var path = group.substring(dot + 1);
+
+ group = group.substring(0, dot);
+ }
+
+ if (_configCache[group] === undefined)
+ {
+ // Load the config group into the cache
+ _configCache[group] = Johana._config.load(group);
+ }
+
+ if (path !== false)
+ {
+ return Arr.path(_config[group], path, null, '.');
+ }
+ else
+ {
+ return _configCache[group];
+ }
+};
+
+
module.exports = JohanaCore;

0 comments on commit 0208322

Please sign in to comment.