As the name implies, loaders are loaded elements, when using CI, often loaded with:
$this->load->library ()
$this->load->view ()
$this->load->model ()
$this->load->database ()
$this->load->helper ()
$this->load->config ()
$this->load->add_package_path ()
Copy Code code as follows:
/**
* Loader Class
*
* Users load views and files, common functions have model (), view (), library (), helper ()
*
* Controller's good assistant, $this->load =& load_class (' Loader ', ' core '), loaded Loader,controller is incredibly powerful.
*/
Class Ci_loader {
protected $_ci_ob_level;
Protected $_ci_view_paths = Array ();
Protected $_ci_library_paths = Array ();
Protected $_ci_model_paths = Array ();
Protected $_ci_helper_paths = Array ();
Protected $_base_classes = Array (); Set by the Controller class
Protected $_ci_cached_vars = Array ();
Protected $_ci_classes = Array ();
Protected $_ci_loaded_files = Array ();
Protected $_ci_models = Array ();
Protected $_ci_helpers = Array ();
Protected $_ci_varmap = Array (' unit_test ' => ' unit ',
' User_agent ' => ' agent ');
Public Function __construct ()
{
Get buffer nesting level
$this->_ci_ob_level = Ob_get_level ();
Library Path
$this->_ci_library_paths = Array (AppPath, basepath);
Helper Path
$this->_ci_helper_paths = Array (AppPath, basepath);
Model Path
$this->_ci_model_paths = Array (AppPath);
View Path
$this->_ci_view_paths = Array (apppath. ' views/' => TRUE);
Log_message (' Debug ', "Loader Class initialized");
}
// --------------------------------------------------------------------
/**
* Initialization of Loader
*
*/
Public Function Initialize ()
{
$this->_ci_classes = Array ();
$this->_ci_loaded_files = Array ();
$this->_ci_models = Array ();
The core class that loads the is_loaded (the kernel class function in common) is given to _base_classes
$this->_base_classes =& is_loaded ();
Loading files in autoload.php configuration
$this->_ci_autoloader ();
return $this;
}
// --------------------------------------------------------------------
/**
* Detection class is loaded
*/
Public Function is_loaded ($class)
{
if (Isset ($this->_ci_classes[$class]))
{
return $this->_ci_classes[$class];
}
return FALSE;
}
// --------------------------------------------------------------------
/**
* Load Class
*/
Public Function library ($library = ', $params = null, $object _name = null)
{
if (Is_array ($library))
{
foreach ($library as $class)
{
$this->library ($class, $params);
}
Return
}
If $library is empty or already loaded ...
if ($library = = ' OR isset ($this->_base_classes[$library]))
{
return FALSE;
}
if (! Is_null ($params) &&! Is_array ($params))
{
$params = NULL;
}
$this->_ci_load_class ($library, $params, $object _name);
}
// --------------------------------------------------------------------
/**
* Load and Instantiate model
*/
Public function model ($model, $name = ', $db _conn = FALSE)
{
CI supports arrays to load multiple model
if (Is_array ($model))
{
foreach ($model as $babe)
{
$this->model ($babe);
}
Return
}
if ($model = = ")
{
Return
}
$path = ';
Whether subdirectories exist
if (($last _slash = Strrpos ($model, '/'))!== FALSE)
{
The path is in front of the last slash
$path = substr ($model, 0, $last _slash + 1);
and the model name behind it
$model = substr ($model, $last _slash + 1);
}
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 this is already being used: '. $name);
}
$model = Strtolower ($model); Model file name All lowercase
foreach ($this->_ci_model_paths as $mod _path)
{
if (! file_exists ($mod _path. ' models/'. $path. $model. ') php '))
{
Continue
}
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 (' Ci_model '))
{
Load_class (' Model ', ' core ');
}
Require_once ($mod _path. ' models/'. $path. $model. php ');
$model = Ucfirst ($model);
$CI-> $name = new $model ();
stored in the Loader::_ci_models, you can use it later to determine whether a model has been loaded.
$this->_ci_models[] = $name;
Return
}
Couldn ' t find the Model
Show_error (' Unable to locate the model for you have specified: '. $model);
}
// --------------------------------------------------------------------
/**
* Database Loader
*/
Public Function Database ($params = ', $return = FALSE, $active _record = NULL)
{
Grab the Super Object
$CI =& get_instance ();
Whether to load DB
if (class_exists (' ci_db ') and $return = = FALSE and $active _record = = NULL and isset ($CI->db) and Is_object ($CI->db))
{
return FALSE;
}
Require_once (basepath. ' database/db.php ');
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);
}
// --------------------------------------------------------------------
/**
* Load Database Tools class
*/
Public 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 is deprecated and strongly discouraged
$CI->load->dbforge ();
Require_once (basepath. ' database/db_utility.php ');
Require_once (basepath. ' database/drivers/'. $CI->db->dbdriver. /'. $CI->db->dbdriver. ' _utility.php ');
$class = ' ci_db_ '. $CI->db->dbdriver. ' _utility ';
$CI->dbutil = new $class ();
}
// --------------------------------------------------------------------
/**
* Load the Database Forge Class
*
* @return String
*/
Public Function Dbforge ()
{
if (! class_exists (' ci_db '))
{
$this->database ();
}
$CI =& get_instance ();
Require_once (basepath. ' database/db_forge.php ');
Require_once (basepath. ' database/drivers/'. $CI->db->dbdriver. /'. $CI->db->dbdriver. ' _forge.php ');
$class = ' ci_db_ '. $CI->db->dbdriver. ' _forge ';
$CI->dbforge = new $class ();
}
// --------------------------------------------------------------------
/**
* Load View File
*/
Public Function View ($view, $vars = Array (), $return = FALSE)
{
return $this->_ci_load Array (' _ci_view ' => $view, ' _ci_vars ' => $this->_ci_object_to_array ($vars), ' _ci_ Return ' => $return));
}
// --------------------------------------------------------------------
/**
* Load normal files
*/
Public function file ($path, $return = FALSE)
{
return $this->_ci_load (Array (' _ci_path ' => $path, ' _ci_return ' => $return));
}
// --------------------------------------------------------------------
/**
* Set Variable
*
* Once variables are set they become available within
* The Controller class and its "view" files.
*
*/
Public Function VARs ($vars = Array (), $val = ')
{
if ($val!= ' and is_string ($vars))
{
$vars = Array ($vars => $val);
}
$vars = $this->_ci_object_to_array ($vars);
if (Is_array ($vars) and Count ($vars) > 0)
{
foreach ($vars as $key => $val)
{
$this->_ci_cached_vars[$key] = $val;
}
}
}
// --------------------------------------------------------------------
/**
* Check and get variables
*/
Public Function Get_var ($key)
{
return Isset ($this->_ci_cached_vars[$key])? $this->_ci_cached_vars[$key]: NULL;
}
// --------------------------------------------------------------------
/**
* Load Helper
*/
Public Function Helper ($helpers = Array ())
{
foreach ($this->_ci_prep_filename ($helpers, ' _helper ') as $helper)
{
if (Isset ($this->_ci_helpers[$helper]))
{
Continue
}
$ext _helper = AppPath. ' helpers/'. Config_item (' Subclass_prefix '). $helper. PHP ';
If it's an extended helper,
if (file_exists ($ext _helper))
{
$base _helper = basepath. ' helpers/'. $helper. PHP ';
if (! file_exists ($base _helper))
{
Show_error (' Unable to load the requested file:helpers/'. $helper. ') php ');
}
Include_once ($ext _helper);
Include_once ($base _helper);
$this->_ci_helpers[$helper] = TRUE;
Log_message (' Debug ', ' Helper loaded: '. $helper);
Continue
}
If the helper is not loaded in the extended Helper,helper path
foreach ($this->_ci_helper_paths as $path)
{
if (file_exists $path. ' helpers/'. $helper. ') php '))
{
Include_once ($path. ' helpers/'. $helper. php ');
$this->_ci_helpers[$helper] = TRUE;
Log_message (' Debug ', ' Helper loaded: '. $helper);
Break
}
}
If the helper has not been loaded successfully, the load helper failed
if (! isset ($this->_ci_helpers[$helper])
{
Show_error (' Unable to load the requested file:helpers/'. $helper. ') php ');
}
}
}
// --------------------------------------------------------------------
/**
* Can see helpers call is also above helper, just helpers alias
*/
Public Function Helpers ($helpers = Array ())
{
$this->helper ($helpers);
}
// --------------------------------------------------------------------
/**
* Load Language file
*/
Public Function language ($file = Array (), $lang = ')
{
$CI =& get_instance ();
if (! Is_array ($file))
{
$file = Array ($file);
}
foreach ($file as $langfile)
{
$CI->lang->load ($langfile, $lang);
}
}
// --------------------------------------------------------------------
/**
* Load configuration file
*/
Public Function config ($file = ', $use _sections = False, $fail _gracefully = False)
{
$CI =& get_instance ();
$CI->config->load ($file, $use _sections, $fail _gracefully);
}
// --------------------------------------------------------------------
/**
* Driver
*
* Load Driver Library
*/
Public Function driver ($library = ', $params = null, $object _name = null)
{
if (! class_exists (' ci_driver_library '))
{
We aren ' t instantiating an object and that ' ll be done by the Library itself
Require basepath. ' libraries/driver.php ';
}
if ($library = = ")
{
return FALSE;
}
We can save the loader some time since Drivers'll *always* is in a subfolder,
And typically identically named to the library
if (! Strpos ($library, '/')
{
$library = Ucfirst ($library). ' /'. $library;
}
return $this->library ($library, $params, $object _name);
}
// --------------------------------------------------------------------
/**
* Add Package Path
*
* Add package path to library, model, assistant, configuration path
*/
Public Function Add_package_path ($path, $view _cascade=true)
{
$path = RTrim ($path, '/'). ' /';
Array_unshift ($this->_ci_library_paths, $path);
Array_unshift ($this->_ci_model_paths, $path);
Array_unshift ($this->_ci_helper_paths, $path);
$this->_ci_view_paths = Array ($path. ' views/' => $view _cascade) + $this->_ci_view_paths;
$config =& $this->_ci_get_component (' config ');
Array_unshift ($config->_config_paths, $path);
}
// --------------------------------------------------------------------
/**
* Get package Paths, default does not contain BasePath
*/
Public Function get_package_paths ($include _base = FALSE)
{
return $include _base = = TRUE? $this->_ci_library_paths: $this->_ci_model_paths;
}
// --------------------------------------------------------------------
/**
* Remove Package Path
*
* Remove a path from the library, model, and helper path arrays if it exists
* If no path is provided, the most recently added path is removed.
*
*/
Public Function Remove_package_path ($path = ', $remove _config_path = TRUE)
{
$config =& $this->_ci_get_component (' config ');
if ($path = = ")
{
$void = Array_shift ($this->_ci_library_paths);
$void = Array_shift ($this->_ci_model_paths);
$void = Array_shift ($this->_ci_helper_paths);
$void = Array_shift ($this->_ci_view_paths);
$void = Array_shift ($config->_config_paths);
}
Else
{
$path = RTrim ($path, '/'). ' /';
foreach (Array (' _ci_library_paths ', ' _ci_model_paths ', ' _ci_helper_paths ') as $var)
{
if (($key = Array_search ($path, $this->{$var}))!== FALSE)
{
unset ($this->{$var}[$key]);
}
}
if (Isset ($this->_ci_view_paths[$path. ' views/']))
{
unset ($this->_ci_view_paths[$path. ' views/']);
}
if (($key = Array_search ($path, $config->_config_paths))!== FALSE)
{
unset ($config->_config_paths[$key]);
}
}
Guaranteed application default path still exists
$this->_ci_library_paths = Array_unique (Array_merge ($this->_ci_library_paths, Array (AppPath, basepath)));
$this->_ci_helper_paths = Array_unique (Array_merge ($this->_ci_helper_paths, Array (AppPath, basepath)));
$this->_ci_model_paths = Array_unique (Array_merge ($this->_ci_model_paths, Array (AppPath)));
$this->_ci_view_paths = Array_merge ($this->_ci_view_paths, Array (apppath. ' views/' => TRUE));
$config->_config_paths = Array_unique (Array_merge ($config->_config_paths, Array (AppPath)));
}
// --------------------------------------------------------------------
/**
* Loader
*
* This function is used to load views and files.
* Variables are prefixed with _ci_ to avoid symbol collision with
* Variables made available to view files
*
* @param array
* @return void
*/
protected function _ci_load ($_ci_data)
{
Set The default data variables
foreach (Array (' _ci_view ', ' _ci_vars ', ' _ci_path ', ' _ci_return ') as $_ci_val)
{
$$_ci_val = (! isset ($_ci_data[$_ci_val))? FALSE: $_ci_data[$_ci_val];
}
$file _exists = FALSE;
If $_ci_path is not empty, the normal file is currently loaded. Loader::file will have a path.
if ($_ci_path!= ')
{
$_ci_x = explode ('/', $_ci_path);
$_ci_file = End ($_ci_x);
}
Else
{
$_ci_ext = PathInfo ($_ci_view, pathinfo_extension);
$_ci_file = ($_ci_ext = = ")? $_ci_view. '. php ': $_ci_view;
foreach ($this->_ci_view_paths as $view _file => $cascade)
{
if (file_exists ($view _file.$_ci_file))
{
$_ci_path = $view _file.$_ci_file;
$file _exists = TRUE;
Break
}
if (! $cascade)
{
Break
}
}
}
An error occurs if the view file does not exist
if (! $file _exists &&! file_exists ($_ci_path))
{
Show_error (' Unable to load the requested file: '. $_ci_file);
}
All the attributes of CI are passed to Loader,view $this refers to loader
$_ci_ci =& get_instance ();
foreach (Get_object_vars ($_ci_ci) as $_ci_key => $_ci_var)
{
if (! isset ($this->$_ci_key))
{
$this->$_ci_key =& $_ci_ci->$_ci_key;
}
}
/*
* Extract and cache variables
*
* You can either set variables using the dedicated $this->load_vars ()
* Function or via the second parameter of this function. We ' ll merge
* The two types and cache them so, that are embedded within
* Can have access to these variables.
*/
if (Is_array ($_ci_vars))
{
$this->_ci_cached_vars = Array_merge ($this->_ci_cached_vars, $_ci_vars);
}
Extract ($this->_ci_cached_vars);
/*
* Put the view content into the buffer
*
*/
Ob_start ();
Support for short labels
if ((bool) @ini_get (' short_open_tag ') = = = FALSE and Config_item (' rewrite_short_tags ') = = TRUE)
{
echo eval ('?> '. Preg_replace ("/;*\s*\?>/", ";?>", Str_replace (' <?= ', ' <?php echo ', file_get_contents ($ _ci_path)));
}
Else
{
Include ($_ci_path); Include () vs include_once () allows for multiple views with the same name
}
Log_message (' Debug ', ' File loaded: '. $_ci_path);
Whether to return view data directly
if ($_ci_return = = TRUE)
{
$buffer = Ob_get_contents ();
@ob_end_clean ();
return $buffer;
}
The current view file is introduced by another view file through the $this->view () method, which is the view file embedded in the view file
if (Ob_get_level () > $this->_ci_ob_level + 1)
{
Ob_end_flush ();
}
Else
{////the contents of the buffer to the output component and empties the shutdown buffer.
$_ci_ci->output->append_output (Ob_get_contents ());
@ob_end_clean ();
}
}
// --------------------------------------------------------------------
/**
* Load Class
*/
protected function _ci_load_class ($class, $params = null, $object _name = null)
{
Remove the. PHP and both ends/get $class is the class name or directory name + class name
$class = Str_replace ('. php ', ', ', trim ($class, '/'));
CI Allow dir/filename mode
$subdir = ';
if (($last _slash = Strrpos ($class, '/'))!== FALSE)
{
Directory
$subdir = substr ($class, 0, $last _slash + 1);
Filename
$class = substr ($class, $last _slash + 1);
}
Allow loaded class name first letter or full lowercase
foreach (Array (Ucfirst ($class), Strtolower ($class)) as $class)
{
$subclass = AppPath. ' libraries/'. $subdir. Config_item (' Subclass_prefix '). $class. PHP ';
Whether it is an extended class
if (file_exists ($subclass))
{
$baseclass = basepath. ' libraries/'. Ucfirst ($class). PHP ';
if (! file_exists ($baseclass))
{
Log_message (' Error ', "Unable to load the requested class:". $class);
Show_error ("Unable to load" requested class: ". $class);
}
Safety:was the class already loaded by a previous call?
if (In_array ($subclass, $this->_ci_loaded_files))
{
Before we deem this is a duplicate request, let ' s
If a custom object name is being supplied. If So, we ' ll
Return a new instance of the object
if (! Is_null ($object _name))
{
$CI =& get_instance ();
if (! isset ($CI-> $object _name))
{
return $this->_ci_init_class ($class, Config_item (' Subclass_prefix '), $params, $object _name);
}
}
$is _duplicate = TRUE;
Log_message (' Debug ', $class. "Class already loaded.) Second attempt ignored. ");
Return
}
Include_once ($baseclass);
Include_once ($subclass);
$this->_ci_loaded_files[] = $subclass;
Instantiating classes
return $this->_ci_init_class ($class, Config_item (' Subclass_prefix '), $params, $object _name);
}
If not expanded, similar to the above
$is _duplicate = FALSE;
foreach ($this->_ci_library_paths as $path)
{
$filepath = $path. ' libraries/'. $subdir. $class. PHP ';
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_loaded_files))
{
Before we deem this is a duplicate request, let ' s
If a custom object name is being supplied. If So, we ' ll
Return a new instance of the object
if (! Is_null ($object _name))
{
$CI =& get_instance ();
if (! isset ($CI-> $object _name))
{
return $this->_ci_init_class ($class, ', $params, $object _name);
}
}
$is _duplicate = TRUE;
Log_message (' Debug ', $class. "Class already loaded.) Second attempt ignored. ");
Return
}
Include_once ($filepath);
$this->_ci_loaded_files[] = $filepath;
return $this->_ci_init_class ($class, ', $params, $object _name);
}
}//End FOREACH
If the class has not been found, the final attempt is whether the class will be recorded in the subdirectory with the same name
if ($subdir = = ")
{
$path = Strtolower ($class). ' /'. $class;
return $this->_ci_load_class ($path, $params);
}
Load failed, error
if ($is _duplicate = = FALSE)
{
Log_message (' Error ', "Unable to load the requested class:". $class);
Show_error ("Unable to load" requested class: ". $class);
}
}
// --------------------------------------------------------------------
/**
* Instantiate a class that has already been loaded
*/
protected function _ci_init_class ($class, $prefix = ', $config = FALSE, $object _name = NULL)
{
Is there a configuration information for the class
if ($config = = NULL)
{
Fetch the config paths containing any package paths
$config _component = $this->_ci_get_component (' config ');
if (Is_array ($config _component->_config_paths))
{
Break on the found file, thus package files
are not overridden by default paths
foreach ($config _component->_config_paths as $path)
{
We test for both uppercase and lowercase, for servers that
are case-sensitive with regard to file names. Check for Environment
A, global next
if (defined (' Environment ') and File_exists ($path.) config/'. Environment. ' /'. Strtolower ($class). php '))
{
Include ($path. ' config/'. Environment. ' /'. Strtolower ($class). php ');
Break
}
ElseIf (defined (' Environment ') and File_exists ($path.) config/'. Environment. ' /'. Ucfirst (Strtolower ($class)). php '))
{
Include ($path. ' config/'. Environment. ' /'. Ucfirst (Strtolower ($class)). php ');
Break
}
ElseIf (file_exists, $path. ' config/'. Strtolower ($class). php '))
{
Include ($path. ' config/'. Strtolower ($class). php ');
Break
}
ElseIf (file_exists, $path. ' config/'. Ucfirst (Strtolower ($class)). php '))
{
Include ($path. ' config/'. Ucfirst (Strtolower ($class)). php ');
Break
}
}
}
}
if ($prefix = = ")
{//system the Library
if (class_exists (' Ci_ '. $class))
{
$name = ' ci_ '. $class;
}
ElseIf (class_exists) (Config_item (' Subclass_prefix '). $class))
{//Extended Library
$name = Config_item (' Subclass_prefix '). $class;
}
Else
{
$name = $class;
}
}
Else
{
$name = $prefix. $class;
}
is the class name valid?
if (! class_exists ($name))
{
Log_message (' Error ', "Non-existent class:". $name);
Show_error ("Non-existent class:". $class);
}
Set the variable name we'll assign the class to
was a custom class name supplied? If so we ' ll use it
$class = Strtolower ($class);
if (Is_null ($object _name))
{
$classvar = (! Isset ($this->_ci_varmap[$class))? $class: $this->_ci_varmap[$class];
}
Else
{
$classvar = $object _name;
}
Save the class name and object name
$this->_ci_classes[$class] = $classvar;
To give an instance of the initialized class to the CI super handle
$CI =& get_instance ();
if ($config!== NULL)
{
$CI-> $classvar = new $name ($config);
}
Else
{
$CI-> $classvar = new $name;
}
}
// --------------------------------------------------------------------
/**
* Auto Loader
*
* autoload.php configuration of the automatic loading files are:
* | 1. Packages
| 2. Libraries
| 3. Helper files
| 4. Custom Config files
| 5. Language files
| 6. Models
*/
Private Function _ci_autoloader ()
{
if (defined (' Environment ') and file_exists (AppPath. ' config/'). Environment. ' /autoload.php '))
{
Include (AppPath. ' config/'. Environment. ' /autoload.php ');
}
Else
{
Include (AppPath. ' config/autoload.php ');
}
if (! isset ($autoload))
{
return FALSE;
}
Automatically load packages, which means adding package_path to Library,model,helper,config
if (Isset ($autoload [' Packages '])
{
foreach ($autoload [' packages '] as $package _path)
{
$this->add_package_path ($package _path);
}
}
Load config file
if (count ($autoload [' config ']) > 0)
{
$CI =& get_instance ();
foreach ($autoload [' config '] as $key => $val)
{
$CI->config->load ($val);
}
}
Loading helper and language
foreach (Array (' helper ', ' language ') as $type)
{
if (Isset ($autoload [$type]) and COUNT ($autoload [$type]) > 0)
{
$this-> $type ($autoload [$type]);
}
}
This seems to be compatible with previous versions of
if (! isset ($autoload [' Libraries ']) and isset ($autoload [' Core '])
{
$autoload [' libraries '] = $autoload [' Core '];
}
Load libraries
if (Isset ($autoload [' Libraries ']) and count ($autoload [' Libraries ']) > 0)
{
Load DB
if (In_array (' database ', $autoload [' Libraries '])
{
$this->database ();
$autoload [' libraries '] = Array_diff ($autoload [' Libraries '], array (' database '));
}
Load all other libraries
foreach ($autoload [' libraries '] as $item)
{
$this->library ($item);
}
}
AutoLoad Models
if (Isset ($autoload [' model '])
{
$this->model ($autoload [' model ']);
}
}
// --------------------------------------------------------------------
/**
* Returns an associative array consisting of object properties
*/
protected function _ci_object_to_array ($object)
{
Return (Is_object ($object))? Get_object_vars ($object): $object;
}
// --------------------------------------------------------------------
/**
* Get an instance of a component of CI
*/
protected function &_ci_get_component ($component)
{
$CI =& get_instance ();
return $CI-> $component;
}
// --------------------------------------------------------------------
/**
* Process file name, this function is to return the correct file name
*/
protected function _ci_prep_filename ($filename, $extension)
{
if (! Is_array ($filename))
{
Return Array (Strtolower str_replace ('. php ', ', ', Str_replace ($extension, ', $filename)). $extension));
}
Else
{
foreach ($filename as $key => $val)
{
$filename [$key] = Strtolower (Str_replace ('. php ', ', ', Str_replace ($extension, ', $val)). $extension);
}
return $filename;
}
}
}