forked from bcit-ci/CodeIgniter
-
Notifications
You must be signed in to change notification settings - Fork 0
MY Loader
Derek Jones edited this page Jul 5, 2012
·
3 revisions
Category:Libraries::Extended Made the BASEPATH and APPPATH changable
class MY_Loader extends CI_Loader
{
var $_ci_base_path = '';
var $_ci_app_path = '';
function CI_Loader()
{
parent::CI_Loader();
$this->_ci_base_path = BASEPATH;
$this->_ci_app_path = APPPATH
$this->_ci_view_path = $this->_ci_app_path.'views/';
log_message('debug', "MY_Loader Class Initialized");
}
function model($model, $name = '', $db_conn = FALSE)
{
if (is_array($model))
{
foreach($model as $babe)
{
$this->model($babe);
}
return;
}
if ($model == '')
{
return;
}
// Is the model in a sub-folder? If so, parse out the filename and path.
if (strpos($model, '/') === FALSE)
{
$path = '';
}
else
{
$x = explode('/', $model);
$model = end($x);
unset($x[count($x)-1]);
$path = implode('/', $x).'/';
}
if ($name == '')
{
$name = $model;
}
if (in_array($name, $this->_ci_models, TRUE))
{
return;
}
$CI =& get_instance();
if (isset($CI->$name))
{
show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
}
$model = strtolower($model);
if ( ! file_exists($this->_ci_app_path.'models/'.$path.$model.EXT))
{
show_error('Unable to locate the model you have specified: '.$model);
}
if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
{
if ($db_conn === TRUE)
$db_conn = '';
$CI->load->database($db_conn, FALSE, TRUE);
}
if ( ! class_exists('Model'))
{
load_class('Model', FALSE);
}
require_once($this->_ci_app_path.'models/'.$path.$model.EXT);
$model = ucfirst($model);
$CI->$name = new $model();
$CI->$name->_assign_libraries();
$this->_ci_models[] = $name;
}
function database($params = '', $return = FALSE, $active_record = FALSE)
{
// Grab the super object
$CI =& get_instance();
// Do we even need to load the database class?
if (class_exists('CI_DB') AND $return == FALSE AND $active_record == FALSE AND isset($CI->db) AND is_object($CI->db))
{
return FALSE;
}
require_once($this->_ci_base_path.'database/DB'.EXT);
if ($return === TRUE)
{
return DB($params, $active_record);
}
// Initialize the db variable. Needed to prevent
// reference errors with some configurations
$CI->db = '';
// Load the DB class
$CI->db =& DB($params, $active_record);
// Assign the DB object to any existing models
$this->_ci_assign_to_models();
}
function dbutil()
{
if ( ! class_exists('CI_DB'))
{
$this->database();
}
$CI =& get_instance();
// for backwards compatibility, load dbforge so we can extend dbutils off it
// this use is deprecated and strongly discouraged
$CI->load->dbforge();
require_once($this->_ci_base_path.'database/DB_utility'.EXT);
require_once($this->_ci_base_path.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility'.EXT);
$class = 'CI_DB_'.$CI->db->dbdriver.'_utility';
$CI->dbutil =& new $class();
$CI->load->_ci_assign_to_models();
}
function dbforge()
{
if ( ! class_exists('CI_DB'))
{
$this->database();
}
$CI =& get_instance();
require_once($this->_ci_base_path.'database/DB_forge'.EXT);
require_once($this->_ci_base_path.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_forge'.EXT);
$class = 'CI_DB_'.$CI->db->dbdriver.'_forge';
$CI->dbforge = new $class();
}
function helper($helpers = array())
{
if ( ! is_array($helpers))
{
$helpers = array($helpers);
}
foreach ($helpers as $helper)
{
$helper = strtolower(str_replace(EXT, '', str_replace('_helper', '', $helper)).'_helper');
if (isset($this->_ci_helpers[$helper]))
{
continue;
}
$ext_helper = $this->_ci_app_path.'helpers/'.config_item('subclass_prefix').$helper.EXT;
// Is this a helper extension request?
if (file_exists($ext_helper))
{
$base_helper = $this->_ci_base_path.'helpers/'.$helper.EXT;
if ( ! file_exists($base_helper))
{
show_error('Unable to load the requested file: helpers/'.$helper.EXT);
}
include_once($ext_helper);
include_once($base_helper);
}
elseif (file_exists($this->_ci_app_path.'helpers/'.$helper.EXT))
{
include_once($this->_ci_app_path.'helpers/'.$helper.EXT);
}
else
{
if (file_exists($this->_ci_base_path.'helpers/'.$helper.EXT))
{
include($this->_ci_base_path.'helpers/'.$helper.EXT);
}
else
{
show_error('Unable to load the requested file: helpers/'.$helper.EXT);
}
}
$this->_ci_helpers[$helper] = TRUE;
}
log_message('debug', 'Helpers loaded: '.implode(', ', $helpers));
}
function plugin($plugins = array())
{
if ( ! is_array($plugins))
{
$plugins = array($plugins);
}
foreach ($plugins as $plugin)
{
$plugin = strtolower(str_replace(EXT, '', str_replace('_pi', '', $plugin)).'_pi');
if (isset($this->_ci_plugins[$plugin]))
{
continue;
}
if (file_exists($this->_ci_app_path.'plugins/'.$plugin.EXT))
{
include($this->_ci_app_path.'plugins/'.$plugin.EXT);
}
else
{
if (file_exists($this->_ci_base_path.'plugins/'.$plugin.EXT))
{
include($this->_ci_base_path.'plugins/'.$plugin.EXT);
}
else
{
show_error('Unable to load the requested file: plugins/'.$plugin.EXT);
}
}
$this->_ci_plugins[$plugin] = TRUE;
}
log_message('debug', 'Plugins loaded: '.implode(', ', $plugins));
}
function script($scripts = array())
{
if ( ! is_array($scripts))
{
$scripts = array($scripts);
}
foreach ($scripts as $script)
{
$script = strtolower(str_replace(EXT, '', $script));
if (isset($this->_ci_scripts[$script]))
{
continue;
}
if ( ! file_exists($this->_ci_app_path.'scripts/'.$script.EXT))
{
show_error('Unable to load the requested script: scripts/'.$script.EXT);
}
include($this->_ci_app_path.'scripts/'.$script.EXT);
}
log_message('debug', 'Scripts loaded: '.implode(', ', $scripts));
}
function _ci_load_class($class, $params = NULL)
{
// Get the class name
$class = str_replace(EXT, '', $class);
// We'll test for both lowercase and capitalized versions of the file name
foreach (array(ucfirst($class), strtolower($class)) as $class)
{
$subclass = $this->_ci_app_path.'libraries/'.config_item('subclass_prefix').$class.EXT;
// Is this a class extension request?
if (file_exists($subclass))
{
$baseclass = $this->_ci_base_path.'libraries/'.ucfirst($class).EXT;
if ( ! file_exists($baseclass))
{
log_message('error', "Unable to load the requested class: ".$class);
show_error("Unable to load the requested class: ".$class);
}
// Safety: Was the class already loaded by a previous call?
if (in_array($subclass, $this->_ci_classes))
{
$is_duplicate = TRUE;
log_message('debug', $class." class already loaded. Second attempt ignored.");
return;
}
include($baseclass);
include($subclass);
$this->_ci_classes[] = $subclass;
return $this->_ci_init_class($class, config_item('subclass_prefix'), $params);
}
// Lets search for the requested library file and load it.
$is_duplicate = FALSE;
for ($i = 1; $i < 3; $i++)
{
$path = ($i % 2) ? $this->_ci_app_path : $this->_ci_base_path;
$filepath = $path.'libraries/'.$class.EXT;
// Does the file exist? No? Bummer...
if ( ! file_exists($filepath))
{
continue;
}
// Safety: Was the class already loaded by a previous call?
if (in_array($filepath, $this->_ci_classes))
{
$is_duplicate = TRUE;
log_message('debug', $class." class already loaded. Second attempt ignored.");
return;
}
include($filepath);
$this->_ci_classes[] = $filepath;
return $this->_ci_init_class($class, '', $params);
}
} // END FOREACH
// If we got this far we were unable to find the requested class.
// We do not issue errors if the load call failed due to a duplicate request
if ($is_duplicate == FALSE)
{
log_message('error', "Unable to load the requested class: ".$class);
show_error("Unable to load the requested class: ".$class);
}
}
}