Browse files

Refactored for unit testing

  • Loading branch information...
1 parent fe8e491 commit 53c52a191cf7d11ff3e46e7a2ecad2ae30fd86c0 @dchill42 committed Aug 30, 2012
Showing with 536 additions and 462 deletions.
  1. +5 −5 bootstrap.txt
  2. +19 −1 index.php
  3. +475 −262 system/core/CodeIgniter.php
  4. +37 −194 system/core/Common.php
View
10 bootstrap.txt
@@ -53,25 +53,25 @@ REFACTOR
index.php:
define paths
include CodeIgniter.php
- create instance
+ create instance (pass APPPATH)
run instance
CodeIgniter.php:
define version
declare app core
declare instance function
- include Common.php(?)
+ include Common.php
CodeIgniter::instance():
- load config.php
- assign config
+ load config.php (from path param if given)
+ assign config (from assign param if given)
load subclass
instantiate class
CodeIgniter::__construct()
set exception handler
kill magic quotes
- set path arrays
+ set path arrays (from constants)
CodeIgniter::run()
Flag running
View
20 index.php
@@ -141,7 +141,6 @@
// The controller function you wish to be called.
// $routing['function'] = '';
-
/*
* -------------------------------------------------------------------
* CUSTOM CONFIG VALUES
@@ -276,12 +275,31 @@
/*
* --------------------------------------------------------------------
+ * Set empty override arguments if not defined above
+ * --------------------------------------------------------------------
+ */
+ // Check config overrides
+ if ( ! isset($assign_to_config))
+ {
+ $assign_to_config = array();
+ }
+
+ // Check routing overrides
+ if ( ! isset($routing))
+ {
+ $routing = array();
+ }
+
+/*
+ * --------------------------------------------------------------------
* LOAD THE BOOTSTRAP FILE
* --------------------------------------------------------------------
*
* And away we go...
*/
require_once BASEPATH.'core/CodeIgniter.php';
+$CI =& CodeIgniter::instance($assign_to_config);
+$CI->run($routing);
/* End of file index.php */
/* Location: ./index.php */
View
737 system/core/CodeIgniter.php
@@ -26,9 +26,16 @@
*/
/**
+ * CodeIgniter Version
+ *
+ * @var string
+ */
+define('CI_VERSION', '3.0-dev');
+
+/**
* CodeIgniter Application Core Class
*
- * This class object is the super class that every library in
+ * This class object is the super class that every object in
* CodeIgniter will be assigned to.
*
* @package CodeIgniter
@@ -41,60 +48,290 @@ class CodeIgniter {
/**
* CodeIgniter singleton instance
*
- * @var object
- * @access private
+ * @access protected
+ * @var object
+ */
+ protected static $instance = NULL;
+
+ /**
+ * Main config loaded during instantiation
+ *
+ * @var array
+ */
+ public $_main_config = array();
+
+ /**
+ * Paths for loading core classes
+ *
+ * @access protected
+ * @var array
+ */
+ protected $_core_paths = array();
+
+ /**
+ * Paths for loading core class extensions
+ *
+ * @access protected
+ * @var array
+ */
+ protected $_ext_paths = array();
+
+ /**
+ * Log threshold
+ *
+ * @access protected
+ * @var int
+ */
+ protected $_log_threshold = 0;
+
+ /**
+ * Subclass prefix for core class extensions
+ *
+ * @access protected
+ * @var string
+ */
+ protected $_subclass_prefix = '';
+
+ /**
+ * Is running flag to prevent run() reentry
+ *
+ * @access protected
+ * @var bool
*/
- private static $instance = NULL;
+ protected $_is_running = FALSE;
/**
* Constructor
+ *
+ * This constructor is protected in order to force instantiation
+ * through instance(), employing a singleton pattern.
+ *
+ * @access protected
*/
- private function __construct()
+ protected function __construct()
{
+ // Set log threshold
+ $this->_log_threshold = isset($this->_main_config['log_threshold']) ?
+ $this->_main_config['log_threshold'] : 0;
+
+ // Set subclass prefix
+ $this->_subclass_prefix = isset($this->_main_config['subclass_prefix']) ?
+ $this->_main_config['subclass_prefix'] : '';
+
+ // Set core class paths
+ $this->_core_paths = array(APPPATH, BASEPATH);
+ $this->_ext_paths = array(APPPATH);
+ // TODO: Consider checking autoload for package paths to add
+
// Define a custom error handler so we can log PHP errors
- set_error_handler('_exception_handler');
+ // This must come after the config items and paths above,
+ // because the exception handler relies on those elements.
+ set_error_handler(array($this, '_exception_handler'));
// Kill magic quotes for older versions
if ( ! is_php('5.4'))
{
@ini_set('magic_quotes_runtime', 0);
}
- log_message('debug', 'CodeIgniter Class Initialized');
+ $this->log_message('debug', 'CodeIgniter Class Initialized');
+ }
+
+ /**
+ * Destructor
+ */
+ public function __destruct()
+ {
+ // Finalize run and send output
+ $this->_finalize();
}
/**
- * Initialize
+ * Get instance
*
- * This function handles loading Config and Loader, which are special.
- * Loading them requires $instance to be set, so we can't do it in the ctor above.
+ * Returns singleton instance of core object
+ * Upon initial instantiation, this function bootstraps the system by
+ * loading the main config file and its own extension class (if available).
+ * All other config files are loaded via CI_Config.
+ * All other core classes are loaded via load_core_class().
+ * All other loadables are loaded via CI_Loader.
+ * The second parameter supports overriding the APPPATH constant in unit testing.
*
- * @return void
+ * @param array Config overrides
+ * @param string Application path override
+ * @return object
*/
- protected function _init()
+ public static function &instance($assign_to_config = NULL, $apppath = NULL)
{
- // Get Config and load constants
- $this->load_core_class('Config');
- $this->config->get('constants.php', NULL);
+ // Check for existing instance
+ if (is_null(self::$instance))
+ {
+ // Determine application path
+ if ( ! $apppath)
+ {
+ $apppath = APPPATH;
+ }
+
+ // Load main config
+ $path = $apppath.'config/';
+ if (defined('ENVIRONMENT') && file_exists($path.ENVIRONMENT.'/config.php'))
+ {
+ // Use ENVIRONMENT config
+ include($path.ENVIRONMENT.'/config.php');
+ }
+ else if (file_exists($path.'config.php'))
+ {
+ // Use regular config
+ include($path.'config.php');
+ }
+ else
+ {
+ // Can't run without config - error out
+ set_status_header(503);
+ exit('The configuration file does not exist.');
+ }
+
+ // Does the $config array exist?
+ if ( ! isset($config) || ! is_array($config))
+ {
+ set_status_header(503);
+ exit('Your config file does not appear to be formatted correctly.');
+ }
- // Load Loader
- $this->load =& load_class('Loader', 'core');
+ // Are any values being dynamically replaced?
+ if (count($assign_to_config) > 0)
+ {
+ foreach ($assign_to_config as $key => $val)
+ {
+ if (isset($config[$key]))
+ {
+ $config[$key] = $val;
+ }
+ }
+ }
+
+ // Load the CodeIgniter subclass, if found
+ $class = 'CodeIgniter';
+ $pre = isset($config['subclass_prefix']) ? $config['subclass_prefix'] : '';
+ $file = $apppath.'core/'.$pre.$class.'.php';
+ if (file_exists($file))
+ {
+ include($file);
+ $class = $pre.$class;
+ }
+
+ // Instantiate object as subclass if defined, otherwise as base name
+ self::$instance = new $class();
+ self::$instance->_main_config = $config;
+ //self::$instance->_init();
+ }
+ return self::$instance;
}
/**
- * Load core class
+ * Load a core class
*
- * Loads a core class and registers it with core object
+ * Loads and registers a core class
+ * This function must not call log_message or show_error in order
+ * to avoid infinite recursion.
*
- * @param string class name
- * @return object
+ * @param string Class name
+ * @param string Object name
+ * @return void
*/
- public function load_core_class($class)
+ public function load_core_class($class, $obj_name = '')
{
- // Load class, immediately assign, and return object
- $name = strtolower($class);
- $this->$name =& load_class($class, 'core');
- return $this->$name;
+ // Determine name
+ if ($obj_name === '')
+ {
+ $obj_name = strtolower($class);
+ }
+
+ // Check if already loaded
+ if (isset($this->$obj_name))
+ {
+ return;
+ }
+
+ // Check if class exists
+ $name = 'CI_'.$class;
+ if ( ! class_exists($name))
+ {
+ // Look for file in core paths
+ foreach ($this->_core_paths as $path)
+ {
+ $file = $path.'core/'.$class.'.php';
+ if (file_exists($file))
+ {
+ include($file);
+ break;
+ }
+ }
+
+ // Make sure class is loaded
+ if ( ! class_exists($name))
+ {
+ set_status_header(503);
+ exit('Unable to locate the specified class: '.$class.'.php');
+ }
+ }
+
+ // Check for class extension
+ $ext = $this->_subclass_prefix.$class;
+ if (class_exists($ext))
+ {
+ // Instantiate extension class instead
+ $name = $ext;
+ }
+ else
+ {
+ // Look for file in extension paths
+ foreach ($this->_ext_paths as $path)
+ {
+ $file = $path.'core/'.$this->_subclass_prefix.$class.'.php';
+ if (file_exists($file))
+ {
+ include($file);
+
+ // See if extension class exists now
+ if (class_exists($ext))
+ {
+ // Instantiate extension class instead
+ $name = $ext;
+ break;
+ }
+ }
+ }
+ }
+
+ // Load class
+ $this->$obj_name = new $name();
+ }
+
+ /**
+ * Log an error message
+ *
+ * @param string Error level
+ * @param string Error message
+ * @param bool PHP error flag
+ * @return void
+ */
+ public function log_message($level = 'error', $message, $php_error = FALSE)
+ {
+ // Check threshold
+ if ($this->_log_threshold === 0)
+ {
+ return;
+ }
+
+ // Check for log class
+ if ( ! isset($this->log))
+ {
+ $this->load_core_class('log');
+ }
+
+ // Write message
+ $this->log->write_log($level, $message, $php_error);
}
/**
@@ -163,291 +400,267 @@ public function call_controller($class, $method, array $args = array(), $name =
}
/**
- * Get instance
+ * Run the CodeIgniter application
*
- * Returns singleton instance of core object
- *
- * @return object
+ * @param array Routing overrides
+ * @return void
*/
- public static function &instance()
+ public function run($routing = NULL)
{
- // Check for existing instance
- if (is_null(self::$instance))
+ // Check running flag
+ if ($this->_is_running)
{
- // Load the CodeIgniter subclass, if found
- $class = 'CodeIgniter';
- $pre = config_item('subclass_prefix');
- $file = APPPATH.'core/'.$pre.$class.'.php';
- if (file_exists($file))
- {
- include($file);
- $class = $pre.$class;
- }
-
- // Instantiate object as subclass if defined, otherwise as base name
- self::$instance = new $class();
- self::$instance->_init();
+ return;
}
- return self::$instance;
- }
-}
-// ------------------------------------------------------------------------
+ // Set running flag
+ $this->_is_running = TRUE;
-/**
- * System Initialization File
- *
- * Loads the base classes and executes the request.
- *
- * @package CodeIgniter
- * @subpackage codeigniter
- * @category Front-controller
- * @author ExpressionEngine Dev Team
- * @link http://codeigniter.com/user_guide/
- */
+ // Load the base classes
+ $this->_load_base();
-/**
- * CodeIgniter Version
- *
- * @var string
- *
- */
- define('CI_VERSION', '3.0-dev');
+ // Load the routing and support classes
+ // We will exit from here if a cache is found
+ $this->_load_routing($routing);
-/*
- * ------------------------------------------------------
- * Load the global functions
- * ------------------------------------------------------
- */
- require(BASEPATH.'core/Common.php');
+ // Load the support classes
+ $this->_load_support();
-/*
- * ------------------------------------------------------
- * Set the subclass_prefix
- * ------------------------------------------------------
- *
- * Normally the "subclass_prefix" is set in the config file.
- * The subclass prefix allows CI to know if a core class is
- * being extended via a library in the local application
- * "libraries" folder. Since CI allows config items to be
- * overriden via data set in the main index. php file,
- * before proceeding we need to know if a subclass_prefix
- * override exists. If so, we will set this value now,
- * before any classes are loaded
- * Note: Since the config file data is cached it doesn't
- * hurt to load it here.
- */
- if ( ! empty($assign_to_config['subclass_prefix']))
- {
- get_config(array('subclass_prefix' => $assign_to_config['subclass_prefix']));
+ // Load and run the routed controller
+ $this->_run_controller();
+
+ // Clear running flag
+ $this->_is_running = FALSE;
}
-/*
- * ------------------------------------------------------
- * Load the application core
- * ------------------------------------------------------
- */
- // Instantiate CodeIgniter [DEPRECATED - call CodeIgniter::instance() directly]
- function &get_instance()
+ /**
+ * Load base classes
+ *
+ * This function loads the base-level core classes
+ * that lay the foundation for the rest of the core
+ * Config, Loader, Benchmark, and Hooks
+ *
+ * @access protected
+ * @return void
+ */
+ protected function _load_base()
{
- return CodeIgniter::instance();
- }
- $CI =& CodeIgniter::instance();
+ // Get Config and load constants
+ $this->load_core_class('Config');
+ $this->config->get('constants.php', NULL);
-/*
- * ------------------------------------------------------
- * Start the timer... tick tock tick tock...
- * ------------------------------------------------------
- */
- $CI->load_core_class('Benchmark');
- $CI->benchmark->mark('total_execution_time_start');
- $CI->benchmark->mark('loading_time:_base_classes_start');
+ // Load Loader as 'load'
+ $this->load_core_class('Loader', 'load');
-/*
- * ------------------------------------------------------
- * Do we have any manually set config items in the index.php file?
- * ------------------------------------------------------
- */
- if (isset($assign_to_config))
- {
- $CI->config->_assign_to_config($assign_to_config);
+ // Load Benchmark and start timer
+ $this->load_core_class('Benchmark');
+ $this->_benchmark->mark('total_execution_time_start');
+ $this->_benchmark->mark('loading_time:_base_classes_start');
+
+ // Load the hooks class and call pre_system
+ $this->load_core_class('Hooks');
+ $this->hooks->_call_hook('pre_system');
}
-/*
- * ------------------------------------------------------
- * Instantiate the hooks class
- * ------------------------------------------------------
- */
- $CI->load_core_class('Hooks');
+ /**
+ * Load routing classes
+ *
+ * This function loads the second level of core classes
+ * leading up to routing.
+ * UTF-8, Output, URI, and Routing
+ * If a cache is found, we output it and exit at the end of the call.
+ *
+ * @access protected
+ * @param array Routing overrides
+ * @return void
+ */
+ protected function _load_routing($routing = NULL)
+ {
+ // Load the UTF-8 class
+ // Note: Order here is rather important as the UTF-8 class needs to be used
+ // very early on, but it relies on Config
+ $this->load_core_class('Utf8');
-/*
- * ------------------------------------------------------
- * Is there a "pre_system" hook?
- * ------------------------------------------------------
- */
- $CI->hooks->_call_hook('pre_system');
+ // Load the output class
+ // Note: By load Output before Router, we ensure it is available to support
+ // 404 overrides in case of a call to show_404().
+ $this->load_core_class('Output');
-/*
- * ------------------------------------------------------
- * Instantiate the UTF-8 class
- * ------------------------------------------------------
- *
- * Note: Order here is rather important as the UTF-8
- * class needs to be used very early on, but it cannot
- * properly determine if UTF-8 can be supported until
- * after the Config class is instantiated.
- *
- */
- $CI->load_core_class('Utf8');
+ // Load the URI class
+ $this->load_core_class('URI');
-/*
- * ------------------------------------------------------
- * Instantiate the output class
- * ------------------------------------------------------
- *
- * Note: By instantiating Output before Router, we ensure
- * it is available to support 404 overrides in case of a
- * call to show_404().
- *
- */
- $CI->load_core_class('Output');
+ // Load the Router class and set routing
+ $this->load_core_class('Router');
+ $this->router->_set_routing();
-/*
- * ------------------------------------------------------
- * Instantiate the URI class
- * ------------------------------------------------------
- */
- $CI->load_core_class('URI');
+ // Set any routing overrides that may exist in the main index file
+ if ( ! empty($routing))
+ {
+ $this->router->_set_overrides($routing);
+ }
-/*
- * ------------------------------------------------------
- * Instantiate the routing class and set the routing
- * ------------------------------------------------------
- */
- $CI->load_core_class('Router');
- $CI->router->_set_routing();
+ // Is there a valid cache file? If so, we're done...
+ if ($this->hooks->_call_hook('cache_override') === FALSE && $this->output->_display_cache() === TRUE)
+ {
+ exit;
+ }
+ }
- // Set any routing overrides that may exist in the main index file
- if (isset($routing))
+ /**
+ * Load support classes
+ *
+ * This function loads the third level of core classes
+ * that offer support for the Controller.
+ * Security, Input, Language, and autoload resources
+ *
+ * @return void
+ */
+ protected function _load_support()
{
- $CI->router->_set_overrides($routing);
+ // Load the Security class
+ $this->load_core_class('Security');
+
+ // Load the Input class
+ $this->load_core_class('Input');
+
+ // Load the Language class
+ $this->load_core_class('Lang');
+
+ // Autoload libraries, etc.
+ $this->load->ci_autoloader();
+
+ // Mark end of core loading
+ $this->benchmark->mark('loading_time:_base_classes_end');
}
-/*
- * ------------------------------------------------------
- * Is there a valid cache file? If so, we're done...
- * ------------------------------------------------------
- */
- if ($CI->hooks->_call_hook('cache_override') === FALSE && $CI->output->_display_cache() === TRUE)
+ /**
+ * Load and run routed Controller
+ *
+ * @access protected
+ * @return void
+ */
+ protected function _run_controller()
{
- exit;
- }
+ // Call pre_controller hook
+ $this->_hooks->_call_hook('pre_controller');
-/*
- * -----------------------------------------------------
- * Load the security class for xss and csrf support
- * -----------------------------------------------------
- */
- $CI->load_core_class('Security');
+ // Get the parsed route and identify class, method, and arguments
+ $route = $this->router->fetch_route();
+ $args = array_slice($route, this_Router::SEG_CLASS);
+ $class = strtolower(array_shift($args));
+ $method = array_shift($args);
-/*
- * ------------------------------------------------------
- * Load the Input class and sanitize globals
- * ------------------------------------------------------
- */
- $CI->load_core_class('Input');
+ // Mark a start point so we can benchmark the controller
+ $this->benchmark->mark('controller_execution_time_( '.$class.' / '.$method.' )_start');
-/*
- * ------------------------------------------------------
- * Load the Language class
- * ------------------------------------------------------
- */
- $CI->load_core_class('Lang');
+ // Load the controller, but don't call the method yet
+ if ($this->load->controller($route, '', FALSE) == FALSE)
+ {
+ show_404($class.'/'.$method);
+ }
-/*
- * ------------------------------------------------------
- * Autoload libraries, etc.
- * ------------------------------------------------------
- */
- $CI->load->ci_autoloader();
+ // Set special "routed" reference to routed Controller
+ $this->routed = $this->$class;
- // Set a mark point for benchmarking
- $CI->benchmark->mark('loading_time:_base_classes_end');
+ // Call post_controller_constructor hook
+ $this->hooks->_call_hook('post_controller_constructor');
-/*
- * ------------------------------------------------------
- * Is there a "pre_controller" hook?
- * ------------------------------------------------------
- */
- $CI->hooks->_call_hook('pre_controller');
+ if ($this->call_controller($class, $method, $args) == FALSE)
+ {
+ // Both _remap and $method failed - go to 404
+ show_404($class.'/'.$method);
+ }
+ }
-/*
- * ------------------------------------------------------
- * Load the local controller
- * ------------------------------------------------------
- */
- // Get the parsed route and identify class, method, and arguments
- $route = $CI->router->fetch_route();
- $args = array_slice($route, CI_Router::SEG_CLASS);
- $class = strtolower(array_shift($args));
- $method = array_shift($args);
+ /**
+ * Finalize bencharks and hooks and send output
+ *
+ * @access protected
+ * @return void
+ */
+ protected function _finalize()
+ {
+ // Check for Benchmark class
+ if (isset($this->benchmark))
+ {
+ // Mark a benchmark end point
+ $this->benchmark->mark('controller_execution_time_( '.$class.' / '.$method.' )_end');
+ }
- // Mark a start point so we can benchmark the controller
- $CI->benchmark->mark('controller_execution_time_( '.$class.' / '.$method.' )_start');
+ // Check for Hooks class
+ if (isset($this->hooks))
+ {
+ // Call post_controller hook
+ $this->hooks->_call_hook('post_controller');
- // Load the controller, but don't call the method yet
- if ($CI->load->controller($route, '', FALSE) == FALSE)
- {
- show_404($class.'/'.$method);
+ // Send the final rendered output to the browser
+ if ($this->hooks->_call_hook('display_override') === FALSE && isset($this->output))
+ {
+ $this->output->_display();
+ }
+
+ // Call post_system hook
+ $this->hooks->_call_hook('post_system');
+ }
}
- // Set special "routed" reference to routed Controller
- $CI->routed = $CI->$class;
+ /**
+ * Exception Handler
+ *
+ * This custom exception handler permits PHP errors to be logged in our own
+ * log files since the user may not have access to server logs. Since this
+ * function effectively intercepts PHP errors, however, we also need to
+ * display errors based on the current error_reporting level.
+ * We do that with the use of a PHP error template.
+ *
+ * @param int Severity
+ * @param string Error message
+ * @param string File path
+ * @param int Line number
+ * @return void
+ */
+ public function _exception_handler($severity, $message, $filepath, $line)
+ {
+ // Load Exception class
+ $this->load_core_class('Exceptions');
-/*
- * ------------------------------------------------------
- * Is there a "post_controller_constructor" hook?
- * ------------------------------------------------------
- */
- $CI->hooks->_call_hook('post_controller_constructor');
+ // Should we display the error? We'll get the current error_reporting
+ // level and add its bits with the severity bits to find out.
+ // And respect display_errors
+ if (($severity & error_reporting()) === $severity && (bool) ini_get('display_errors') === TRUE)
+ {
+ $this->exceptions->show_php_error($severity, $message, $filepath, $line);
+ }
-/*
- * ------------------------------------------------------
- * Call the requested method
- * ------------------------------------------------------
- */
- if ($CI->call_controller($class, $method, $args) == FALSE)
- {
- // Both _remap and $method failed - go to 404
- show_404($class.'/'.$method);
- }
+ // Should we log the error? No? We're done...
+ if ($this->_log_threshold === 0)
+ {
+ return;
+ }
- // Mark a benchmark end point
- $CI->benchmark->mark('controller_execution_time_( '.$class.' / '.$method.' )_end');
+ $this->exceptions->log_exception($severity, $message, $filepath, $line);
+ }
+}
-/*
- * ------------------------------------------------------
- * Is there a "post_controller" hook?
- * ------------------------------------------------------
+/**
+ * Global function to get CodeIgniter instance
+ *
+ * DEPRECATED - call CodeIgniter::instance() directly
+ *
+ * @return object CodeIgniter instance
*/
- $CI->hooks->_call_hook('post_controller');
+function &get_instance()
+{
+ return CodeIgniter::instance();
+}
/*
* ------------------------------------------------------
- * Send the final rendered output to the browser
+ * Load the global functions
* ------------------------------------------------------
*/
- if ($CI->hooks->_call_hook('display_override') === FALSE)
- {
- $CI->output->_display();
- }
+require(BASEPATH.'core/Common.php');
-/*
- * ------------------------------------------------------
- * Is there a "post_system" hook?
- * ------------------------------------------------------
- */
- $CI->hooks->_call_hook('post_system');
/* End of file CodeIgniter.php */
/* Location: ./system/core/CodeIgniter.php */
View
231 system/core/Common.php
@@ -117,11 +117,11 @@ function is_really_writable($file)
if ( ! function_exists('load_class'))
{
/**
- * Class registry
+ * Load class
*
- * This function acts as a singleton. If the requested class does not
- * exist it is instantiated and set to a static variable. If it has
- * previously been instantiated the variable is returned.
+ * This function returns classes loaded in the core CodeIgniter object.
+ * DEPRECATED - get object from CodeIgniter::instance() directly
+ * Core classes are loaded internally, and all others should call CI_Loader.
*
* @param string the class name being requested
* @param string the directory where the class should be found
@@ -130,58 +130,18 @@ function is_really_writable($file)
*/
function &load_class($class, $directory = 'libraries', $prefix = 'CI_')
{
- static $_classes = array();
-
- // Does the class exist? If so, we're done...
- if (isset($_classes[$class]))
- {
- return $_classes[$class];
- }
-
- $name = FALSE;
-
- // Look for the class first in the local application/libraries folder
- // then in the native system/libraries folder
- foreach (array(APPPATH, BASEPATH) as $path)
+ // Get instance, lowercase class, and check
+ $CI = CodeIgniter::instance();
+ $class = strtolower($class);
+ if (isset($CI->$class))
{
- if (file_exists($path.$directory.'/'.$class.'.php'))
- {
- $name = $prefix.$class;
-
- if (class_exists($name) === FALSE)
- {
- require($path.$directory.'/'.$class.'.php');
- }
-
- break;
- }
+ // Return loaded class
+ return $CI->$class;
}
- // Is the request a class extension? If so we load it too
- if (file_exists(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php'))
- {
- $name = config_item('subclass_prefix').$class;
-
- if (class_exists($name) === FALSE)
- {
- require(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php');
- }
- }
-
- // Did we find the class?
- if ($name === FALSE)
- {
- // Note: We use exit() rather then show_error() in order to avoid a
- // self-referencing loop with the Exceptions class
- set_status_header(503);
- exit('Unable to locate the specified class: '.$class.'.php');
- }
-
- // Keep track of what we just loaded
- is_loaded($class);
-
- $_classes[$class] = new $name();
- return $_classes[$class];
+ // Failed - return NULL
+ $null = NULL;
+ return $null;
}
}
@@ -190,86 +150,19 @@ function &load_class($class, $directory = 'libraries', $prefix = 'CI_')
if ( ! function_exists('is_loaded'))
{
/**
- * Keeps track of which libraries have been loaded. This function is
- * called by the load_class() function above
+ * Checks if a class is loaded
+ *
+ * DEPRECATED - check object with CodeIgniter::instance() directly
*
* @param string
* @return array
*/
function &is_loaded($class = '')
{
- static $_is_loaded = array();
-
- if ($class !== '')
- {
- $_is_loaded[strtolower($class)] = $class;
- }
-
- return $_is_loaded;
- }
-}
-
-// ------------------------------------------------------------------------
-
-if ( ! function_exists('get_config'))
-{
- /**
- * Loads the main config.php file
- *
- * This function lets us grab the config file even if the Config class
- * hasn't been instantiated yet
- *
- * @param array
- * @return array
- */
- function &get_config($replace = array())
- {
- static $_config;
-
- if (isset($_config))
- {
- return $_config[0];
- }
-
- $file_path = APPPATH.'config/config.php';
- $found = FALSE;
- if (file_exists($file_path))
- {
- $found = TRUE;
- require($file_path);
- }
-
- // Is the config file in the environment folder?
- if (defined('ENVIRONMENT') && file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php'))
- {
- require($file_path);
- }
- elseif ( ! $found)
- {
- set_status_header(503);
- exit('The configuration file does not exist.');
- }
-
- // Does the $config array exist in the file?
- if ( ! isset($config) OR ! is_array($config))
- {
- set_status_header(503);
- exit('Your config file does not appear to be formatted correctly.');
- }
-
- // Are any values being dynamically replaced?
- if (count($replace) > 0)
- {
- foreach ($replace as $key => $val)
- {
- if (isset($config[$key]))
- {
- $config[$key] = $val;
- }
- }
- }
-
- return $_config[0] =& $config;
+ // Get instance, lowercase class, and check
+ $CI = CodeIgniter::instance();
+ $class = strtolower($class);
+ return isset($CI->$class);
}
}
@@ -285,20 +178,15 @@ function &get_config($replace = array())
*/
function config_item($item)
{
- static $_config_item = array();
-
- if ( ! isset($_config_item[$item]))
+ // Get instance and check for config
+ $CI = CodeIgniter::instance();
+ if ( ! isset($CI->config))
{
- $config =& get_config();
-
- if ( ! isset($config[$item]))
- {
- return FALSE;
- }
- $_config_item[$item] = $config[$item];
+ return FALSE;
}
- return $_config_item[$item];
+ // Return config item
+ return $CI->config->item($item);
}
}
@@ -348,8 +236,10 @@ function &get_mimes()
*/
function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
{
- $_error =& load_class('Exceptions', 'core');
- $_error->show_error($heading, $message, 'error_general', $status_code);
+ // Get instance, load Exceptions and call show_error
+ $CI = CodeIgniter::instance();
+ $CI->load_core_class('Exceptions');
+ $CI->exceptions->show_error($heading, $message, 'error_general', $status_code);
}
}
@@ -370,8 +260,10 @@ function show_error($message, $status_code = 500, $heading = 'An Error Was Encou
*/
function show_404($page = '', $log_error = TRUE)
{
- $_error =& load_class('Exceptions', 'core');
- $_error->show_404($page, $log_error);
+ // Get instance, load Exceptions and call show_404
+ $CI = CodeIgniter::instance();
+ $CI->load_core_class('Exceptions');
+ $CI->exceptions->show_404($page, $log_error);
}
}
@@ -392,15 +284,9 @@ function show_404($page = '', $log_error = TRUE)
*/
function log_message($level = 'error', $message, $php_error = FALSE)
{
- static $_log;
-
- if (config_item('log_threshold') === 0)
- {
- return;
- }
-
- $_log =& load_class('Log');
- $_log->write_log($level, $message, $php_error);
+ // Get instance and call log_message
+ $CI = CodeIgniter::instance();
+ $CI->log_message($level, $message, $php_error);
}
}
@@ -499,49 +385,6 @@ function set_status_header($code = 200, $text = '')
// --------------------------------------------------------------------
-if ( ! function_exists('_exception_handler'))
-{
- /**
- * Exception Handler
- *
- * This is the custom exception handler that is declaired at the top
- * of Codeigniter.php. The main reason we use this is to permit
- * PHP errors to be logged in our own log files since the user may
- * not have access to server logs. Since this function
- * effectively intercepts PHP errors, however, we also need
- * to display errors based on the current error_reporting level.
- * We do that with the use of a PHP error template.
- *
- * @param int
- * @param string
- * @param string
- * @param int
- * @return void
- */
- function _exception_handler($severity, $message, $filepath, $line)
- {
- $_error =& load_class('Exceptions', 'core');
-
- // Should we display the error? We'll get the current error_reporting
- // level and add its bits with the severity bits to find out.
- // And respect display_errors
- if (($severity & error_reporting()) === $severity && (bool) ini_get('display_errors') === TRUE)
- {
- $_error->show_php_error($severity, $message, $filepath, $line);
- }
-
- // Should we log the error? No? We're done...
- if (config_item('log_threshold') === 0)
- {
- return;
- }
-
- $_error->log_exception($severity, $message, $filepath, $line);
- }
-}
-
-// --------------------------------------------------------------------
-
if ( ! function_exists('remove_invisible_characters'))
{
/**

0 comments on commit 53c52a1

Please sign in to comment.