Skip to content

Instantly share code, notes, and snippets.

@philsturgeon
Created November 26, 2009 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save philsturgeon/243409 to your computer and use it in GitHub Desktop.
Save philsturgeon/243409 to your computer and use it in GitHub Desktop.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 4.3.2 or newer
*
* @package CodeIgniter
* @author ExpressionEngine Dev Team
* @copyright Copyright (c) 2008, EllisLab, Inc.
* @license http://codeigniter.com/user_guide/license.html
* @link http://codeigniter.com
* @since Version 1.0
* @filesource
*/
define('SHAREPATH', 'C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/codeigniter/shared/');
// ------------------------------------------------------------------------
/**
* Loader Class
*
* Loads views and files
*
* @package CodeIgniter
* @subpackage Libraries
* @author ExpressionEngine Dev Team
* @category Loader
* @link http://codeigniter.com/user_guide/libraries/loader.html
*/
class MY_Loader extends CI_Loader {
/**
* Model Loader
*
* This function lets users load and instantiate models.
*
* @access public
* @param string the name of the class
* @param string name for the model
* @param bool database connection
* @return void
*/
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);
// PJS Shared change
//if ( ! file_exists(APPPATH.'models/'.$path.$model.EXT))
$app_model = APPPATH.'models/'.$path.$model.EXT;
$share_model = SHAREPATH.'models/'.$path.$model.EXT;
if ( (! $app_model_exists = file_exists($app_model)) &&
(! $share_model_exists = file_exists($share_model)) )
{
show_error('Unable to locate the model you have specified: '.$model);
}
// end PJS Shared change
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);
}
// PJS Shared change
if($app_model_exists)
{
require_once($app_model);
}
else
{
require_once($share_model);
}
//require_once(APPPATH.'models/'.$path.$model.EXT);
// end PJS Shared change
$model = ucfirst($model);
$CI->$name = new $model();
$CI->$name->_assign_libraries();
$this->_ci_models[] = $name;
}
/**
* Load class
*
* This function loads the requested class.
*
* @access private
* @param string the item that is being loaded
* @param mixed any additional parameters
* @param string an optional object name
* @return void
*/
function _ci_load_class($class, $params = NULL, $object_name = NULL)
{
// Get the class name, and while we're at it trim any slashes.
// The directory path can be included as part of the class name,
// but we don't want a leading slash
$class = str_replace(EXT, '', trim($class, '/'));
// Was the path included with the class name?
// We look for a slash to determine this
$subdir = '';
if (strpos($class, '/') !== FALSE)
{
// explode the path so we can separate the filename from the path
$x = explode('/', $class);
// Reset the $class variable now that we know the actual filename
$class = end($x);
// Kill the filename from the array
unset($x[count($x)-1]);
// Glue the path back together, sans filename
$subdir = implode($x, '/').'/';
}
// We'll test for both lowercase and capitalized versions of the file name
foreach (array(ucfirst($class), strtolower($class)) as $class)
{
$subclass = APPPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.EXT;
// PJS Shared change
if( ! file_exists($subclass) )
{
$subclass = SHAREPATH.'libraries/'.$subdir.config_item('subclass_prefix').$class.EXT;
}
// end PJS Shared change
// Is this a class extension request?
if (file_exists($subclass))
{
$baseclass = BASEPATH.'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_loaded_files))
{
// Before we deem this to be a duplicate request, let's see
// 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;
return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
}
// Lets search for the requested library file and load it.
$is_duplicate = FALSE;
for ($i = 1; $i <= 3; $i++)
{
// PJS Shared change
switch( $i )
{
case 1:
$path = APPPATH;
break;
case 2:
$path = SHAREPATH;
break;
case 3:
$path = BASEPATH;
break;
}
//$path = ($i % 2) ? APPPATH : BASEPATH;
// PJS Shared change
$filepath = $path.'libraries/'.$subdir.$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_loaded_files))
{
// Before we deem this to be a duplicate request, let's see
// 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
// One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
if ($subdir == '')
{
$path = strtolower($class).'/'.$class;
return $this->_ci_load_class($path, $params);
}
// 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);
}
}
// --------------------------------------------------------------------
}
/* End of file Loader.php */
/* Location: ./system/libraries/Loader.php */
@helmut
Copy link

helmut commented Jun 25, 2010

Does this load helpers too?

@philsturgeon
Copy link
Author

I wouldn't bother, you can use "Application Packages" in CodeIgniter 2.0 to do this which supports models, libraries, helpers, config, etc.

It's always great when the EllisLab guys make my little hacks obsolete. :-D

@helmut
Copy link

helmut commented Jun 26, 2010

Hmmm... not much use to me now. how far off is that do you reckon?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment