Guest User

CodeIgniter MY_Loader class

a guest
Apr 7th, 2014
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.10 KB | None | 0 0
  1. <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. class MY_Loader extends CI_Loader {
  4.  
  5.     private $_CI;
  6.  
  7.     /**
  8.      * Constructor
  9.      */
  10.     public function __construct()
  11.     {
  12.         parent::__construct();
  13.         $this->_CI =& get_instance();
  14.         set_error_handler(array($this, '_error_handler'));
  15.         spl_autoload_register(array($this, '_class_autoloader'));
  16.         $this->class_file('Custom_exception', CORE_PATH);
  17.     }
  18.  
  19.     /**
  20.      * Custom error handler.
  21.      *
  22.      * @param $severity  PHP error code.
  23.      * @param $message  Text of error.
  24.      * @param $filename  File error occurred in.
  25.      * @param $line  Line number of error.
  26.      * @return void
  27.      * @throws ErrorException
  28.      * @access private
  29.      */
  30.     private function _error_handler($severity, $message, $filename, $line)
  31.     {
  32.         throw new ErrorException($message, 0, $severity, $filename, $line);
  33.     }
  34.  
  35.     /**
  36.      * @param $class_name  The class name.
  37.      * @return void
  38.      * @access private
  39.      */
  40.     private function _class_autoloader($class_name)
  41.     {
  42.         if (strpos($class_name, $this->_CI->config->item('native_prefix')) === FALSE AND
  43.             strpos($class_name, $this->_CI->config->item('subclass_prefix')) === FALSE AND
  44.             strpos($class_name, 'PEAR') === FALSE)
  45.         {
  46.             $this->class_file($class_name, NULL, 3);
  47.         }
  48.     }
  49.  
  50.     /**
  51.      * @param $class_name  Class name.
  52.      * @param $file_path  File path.
  53.      * @return void
  54.      */
  55.     public function class_file($class_name, $file_path = NULL, $backtrace_level = 1)
  56.     {
  57.         if (is_null($file_path))
  58.         {
  59.             $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $backtrace_level);
  60.             $backtrace = array_pop($backtrace);
  61.             $full_path = dirname($backtrace['file']) . '/' . $class_name . '.php';
  62.         }
  63.         else
  64.         {
  65.             $full_path = $file_path . $class_name . '.php';
  66.         }
  67.        
  68.         if (in_array($full_path, $this->_ci_loaded_files))
  69.         {
  70.             log_message('debug', $class_name . ' class already loaded. Second attempt ignored.');
  71.             return;
  72.         }
  73.  
  74.         try
  75.         {
  76.             include_once $full_path;
  77.         }
  78.         catch (ErrorException $e)
  79.         {
  80.             $log = 'Unable to load the requested class: ' . $class_name;
  81.             if (ENVIRONMENT === 'development')
  82.             {
  83.                 $log .= '<br><b> Full Trace: </b>' . str_replace('#', '<br>#', $e->getTraceAsString());
  84.             }
  85.  
  86.             log_message('error', $log);
  87.             show_error($log);
  88.         }
  89.  
  90.         if ( ! isset($e))
  91.         {
  92.             $this->_ci_loaded_files[] = $full_path;
  93.         }
  94.     }
  95.  
  96.     /**
  97.      * Clean all cached variables.
  98.      *
  99.      * @return void
  100.      */
  101.     public function clean_cached_vars()
  102.     {
  103.         // sets this instance variable to its default value
  104.         $this->_ci_cached_vars = array();
  105.     }
  106. }
  107.  
  108. // END MY_Loader class
  109.  
  110. /* End of file MY_Loader.php */
  111. /* Location: ./application/core/MY_Loader.php */
Advertisement
Add Comment
Please, Sign In to add comment