Share Pastebin
Guest
Public paste!

Extended CI Loader

By: a guest | Apr 8th, 2010 | Syntax: PHP | Size: 9.74 KB | Hits: 533 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. class EXT_Loader extends CI_Loader {
  4.        
  5.         private $apppath = NULL;
  6.        
  7.         function EXT_Loader()
  8.         {      
  9.                 parent::CI_Loader();
  10.                
  11.                 $this->apppath = APPPATH;
  12.         }
  13.  
  14.         function model($model, $name = '', $db_conn = FALSE, $apppath = NULL)
  15.         {              
  16.                 if (is_array($model))
  17.                 {
  18.                         foreach($model as $alias => $babe)
  19.                         {
  20.                                 $alias = is_numeric($alias) ? '' : $alias;
  21.                                 $this->model($babe, $alias);
  22.                         }
  23.                         return;
  24.                 }
  25.  
  26.                 if ($model == '')
  27.                 {
  28.                         return;
  29.                 }
  30.        
  31.                 // Is the model in a sub-folder? If so, parse out the filename and path.
  32.                 if (strpos($model, '/') === FALSE)
  33.                 {
  34.                         $path = '';
  35.                 }
  36.                 else
  37.                 {
  38.                         $x = explode('/', $model);
  39.                         $model = end($x);                      
  40.                         unset($x[count($x)-1]);
  41.                         $path = implode('/', $x).'/';
  42.                 }
  43.        
  44.                 if ($name == '')
  45.                 {
  46.                         $name = $model;
  47.                 }
  48.                
  49.                 if (in_array($name, $this->_ci_models, TRUE))
  50.                 {
  51.                         return;
  52.                 }
  53.                
  54.                 $CI =& get_instance();
  55.                 if (isset($CI->$name))
  56.                 {
  57.                         show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
  58.                 }
  59.        
  60.                 $model = strtolower($model);
  61.                
  62.                 /* added line for dynamic app path */
  63.                 if($apppath !== NULL){
  64.                         $this->apppath = $apppath;
  65.                 }
  66.                
  67.                 // changed var below for dynamic app path
  68.                 if ( ! file_exists($this->apppath.'models/'.$path.$model.EXT))
  69.                 {
  70.                         show_error('Unable to locate the model you have specified: '.$model);
  71.                 }
  72.                                
  73.                 if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
  74.                 {
  75.                         if ($db_conn === TRUE)
  76.                                 $db_conn = '';
  77.                
  78.                         $CI->load->database($db_conn, FALSE, TRUE);
  79.                 }
  80.        
  81.                 if ( ! class_exists('Model'))
  82.                 {
  83.                         load_class('Model', FALSE);
  84.                 }
  85.  
  86.                 require_once($this->apppath.'models/'.$path.$model.EXT);
  87.  
  88.                 $model = ucfirst($model);
  89.                                
  90.                 $CI->$name = new $model();
  91.                 $CI->$name->_assign_libraries();
  92.                
  93.                 $this->_ci_models[] = $name;   
  94.         }
  95.        
  96.        
  97.         function library($library = '', $params = NULL, $object_name = NULL, $apppath = NULL)
  98.         {
  99.                 if ($library == '')
  100.                 {
  101.                         return FALSE;
  102.                 }
  103.  
  104.                 if ( ! is_null($params) AND ! is_array($params))
  105.                 {
  106.                         $params = NULL;
  107.                 }
  108.                
  109.                 if($apppath !== NULL){
  110.                         $this->apppath = $apppath;
  111.                 }
  112.  
  113.                 if (is_array($library))
  114.                 {
  115.                         foreach ($library as $class)
  116.                         {
  117.                                 $this->_ci_load_class($class, $params, $object_name);
  118.                         }
  119.                 }
  120.                 else
  121.                 {
  122.                         $this->_ci_load_class($library, $params, $object_name);
  123.                 }
  124.                
  125.                 $this->_ci_assign_to_models();
  126.         }
  127.        
  128.        
  129.         function _ci_load_class($class, $params = NULL, $object_name = NULL)
  130.         {      
  131.                 // Get the class name, and while we're at it trim any slashes.  
  132.                 // The directory path can be included as part of the class name,
  133.                 // but we don't want a leading slash
  134.                 $class = str_replace(EXT, '', trim($class, '/'));
  135.        
  136.                 // Was the path included with the class name?
  137.                 // We look for a slash to determine this
  138.                 $subdir = '';
  139.                 if (strpos($class, '/') !== FALSE)
  140.                 {
  141.                         // explode the path so we can separate the filename from the path
  142.                         $x = explode('/', $class);     
  143.                        
  144.                         // Reset the $class variable now that we know the actual filename
  145.                         $class = end($x);
  146.                        
  147.                         // Kill the filename from the array
  148.                         unset($x[count($x)-1]);
  149.                        
  150.                         // Glue the path back together, sans filename
  151.                         $subdir = implode($x, '/').'/';
  152.                 }
  153.                
  154.                 // We'll test for both lowercase and capitalized versions of the file name
  155.                 foreach (array(ucfirst($class), strtolower($class)) as $class)
  156.                 {
  157.                        
  158.                         $subclass = $this->apppath.'libraries/'.$subdir.config_item('subclass_prefix').$class.EXT;
  159.  
  160.                         // Is this a class extension request?                  
  161.                         if (file_exists($subclass))
  162.                         {
  163.                                 $baseclass = BASEPATH.'libraries/'.ucfirst($class).EXT;
  164.                                
  165.                                 if ( ! file_exists($baseclass))
  166.                                 {
  167.                                         log_message('error', "Unable to load the requested class: ".$class);
  168.                                         show_error("Unable to load the requested class: ".$class);
  169.                                 }
  170.  
  171.                                 // Safety:  Was the class already loaded by a previous call?
  172.                                 if (in_array($subclass, $this->_ci_loaded_files))
  173.                                 {
  174.                                         // Before we deem this to be a duplicate request, let's see
  175.                                         // if a custom object name is being supplied.  If so, we'll
  176.                                         // return a new instance of the object
  177.                                         if ( ! is_null($object_name))
  178.                                         {
  179.                                                 $CI =& get_instance();
  180.                                                 if ( ! isset($CI->$object_name))
  181.                                                 {
  182.                                                         return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);                   
  183.                                                 }
  184.                                         }
  185.                                        
  186.                                         $is_duplicate = TRUE;
  187.                                         log_message('debug', $class." class already loaded. Second attempt ignored.");
  188.                                         return;
  189.                                 }
  190.        
  191.                                 include_once($baseclass);                              
  192.                                 include_once($subclass);
  193.                                 $this->_ci_loaded_files[] = $subclass;
  194.        
  195.                                 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);                   
  196.                         }
  197.                
  198.                         // Lets search for the requested library file and load it.
  199.                         $is_duplicate = FALSE;         
  200.                         for ($i = 1; $i < 3; $i++)
  201.                         {
  202.                                 $path = ($i % 2) ? $this->apppath : BASEPATH;  
  203.                                 $filepath = $path.'libraries/'.$subdir.$class.EXT;
  204.                                
  205.                                 // Does the file exist?  No?  Bummer...
  206.                                 if ( ! file_exists($filepath))
  207.                                 {
  208.                                         continue;
  209.                                 }
  210.                                
  211.                                 // Safety:  Was the class already loaded by a previous call?
  212.                                 if (in_array($filepath, $this->_ci_loaded_files))
  213.                                 {
  214.                                         // Before we deem this to be a duplicate request, let's see
  215.                                         // if a custom object name is being supplied.  If so, we'll
  216.                                         // return a new instance of the object
  217.                                         if ( ! is_null($object_name))
  218.                                         {
  219.                                                 $CI =& get_instance();
  220.                                                 if ( ! isset($CI->$object_name))
  221.                                                 {
  222.                                                         return $this->_ci_init_class($class, '', $params, $object_name);
  223.                                                 }
  224.                                         }
  225.                                
  226.                                         $is_duplicate = TRUE;
  227.                                         log_message('debug', $class." class already loaded. Second attempt ignored.");
  228.                                         return;
  229.                                 }
  230.                                
  231.                                 include_once($filepath);
  232.                                 $this->_ci_loaded_files[] = $filepath;
  233.                                 return $this->_ci_init_class($class, '', $params, $object_name);
  234.                         }
  235.                 } // END FOREACH
  236.  
  237.                 // One last attempt.  Maybe the library is in a subdirectory, but it wasn't specified?
  238.                 if ($subdir == '')
  239.                 {
  240.                         $path = strtolower($class).'/'.$class;
  241.                         return $this->_ci_load_class($path, $params);
  242.                 }
  243.                
  244.                 // If we got this far we were unable to find the requested class.
  245.                 // We do not issue errors if the load call failed due to a duplicate request
  246.                 if ($is_duplicate == FALSE)
  247.                 {
  248.                         log_message('error', "Unable to load the requested class: ".$class);
  249.                         show_error("Unable to load the requested class: ".$class);
  250.                 }
  251.         }
  252.        
  253.        
  254.         function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
  255.         {      
  256.                 // Is there an associated config file for this class?
  257.                 if ($config === NULL)
  258.                 {
  259.                         // We test for both uppercase and lowercase, for servers that
  260.                         // are case-sensitive with regard to file names
  261.                         if (file_exists($this->apppath.'config/'.strtolower($class).EXT))
  262.                         {
  263.                                 include_once($this->apppath.'config/'.strtolower($class).EXT);
  264.                         }                      
  265.                         elseif (file_exists($this->apppath.'config/'.ucfirst(strtolower($class)).EXT))
  266.                         {
  267.                                 include_once($this->apppath.'config/'.ucfirst(strtolower($class)).EXT);
  268.                         }
  269.                 }
  270.                
  271.                 if ($prefix == '')
  272.                 {                      
  273.                         if (class_exists('CI_'.$class))
  274.                         {
  275.                                 $name = 'CI_'.$class;
  276.                         }
  277.                         elseif (class_exists(config_item('subclass_prefix').$class))
  278.                         {
  279.                                 $name = config_item('subclass_prefix').$class;
  280.                         }
  281.                         else
  282.                         {
  283.                                 $name = $class;
  284.                         }
  285.                 }
  286.                 else
  287.                 {
  288.                         $name = $prefix.$class;
  289.                 }
  290.                
  291.                 // Is the class name valid?
  292.                 if ( ! class_exists($name))
  293.                 {
  294.                         log_message('error', "Non-existent class: ".$name);
  295.                         show_error("Non-existent class: ".$class);
  296.                 }
  297.                
  298.                 // Set the variable name we will assign the class to
  299.                 // Was a custom class name supplied?  If so we'll use it
  300.                 $class = strtolower($class);
  301.                
  302.                 if (is_null($object_name))
  303.                 {
  304.                         $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
  305.                 }
  306.                 else
  307.                 {
  308.                         $classvar = $object_name;
  309.                 }
  310.  
  311.                 // Save the class name and object name         
  312.                 $this->_ci_classes[$class] = $classvar;
  313.  
  314.                 // Instantiate the class               
  315.                 $CI =& get_instance();
  316.                 if ($config !== NULL)
  317.                 {
  318.                         $CI->$classvar = new $name($config);
  319.                 }
  320.                 else
  321.                 {
  322.                         $CI->$classvar = new $name;
  323.                 }
  324.         }
  325.        
  326.        
  327.         function _ci_autoloader()
  328.         {      
  329.                 include_once(APPPATH.'config/autoload'.EXT);
  330.                
  331.                 if ( ! isset($autoload))
  332.                 {
  333.                         return FALSE;
  334.                 }
  335.                
  336.                 // Load any custom config file
  337.                 if (count($autoload['config']) > 0)
  338.                 {                      
  339.                         $CI =& get_instance();
  340.                         foreach ($autoload['config'] as $key => $val)
  341.                         {
  342.                                 $CI->config->load($val);
  343.                         }
  344.                 }              
  345.  
  346.                 // Autoload plugins, helpers and languages
  347.                 foreach (array('helper', 'plugin', 'language') as $type)
  348.                 {                      
  349.                         if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
  350.                         {
  351.                                 $this->$type($autoload[$type]);
  352.                         }              
  353.                 }
  354.  
  355.                 // A little tweak to remain backward compatible
  356.                 // The $autoload['core'] item was deprecated
  357.                 if ( ! isset($autoload['libraries']))
  358.                 {
  359.                         $autoload['libraries'] = $autoload['core'];
  360.                 }
  361.                
  362.                 // Load libraries
  363.                 if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
  364.                 {
  365.                         // Load the database driver.
  366.                         if (in_array('database', $autoload['libraries']))
  367.                         {
  368.                                 $this->database();
  369.                                 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
  370.                         }
  371.  
  372.                         // Load scaffolding
  373.                         if (in_array('scaffolding', $autoload['libraries']))
  374.                         {
  375.                                 $this->scaffolding();
  376.                                 $autoload['libraries'] = array_diff($autoload['libraries'], array('scaffolding'));
  377.                         }
  378.                
  379.                         // Load all other libraries
  380.                         foreach ($autoload['libraries'] as $alias => $item)
  381.                         {
  382.                                 $alias = is_numeric($alias) ? NULL : $alias;
  383.                                 $this->library($item, NULL, $alias);
  384.                         }
  385.                 }              
  386.  
  387.                 // Autoload models
  388.                 if (isset($autoload['model']))
  389.                 {
  390.                         $this->model($autoload['model']);
  391.                 }
  392.  
  393.         }
  394. }