Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

autoload

  • Loading branch information...
commit a2699c139061da0a344222996aab2a84db7608d7 1 parent 7f6c043
@vovazolotoy vovazolotoy authored
View
10 johana.js
@@ -15,10 +15,13 @@ MODPATH = require('fs').realpathSync('./modules') + '/';
*/
SYSPATH = require('fs').realpathSync('./system') + '/';
+/**
+ * Set the full path to the docroot
+ */
DOCROOT = require('fs').realpathSync('.') + '/';
/**
- * Load the core Johana class
+ * Load the Johana core
*/
require(SYSPATH + 'prototypes/johana/core');
@@ -33,9 +36,10 @@ else
require(SYSPATH + 'prototypes/johana');
}
-require(SYSPATH + 'prototypes/johana/autoload');
+// Load prototypes
+require(SYSPATH + 'prototypes/autoload');
// Bootstrap the application
require(APPPATH + 'bootstrap');
-Johana.conf.attach(new ConfigFile());
+console.log(Johana.config('vova').lname);
View
89 system/prototypes/autoload.js
@@ -0,0 +1,89 @@
+/**
+ * Provides auto-loading support of prototypes that follow Johana's
+ * naming conventions
+ *
+ * // Loads prototypes/my/proto/name.js
+ * Johana.autoLoad('MyProtoName');
+ *
+ * @package Johana
+ * @category Core
+ * @author Johana Team
+ * @copyright (c) 2011 Johana Team
+ * @license http://johanaframework.org/license
+ */
+Autoload = {};
+
+/**
+ * Get list of Protorypes in folder (recursive)
+ *
+ * @param String directory for lookup
+ * @param String prefix to prototype name if file is not in root dir
+ * @return Array list of prototypes names
+ */
+Autoload.list = function (root, prefix)
+{
+ prefix = prefix || '';
+
+ var prototypes = [];
+
+ // Load all items in root dir
+ try
+ {
+ var files = require('fs').readdirSync(root);
+ }
+ catch (e)
+ {
+ var files = false;
+ }
+
+ for (var f in files)
+ {
+ // Get info about loaded fs item
+ var stats = require('fs').statSync(root + '/' + files[f]);
+
+ if (stats.isFile())
+ {
+ // As prototype name use filename without extension
+ var proto = files[f].replace(/\.[^.]+$/, '');
+
+ // Capitalize prototype name
+ proto = proto.charAt(0).toUpperCase() + proto.slice(1);
+
+ prototypes.push(prefix + proto);
+ }
+ else if (stats.isDirectory())
+ {
+ // Append to prefix capitalized directory name
+ var dir = files[f].charAt(0).toUpperCase() + files[f].slice(1);
+
+ // Review sub items
+ var sub = Autoload.list(root + '/' + files[f], prefix + dir);
+
+ prototypes = prototypes.concat(sub);
+ }
+ }
+
+ return prototypes;
+};
+
+/**
+ * Get list of Protorypes in folder (recursive)
+ *
+ * @param String directory for lookup
+ * @param String prefix to prototype name if file is not in root dir
+ * @return Array list of prototypes names
+ */
+Autoload.init = function (path)
+{
+ var prototypes = Autoload.list(path);
+
+ for (var p in prototypes)
+ {
+ // Load to global context
+ Johana.autoLoad(prototypes[p]);
+ }
+};
+
+// Load system and application prototypes
+Autoload.init(SYSPATH + 'prototypes');
+Autoload.init(APPPATH + 'prototypes');
View
45 system/prototypes/johana/autoload.js
@@ -1,45 +0,0 @@
-
-// Load native node's modules to global
-buffer = require('buffer'),
-querystring = require('querystring'),
-http = require('http'),
-net = require('net'),
-freelist = require('freelist'),
-vm = require('vm'),
-util = require('util'),
-assert = require('assert'),
-module = require('module'),
-_debugger = require('_debugger'),
-tty_win32 = require('tty_win32'),
-fs = require('fs'),
-os = require('os'),
-dns = require('dns'),
-events = require('events'),
-url = require('url'),
-tls = require('tls'),
-tty_posix = require('tty_posix'),
-crypto = require('crypto'),
-sys = require('sys'),
-https = require('https'),
-stream = require('stream'),
-readline = require('readline'),
-_linklist = require('_linklist'),
-tty = require('tty'),
-child_process = require('child_process'),
-repl = require('repl'),
-path = require('path'),
-string_decoder = require('string_decoder'),
-timers = require('timers'),
-constants = require('constants');
-
-Johana.autoLoad('Route');
-Johana.autoLoad('Profiler');
-Johana.autoLoad('Request');
-Johana.autoLoad('View');
-Johana.autoLoad('JohanaConfigReader');
-Johana.autoLoad('ConfigReader');
-Johana.autoLoad('JohanaConfigFile');
-Johana.autoLoad('ConfigFile');
-Johana.autoLoad('JohanaConfig');
-Johana.autoLoad('Config');
-Johana.autoLoad('ControllerAuth');
View
38 system/prototypes/johana/core.js
@@ -202,7 +202,7 @@ JohanaCore.init = function(settings)
{
try
{
- var cacheDirExists = fs.statSync(settings['cacheDir']).isDirectory();
+ var cacheDirExists = require('fs').statSync(settings['cacheDir']).isDirectory();
}
catch (e)
{
@@ -215,7 +215,7 @@ JohanaCore.init = function(settings)
try
{
// Create the cache directory
- fs.mkdirSync(settings['cacheDir'], 0755);
+ require('fs').mkdirSync(settings['cacheDir'], 0755);
}
catch (e)
{
@@ -225,7 +225,7 @@ JohanaCore.init = function(settings)
}
// Set the cache directory path
- Johana.cacheDir = fs.realpathSync(settings['cacheDir']);
+ Johana.cacheDir = require('fs').realpathSync(settings['cacheDir']);
}
else
{
@@ -236,7 +236,7 @@ JohanaCore.init = function(settings)
try
{
// Check if cache dir it writable
- fs.chmodSync(Johana.cacheDir, 0755);
+ require('fs').chmodSync(Johana.cacheDir, 0755);
}
catch (e)
{
@@ -305,7 +305,7 @@ JohanaCore.deinit = function()
/**
* Provides auto-loading support of base objects
*
- * @param String class name
+ * @param String prototype name
* @return Boolean
*/
JohanaCore.autoLoad = function(lib)
@@ -314,6 +314,12 @@ JohanaCore.autoLoad = function(lib)
var path = Johana.findFile('prototypes', file);
+ if (global[lib] !== undefined)
+ {
+ // Object was loaded before
+ return true;
+ }
+
if (path)
{
// Make some magic
@@ -361,7 +367,7 @@ JohanaCore.modules = function(modules)
try
{
// Check if module is valid
- var module = fs.statSync(modules[name]);
+ var module = require('fs').statSync(modules[name]);
}
catch (e)
{
@@ -371,7 +377,7 @@ JohanaCore.modules = function(modules)
if (module && module.isDirectory())
{
// Add the module to include paths
- modules[name] = fs.realpathSync(modules[name]) + '/';
+ modules[name] = require('fs').realpathSync(modules[name]) + '/';
paths.push(modules[name]);
}
@@ -398,7 +404,7 @@ JohanaCore.modules = function(modules)
try
{
// Check if exists initialization file
- var init = fs.statSync(file);
+ var init = require('fs').statSync(file);
}
catch (e)
{
@@ -410,6 +416,9 @@ JohanaCore.modules = function(modules)
// Include the module initialization file once
require(file);
}
+
+ // load module prototypes
+ Autoload.init(_modules[path] + 'prototypes');
}
return _modules;
@@ -507,7 +516,7 @@ JohanaCore.findFile = function(dir, file, ext, list)
try
{
// check if file exists
- var file = fs.statSync(paths[dir] + path);
+ var file = require('fs').statSync(paths[dir] + path);
}
catch (e)
{
@@ -531,7 +540,7 @@ JohanaCore.findFile = function(dir, file, ext, list)
try
{
// check if file exists
- var file = fs.statSync(_paths[dir] + path);
+ var file = require('fs').statSync(_paths[dir] + path);
}
catch (e)
{
@@ -577,6 +586,7 @@ JohanaCore.findFile = function(dir, file, ext, list)
*
* // Get only the hostname of the default connection
* var host = Johana.config('database.default.connection.hostname')
+ * var host = Johana.config('database').default.connection.hostname
*
* @param String group name
* @return Config
@@ -587,14 +597,14 @@ JohanaCore.config = function(group)
{
var path = false;
- var dotIndex = group.indexOf('.');
+ var dot = group.indexOf('.');
- if (dotIndex !== -1)
+ if (dot !== -1)
{
// Split the config group and path
- var path = group.substring(dotIndex + 1);
+ var path = group.substring(dot + 1);
- group = group.substring(0, dotIndex);
+ group = group.substring(0, dot);
}
if (_configCache[group] === undefined)
Please sign in to comment.
Something went wrong with that request. Please try again.