Loader.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2006, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.0
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * Loader Class
00020  *
00021  * Loads views and files
00022  *
00023  * @package             CodeIgniter
00024  * @subpackage  Libraries
00025  * @author              ExpressionEngine Dev Team
00026  * @category    Loader
00027  * @link                http://codeigniter.com/user_guide/libraries/loader.html
00028  */
00029 class CI_Loader {
00030 
00031         // All these are set automatically. Don't mess with them.
00032         var $_ci_ob_level;
00033         var $_ci_view_path              = '';
00034         var $_ci_is_php5                = FALSE;
00035         var $_ci_is_instance    = FALSE; // Whether we should use $this or $CI =& get_instance()
00036         var $_ci_cached_vars    = array();
00037         var $_ci_classes                = array();
00038         var $_ci_models                 = array();
00039         var $_ci_helpers                = array();
00040         var $_ci_plugins                = array();
00041         var $_ci_scripts                = array();
00042         var $_ci_varmap                 = array('unit_test' => 'unit', 'user_agent' => 'agent');
00043         
00044 
00045         /**
00046          * Constructor
00047          *
00048          * Sets the path to the view files and gets the initial output buffering level
00049          *
00050          * @access      public
00051          */
00052         function CI_Loader()
00053         {       
00054                 $this->_ci_is_php5 = (floor(phpversion()) >= 5) ? TRUE : FALSE;
00055                 $this->_ci_view_path = APPPATH.'views/';
00056                 $this->_ci_ob_level  = ob_get_level();
00057                                 
00058                 log_message('debug', "Loader Class Initialized");
00059         }
00060         
00061         // --------------------------------------------------------------------
00062         
00063         /**
00064          * Class Loader
00065          *
00066          * This function lets users load and instantiate classes.
00067          * It is designed to be called from a user's app controllers.
00068          *
00069          * @access      public
00070          * @param       string  the name of the class
00071          * @param       mixed   the optional parameters
00072          * @return      void
00073          */     
00074         function library($library = '', $params = NULL)
00075         {               
00076                 if ($library == '')
00077                 {
00078                         return FALSE;
00079                 }
00080 
00081                 if (is_array($library))
00082                 {
00083                         foreach ($library as $class)
00084                         {
00085                                 $this->_ci_load_class($class, $params);
00086                         }
00087                 }
00088                 else
00089                 {
00090                         $this->_ci_load_class($library, $params);
00091                 }
00092                 
00093                 $this->_ci_assign_to_models();
00094         }
00095 
00096         // --------------------------------------------------------------------
00097         
00098         /**
00099          * Model Loader
00100          *
00101          * This function lets users load and instantiate models.
00102          *
00103          * @param       string  the name of the class
00104          * @param       string  name for the model
00105          * @param       bool    database connection
00106          * @return      void
00107          */     
00108         function model($model, $name = '', $db_conn = FALSE)
00109         {               
00110                 if (is_array($model))
00111                 {
00112                         foreach($model as $babe)
00113                         {
00114                                 $this->model($babe);    
00115                         }
00116                         return;
00117                 }
00118 
00119                 if ($model == '')
00120                 {
00121                         return;
00122                 }
00123         
00124                 // Is the model in a sub-folder? If so, parse out the filename and path.
00125                 if (strpos($model, '/') === FALSE)
00126                 {
00127                         $path = '';
00128                 }
00129                 else
00130                 {
00131                         $x = explode('/', $model);
00132                         $model = end($x);                       
00133                         unset($x[count($x)-1]);
00134                         $path = implode('/', $x).'/';
00135                 }
00136         
00137                 if ($name == '')
00138                 {
00139                         $name = $model;
00140                 }
00141                 
00142                 if (in_array($name, $this->_ci_models, TRUE))
00143                 {
00144                         return;
00145                 }
00146                 
00147                 $CI =& get_instance();
00148                 if (isset($CI->$name))
00149                 {
00150                         show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
00151                 }
00152         
00153                 $model = strtolower($model);
00154                 
00155                 if ( ! file_exists(APPPATH.'models/'.$path.$model.EXT))
00156                 {
00157                         show_error('Unable to locate the model you have specified: '.$model);
00158                 }
00159                                 
00160                 if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
00161                 {
00162                         if ($db_conn === TRUE)
00163                                 $db_conn = '';
00164                 
00165                         $CI->load->database($db_conn, FALSE, TRUE);
00166                 }
00167         
00168                 if ( ! class_exists('Model'))
00169                 {
00170                         load_class('Model', FALSE);
00171                 }
00172 
00173                 require_once(APPPATH.'models/'.$path.$model.EXT);
00174 
00175                 $model = ucfirst($model);
00176                                 
00177                 $CI->$name = new $model();
00178                 $CI->$name->_assign_libraries();
00179                 
00180                 $this->_ci_models[] = $name;    
00181         }
00182                 
00183         // --------------------------------------------------------------------
00184         
00185         /**
00186          * Database Loader
00187          *
00188          * @access      public
00189          * @param       string  the DB credentials
00190          * @param       bool    whether to return the DB object
00191          * @param       bool    whether to enable active record (this allows us to override the config setting)
00192          * @return      object
00193          */     
00194         function database($params = '', $return = FALSE, $active_record = FALSE)
00195         {
00196                 // Grab the super object
00197                 $CI =& get_instance();
00198                 
00199                 // Do we even need to load the database class?
00200                 if (class_exists('CI_DB') AND $return == FALSE AND $active_record == FALSE AND isset($CI->db) AND is_object($CI->db))
00201                 {
00202                         return FALSE;
00203                 }       
00204         
00205                 require_once(BASEPATH.'database/DB'.EXT);
00206 
00207                 if ($return === TRUE)
00208                 {
00209                         return DB($params, $active_record);
00210                 }
00211                 
00212                 // Initialize the db variable.  Needed to prevent   
00213                 // reference errors with some configurations
00214                 $CI->db = '';
00215                 
00216                 // Load the DB class
00217                 $CI->db =& DB($params, $active_record); 
00218                 
00219                 // Assign the DB object to any existing models
00220                 $this->_ci_assign_to_models();
00221         }
00222         
00223         // --------------------------------------------------------------------
00224 
00225         /**
00226          * Load the Utilities Class
00227          *
00228          * @access      public
00229          * @return      string          
00230          */             
00231         function dbutil()
00232         {
00233                 if ( ! class_exists('CI_DB'))
00234                 {
00235                         $this->database();
00236                 }
00237                 
00238                 $CI =& get_instance();
00239 
00240                 // for backwards compatibility, load dbforge so we can extend dbutils off it
00241                 // this use is deprecated and strongly discouraged
00242                 $CI->load->dbforge();
00243         
00244                 require_once(BASEPATH.'database/DB_utility'.EXT);
00245                 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility'.EXT);
00246                 $class = 'CI_DB_'.$CI->db->dbdriver.'_utility';
00247 
00248                 $CI->dbutil =& new $class();
00249 
00250                 $CI->load->_ci_assign_to_models();
00251         }
00252         
00253         // --------------------------------------------------------------------
00254 
00255         /**
00256          * Load the Database Forge Class
00257          *
00258          * @access      public
00259          * @return      string          
00260          */             
00261         function dbforge()
00262         {
00263                 if ( ! class_exists('CI_DB'))
00264                 {
00265                         $this->database();
00266                 }
00267                 
00268                 $CI =& get_instance();
00269         
00270                 require_once(BASEPATH.'database/DB_forge'.EXT);
00271                 require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_forge'.EXT);
00272                 $class = 'CI_DB_'.$CI->db->dbdriver.'_forge';
00273 
00274                 $CI->dbforge = new $class();
00275                 
00276                 $CI->load->_ci_assign_to_models();
00277         }
00278         
00279         // --------------------------------------------------------------------
00280         
00281         /**
00282          * Load View
00283          *
00284          * This function is used to load a "view" file.  It has three parameters:
00285          *
00286          * 1. The name of the "view" file to be included.
00287          * 2. An associative array of data to be extracted for use in the view.
00288          * 3. TRUE/FALSE - whether to return the data or load it.  In
00289          * some cases it's advantageous to be able to return data so that
00290          * a developer can process it in some way.
00291          *
00292          * @access      public
00293          * @param       string
00294          * @param       array
00295          * @param       bool
00296          * @return      void
00297          */
00298         function view($view, $vars = array(), $return = FALSE)
00299         {
00300                 return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
00301         }
00302         
00303         // --------------------------------------------------------------------
00304         
00305         /**
00306          * Load File
00307          *
00308          * This is a generic file loader
00309          *
00310          * @access      public
00311          * @param       string
00312          * @param       bool
00313          * @return      string
00314          */
00315         function file($path, $return = FALSE)
00316         {
00317                 return $this->_ci_load(array('_ci_path' => $path, '_ci_return' => $return));
00318         }
00319         
00320         // --------------------------------------------------------------------
00321         
00322         /**
00323          * Set Variables
00324          *
00325          * Once variables are set they become available within
00326          * the controller class and its "view" files.
00327          *
00328          * @access      public
00329          * @param       array
00330          * @return      void
00331          */
00332         function vars($vars = array())
00333         {
00334                 $vars = $this->_ci_object_to_array($vars);
00335         
00336                 if (is_array($vars) AND count($vars) > 0)
00337                 {
00338                         foreach ($vars as $key => $val)
00339                         {
00340                                 $this->_ci_cached_vars[$key] = $val;
00341                         }
00342                 }
00343         }
00344         
00345         // --------------------------------------------------------------------
00346         
00347         /**
00348          * Load Helper
00349          *
00350          * This function loads the specified helper file.
00351          *
00352          * @access      public
00353          * @param       mixed
00354          * @return      void
00355          */
00356         function helper($helpers = array())
00357         {
00358                 if ( ! is_array($helpers))
00359                 {
00360                         $helpers = array($helpers);
00361                 }
00362         
00363                 foreach ($helpers as $helper)
00364                 {               
00365                         $helper = strtolower(str_replace(EXT, '', str_replace('_helper', '', $helper)).'_helper');
00366                 
00367                         if (isset($this->_ci_helpers[$helper]))
00368                         {
00369                                 continue;
00370                         }
00371                         
00372                         $ext_helper = APPPATH.'helpers/'.config_item('subclass_prefix').$helper.EXT;
00373 
00374                         // Is this a helper extension request?                  
00375                         if (file_exists($ext_helper))
00376                         {
00377                                 $base_helper = BASEPATH.'helpers/'.$helper.EXT;
00378                                 
00379                                 if ( ! file_exists($base_helper))
00380                                 {
00381                                         show_error('Unable to load the requested file: helpers/'.$helper.EXT);
00382                                 }
00383                                 
00384                                 include_once($ext_helper);
00385                                 include_once($base_helper);
00386                         }
00387                         elseif (file_exists(APPPATH.'helpers/'.$helper.EXT))
00388                         { 
00389                                 include_once(APPPATH.'helpers/'.$helper.EXT);
00390                         }
00391                         else
00392                         {               
00393                                 if (file_exists(BASEPATH.'helpers/'.$helper.EXT))
00394                                 {
00395                                         include_once(BASEPATH.'helpers/'.$helper.EXT);
00396                                 }
00397                                 else
00398                                 {
00399                                         show_error('Unable to load the requested file: helpers/'.$helper.EXT);
00400                                 }
00401                         }
00402 
00403                         $this->_ci_helpers[$helper] = TRUE;
00404                         
00405                 }
00406                 
00407                 log_message('debug', 'Helpers loaded: '.implode(', ', $helpers));
00408         }
00409         
00410         // --------------------------------------------------------------------
00411         
00412         /**
00413          * Load Helpers
00414          *
00415          * This is simply an alias to the above function in case the
00416          * user has written the plural form of this function.
00417          *
00418          * @access      public
00419          * @param       array
00420          * @return      void
00421          */
00422         function helpers($helpers = array())
00423         {
00424                 $this->helper($helpers);
00425         }
00426         
00427         // --------------------------------------------------------------------
00428         
00429         /**
00430          * Load Plugin
00431          *
00432          * This function loads the specified plugin.
00433          *
00434          * @access      public
00435          * @param       array
00436          * @return      void
00437          */
00438         function plugin($plugins = array())
00439         {
00440                 if ( ! is_array($plugins))
00441                 {
00442                         $plugins = array($plugins);
00443                 }
00444         
00445                 foreach ($plugins as $plugin)
00446                 {       
00447                         $plugin = strtolower(str_replace(EXT, '', str_replace('_pi', '', $plugin)).'_pi');              
00448 
00449                         if (isset($this->_ci_plugins[$plugin]))
00450                         {
00451                                 continue;
00452                         }
00453 
00454                         if (file_exists(APPPATH.'plugins/'.$plugin.EXT))
00455                         {
00456                                 include_once(APPPATH.'plugins/'.$plugin.EXT);   
00457                         }
00458                         else
00459                         {
00460                                 if (file_exists(BASEPATH.'plugins/'.$plugin.EXT))
00461                                 {
00462                                         include_once(BASEPATH.'plugins/'.$plugin.EXT);  
00463                                 }
00464                                 else
00465                                 {
00466                                         show_error('Unable to load the requested file: plugins/'.$plugin.EXT);
00467                                 }
00468                         }
00469                         
00470                         $this->_ci_plugins[$plugin] = TRUE;
00471                 }
00472                 
00473                 log_message('debug', 'Plugins loaded: '.implode(', ', $plugins));
00474         }
00475 
00476         // --------------------------------------------------------------------
00477         
00478         /**
00479          * Load Plugins
00480          *
00481          * This is simply an alias to the above function in case the
00482          * user has written the plural form of this function.
00483          *
00484          * @access      public
00485          * @param       array
00486          * @return      void
00487          */
00488         function plugins($plugins = array())
00489         {
00490                 $this->plugin($plugins);
00491         }
00492 
00493         // --------------------------------------------------------------------
00494         
00495         /**
00496          * Load Script
00497          *
00498          * This function loads the specified include file from the
00499          * application/scripts/ folder.
00500          *
00501          * NOTE:  This feature has been deprecated but it will remain available
00502          * for legacy users.
00503          *
00504          * @access      public
00505          * @param       array
00506          * @return      void
00507          */
00508         function script($scripts = array())
00509         {
00510                 if ( ! is_array($scripts))
00511                 {
00512                         $scripts = array($scripts);
00513                 }
00514         
00515                 foreach ($scripts as $script)
00516                 {       
00517                         $script = strtolower(str_replace(EXT, '', $script));
00518 
00519                         if (isset($this->_ci_scripts[$script]))
00520                         {
00521                                 continue;
00522                         }
00523                 
00524                         if ( ! file_exists(APPPATH.'scripts/'.$script.EXT))
00525                         {
00526                                 show_error('Unable to load the requested script: scripts/'.$script.EXT);
00527                         }
00528                         
00529                         include_once(APPPATH.'scripts/'.$script.EXT);
00530                 }
00531                 
00532                 log_message('debug', 'Scripts loaded: '.implode(', ', $scripts));
00533         }
00534                 
00535         // --------------------------------------------------------------------
00536         
00537         /**
00538          * Loads a language file
00539          *
00540          * @access      public
00541          * @param       array
00542          * @param       string
00543          * @return      void
00544          */
00545         function language($file = array(), $lang = '')
00546         {
00547                 $CI =& get_instance();
00548 
00549                 if ( ! is_array($file))
00550                 {
00551                         $file = array($file);
00552                 }
00553 
00554                 foreach ($file as $langfile)
00555                 {       
00556                         $CI->lang->load($langfile, $lang);
00557                 }
00558         }
00559 
00560         /**
00561          * Loads language files for scaffolding
00562          *
00563          * @access      public
00564          * @param       string
00565          * @return      arra
00566          */
00567         function scaffold_language($file = '', $lang = '', $return = FALSE)
00568         {
00569                 $CI =& get_instance();
00570                 return $CI->lang->load($file, $lang, $return);
00571         }
00572         
00573         // --------------------------------------------------------------------
00574         
00575         /**
00576          * Loads a config file
00577          *
00578          * @access      public
00579          * @param       string
00580          * @return      void
00581          */
00582         function config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
00583         {                       
00584                 $CI =& get_instance();
00585                 $CI->config->load($file, $use_sections, $fail_gracefully);
00586         }
00587 
00588         // --------------------------------------------------------------------
00589         
00590         /**
00591          * Scaffolding Loader
00592          *
00593          * This initializing function works a bit different than the
00594          * others. It doesn't load the class.  Instead, it simply
00595          * sets a flag indicating that scaffolding is allowed to be
00596          * used.  The actual scaffolding function below is
00597          * called by the front controller based on whether the
00598          * second segment of the URL matches the "secret" scaffolding
00599          * word stored in the application/config/routes.php
00600          *
00601          * @access      public
00602          * @param       string
00603          * @return      void
00604          */     
00605         function scaffolding($table = '')
00606         {               
00607                 if ($table === FALSE)
00608                 {
00609                         show_error('You must include the name of the table you would like to access when you initialize scaffolding');
00610                 }
00611                 
00612                 $CI =& get_instance();
00613                 $CI->_ci_scaffolding = TRUE;
00614                 $CI->_ci_scaff_table = $table;
00615         }
00616 
00617         // --------------------------------------------------------------------
00618                 
00619         /**
00620          * Loader
00621          *
00622          * This function is used to load views and files.
00623          * Variables are prefixed with _ci_ to avoid symbol collision with
00624          * variables made available to view files
00625          *
00626          * @access      private
00627          * @param       array
00628          * @return      void
00629          */
00630         function _ci_load($_ci_data)
00631         {
00632                 // Set the default data variables
00633                 foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
00634                 {
00635                         $$_ci_val = ( ! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val];
00636                 }
00637 
00638                 // Set the path to the requested file
00639                 if ($_ci_path == '')
00640                 {
00641                         $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
00642                         $_ci_file = ($_ci_ext == '') ? $_ci_view.EXT : $_ci_view;
00643                         $_ci_path = $this->_ci_view_path.$_ci_file;
00644                 }
00645                 else
00646                 {
00647                         $_ci_x = explode('/', $_ci_path);
00648                         $_ci_file = end($_ci_x);
00649                 }
00650                 
00651                 if ( ! file_exists($_ci_path))
00652                 {
00653                         show_error('Unable to load the requested file: '.$_ci_file);
00654                 }
00655         
00656                 // This allows anything loaded using $this->load (views, files, etc.)
00657                 // to become accessible from within the Controller and Model functions.
00658                 // Only needed when running PHP 5
00659                 
00660                 if ($this->_ci_is_instance())
00661                 {
00662                         $_ci_CI =& get_instance();
00663                         foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
00664                         {
00665                                 if ( ! isset($this->$_ci_key))
00666                                 {
00667                                         $this->$_ci_key =& $_ci_CI->$_ci_key;
00668                                 }
00669                         }
00670                 }
00671 
00672                 /*
00673                  * Extract and cache variables
00674                  *
00675                  * You can either set variables using the dedicated $this->load_vars()
00676                  * function or via the second parameter of this function. We'll merge
00677                  * the two types and cache them so that views that are embedded within
00678                  * other views can have access to these variables.
00679                  */     
00680                 if (is_array($_ci_vars))
00681                 {
00682                         $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
00683                 }
00684                 extract($this->_ci_cached_vars);
00685                                 
00686                 /*
00687                  * Buffer the output
00688                  *
00689                  * We buffer the output for two reasons:
00690                  * 1. Speed. You get a significant speed boost.
00691                  * 2. So that the final rendered template can be
00692                  * post-processed by the output class.  Why do we
00693                  * need post processing?  For one thing, in order to
00694                  * show the elapsed page load time.  Unless we
00695                  * can intercept the content right before it's sent to
00696                  * the browser and then stop the timer it won't be accurate.
00697                  */
00698                 ob_start();
00699                                 
00700                 // If the PHP installation does not support short tags we'll
00701                 // do a little string replacement, changing the short tags
00702                 // to standard PHP echo statements.
00703                 
00704                 if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
00705                 {
00706                         echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
00707                 }
00708                 else
00709                 {
00710                         include($_ci_path); // include() vs include_once() allows for multiple views with the same name
00711                 }
00712                 
00713                 log_message('debug', 'File loaded: '.$_ci_path);
00714                 
00715                 // Return the file data if requested
00716                 if ($_ci_return === TRUE)
00717                 {               
00718                         $buffer = ob_get_contents();
00719                         @ob_end_clean();
00720                         return $buffer;
00721                 }
00722 
00723                 /*
00724                  * Flush the buffer... or buff the flusher?
00725                  *
00726                  * In order to permit views to be nested within
00727                  * other views, we need to flush the content back out whenever
00728                  * we are beyond the first level of output buffering so that
00729                  * it can be seen and included properly by the first included
00730                  * template and any subsequent ones. Oy!
00731                  *
00732                  */     
00733                 if (ob_get_level() > $this->_ci_ob_level + 1)
00734                 {
00735                         ob_end_flush();
00736                 }
00737                 else
00738                 {
00739                         // PHP 4 requires that we use a global
00740                         global $OUT;
00741                         $OUT->append_output(ob_get_contents());
00742                         @ob_end_clean();
00743                 }
00744         }
00745 
00746         // --------------------------------------------------------------------
00747 
00748         /**
00749          * Load class
00750          *
00751          * This function loads the requested class.
00752          *
00753          * @access      private
00754          * @param       string  the item that is being loaded
00755          * @param       mixed   any additional parameters
00756          * @return      void
00757          */
00758         function _ci_load_class($class, $params = NULL)
00759         {       
00760                 // Get the class name
00761                 $class = str_replace(EXT, '', $class);
00762 
00763                 // We'll test for both lowercase and capitalized versions of the file name
00764                 foreach (array(ucfirst($class), strtolower($class)) as $class)
00765                 {
00766                         $subclass = APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT;
00767 
00768                         // Is this a class extension request?                   
00769                         if (file_exists($subclass))
00770                         {
00771                                 $baseclass = BASEPATH.'libraries/'.ucfirst($class).EXT;
00772                                 
00773                                 if ( ! file_exists($baseclass))
00774                                 {
00775                                         log_message('error', "Unable to load the requested class: ".$class);
00776                                         show_error("Unable to load the requested class: ".$class);
00777                                 }
00778 
00779                                 // Safety:  Was the class already loaded by a previous call?
00780                                 if (in_array($subclass, $this->_ci_classes))
00781                                 {
00782                                         $is_duplicate = TRUE;
00783                                         log_message('debug', $class." class already loaded. Second attempt ignored.");
00784                                         return;
00785                                 }
00786         
00787                                 include_once($baseclass);                               
00788                                 include_once($subclass);
00789                                 $this->_ci_classes[] = $subclass;
00790         
00791                                 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params);                  
00792                         }
00793                 
00794                         // Lets search for the requested library file and load it.
00795                         $is_duplicate = FALSE;          
00796                         for ($i = 1; $i < 3; $i++)
00797                         {
00798                                 $path = ($i % 2) ? APPPATH : BASEPATH;  
00799                                 $filepath = $path.'libraries/'.$class.EXT;
00800                                 
00801                                 // Does the file exist?  No?  Bummer...
00802                                 if ( ! file_exists($filepath))
00803                                 {
00804                                         continue;
00805                                 }
00806                                 
00807                                 // Safety:  Was the class already loaded by a previous call?
00808                                 if (in_array($filepath, $this->_ci_classes))
00809                                 {
00810                                         $is_duplicate = TRUE;
00811                                         log_message('debug', $class." class already loaded. Second attempt ignored.");
00812                                         return;
00813                                 }
00814                                 
00815                                 include_once($filepath);
00816                                 $this->_ci_classes[] = $filepath;
00817                                 return $this->_ci_init_class($class, '', $params);
00818                         }
00819                 } // END FOREACH
00820                 
00821                 // If we got this far we were unable to find the requested class.
00822                 // We do not issue errors if the load call failed due to a duplicate request
00823                 if ($is_duplicate == FALSE)
00824                 {
00825                         log_message('error', "Unable to load the requested class: ".$class);
00826                         show_error("Unable to load the requested class: ".$class);
00827                 }
00828         }
00829         
00830         // --------------------------------------------------------------------
00831 
00832         /**
00833          * Instantiates a class
00834          *
00835          * @access      private
00836          * @param       string
00837          * @param       string
00838          * @return      null
00839          */
00840         function _ci_init_class($class, $prefix = '', $config = FALSE)
00841         {       
00842                 $class = strtolower($class);
00843                 
00844                 // Is there an associated config file for this class?
00845                 if ($config === NULL)
00846                 {
00847                         if (file_exists(APPPATH.'config/'.$class.EXT))
00848                         {
00849                                 include_once(APPPATH.'config/'.$class.EXT);
00850                         }
00851                 }
00852                 
00853                 if ($prefix == '')
00854                 {
00855                         $name = (class_exists('CI_'.$class)) ? 'CI_'.$class : $class;
00856                 }
00857                 else
00858                 {
00859                         $name = $prefix.$class;
00860                 }
00861                 
00862                 // Set the variable name we will assign the class to    
00863                 $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
00864                                 
00865                 // Instantiate the class                
00866                 $CI =& get_instance();
00867                 if ($config !== NULL)
00868                 {
00869                         $CI->$classvar = new $name($config);
00870                 }
00871                 else
00872                 {               
00873                         $CI->$classvar = new $name;
00874                 }       
00875         }       
00876         
00877         // --------------------------------------------------------------------
00878         
00879         /**
00880          * Autoloader
00881          *
00882          * The config/autoload.php file contains an array that permits sub-systems,
00883          * libraries, plugins, and helpers to be loaded automatically.
00884          *
00885          * @access      private
00886          * @param       array
00887          * @return      void
00888          */
00889         function _ci_autoloader()
00890         {       
00891                 include_once(APPPATH.'config/autoload'.EXT);
00892                 
00893                 if ( ! isset($autoload))
00894                 {
00895                         return FALSE;
00896                 }
00897                 
00898                 // Load any custom config file
00899                 if (count($autoload['config']) > 0)
00900                 {                       
00901                         $CI =& get_instance();
00902                         foreach ($autoload['config'] as $key => $val)
00903                         {
00904                                 $CI->config->load($val);
00905                         }
00906                 }               
00907 
00908                 // Autoload plugins, helpers and languages
00909                 foreach (array('helper', 'plugin', 'language') as $type)
00910                 {                       
00911                         if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
00912                         {
00913                                 $this->$type($autoload[$type]);
00914                         }               
00915                 }
00916 
00917 
00918 
00919                 // A little tweak to remain backward compatible
00920                 // The $autoload['core'] item was deprecated
00921                 if ( ! isset($autoload['libraries']))
00922                 {
00923                         $autoload['libraries'] = $autoload['core'];
00924                 }
00925                 
00926                 // Load libraries
00927                 if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
00928                 {
00929                         // Load the database driver.
00930                         if (in_array('database', $autoload['libraries']))
00931                         {
00932                                 $this->database();
00933                                 $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
00934                         }
00935 
00936                         // Load scaffolding
00937                         if (in_array('scaffolding', $autoload['libraries']))
00938                         {
00939                                 $this->scaffolding();
00940                                 $autoload['libraries'] = array_diff($autoload['libraries'], array('scaffolding'));
00941                         }
00942                 
00943                         // Load all other libraries
00944                         foreach ($autoload['libraries'] as $item)
00945                         {
00946                                 $this->library($item);
00947                         }
00948                 }               
00949 
00950                 // Autoload models
00951                 if (isset($autoload['model']))
00952                 {
00953                         $this->model($autoload['model']);
00954                 }
00955 
00956         }
00957         
00958         // --------------------------------------------------------------------
00959 
00960         /**
00961          * Assign to Models
00962          *
00963          * Makes sure that anything loaded by the loader class (libraries, plugins, etc.)
00964          * will be available to models, if any exist.
00965          *
00966          * @access      private
00967          * @param       object
00968          * @return      array
00969          */
00970         function _ci_assign_to_models()
00971         {
00972                 if (count($this->_ci_models) == 0)
00973                 {
00974                         return;
00975                 }
00976         
00977                 if ($this->_ci_is_instance())
00978                 {
00979                         $CI =& get_instance();
00980                         foreach ($this->_ci_models as $model)
00981                         {                       
00982                                 $CI->$model->_assign_libraries();
00983                         }
00984                 }
00985                 else
00986                 {               
00987                         foreach ($this->_ci_models as $model)
00988                         {                       
00989                                 $this->$model->_assign_libraries();
00990                         }
00991                 }
00992         }       
00993 
00994         // --------------------------------------------------------------------
00995 
00996         /**
00997          * Object to Array
00998          *
00999          * Takes an object as input and converts the class variables to array key/vals
01000          *
01001          * @access      private
01002          * @param       object
01003          * @return      array
01004          */
01005         function _ci_object_to_array($object)
01006         {
01007                 return (is_object($object)) ? get_object_vars($object) : $object;
01008         }
01009 
01010         // --------------------------------------------------------------------
01011 
01012         /**
01013          * Determines whether we should use the CI instance or $this
01014          *
01015          * @access      private
01016          * @return      bool
01017          */
01018         function _ci_is_instance()
01019         {
01020                 if ($this->_ci_is_php5 == TRUE)
01021                 {
01022                         return TRUE;
01023                 }
01024         
01025                 global $CI;
01026                 return (is_object($CI)) ? TRUE : FALSE;
01027         }
01028         
01029 }
01030 
01031 /* End of file Loader.php */
01032 /* Location: ./system/libraries/Loader.php */