From c26b9ebb00e29be2e972fece3bcf73d33249a64b Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 24 Feb 2014 11:31:36 +0200 Subject: Don't use globals - Use load_class() to get objects during bootstrap process. - Change load_class() to accept a class constructor parameter instead of previously unused class name prefix. - Change CI_Router::__construct() to accept as a parameter. --- system/core/CodeIgniter.php | 5 ++--- system/core/Common.php | 11 ++++++----- system/core/Input.php | 6 ++---- system/core/Output.php | 5 +++-- system/core/Router.php | 6 ++---- system/core/URI.php | 4 +--- tests/mocks/core/common.php | 2 ++ user_guide_src/source/changelog.rst | 1 + 8 files changed, 19 insertions(+), 21 deletions(-) diff --git a/system/core/CodeIgniter.php b/system/core/CodeIgniter.php index 6d6747106..df5fa3b02 100644 --- a/system/core/CodeIgniter.php +++ b/system/core/CodeIgniter.php @@ -212,7 +212,7 @@ defined('BASEPATH') OR exit('No direct script access allowed'); * Instantiate the routing class and set the routing * ------------------------------------------------------ */ - $RTR =& load_class('Router', 'core'); + $RTR =& load_class('Router', 'core', isset($routing) ? $routing : NULL); /* * ------------------------------------------------------ @@ -226,8 +226,7 @@ defined('BASEPATH') OR exit('No direct script access allowed'); * Is there a valid cache file? If so, we're done... * ------------------------------------------------------ */ - if ($EXT->call_hook('cache_override') === FALSE - && $OUT->_display_cache($CFG, $URI) === TRUE) + if ($EXT->call_hook('cache_override') === FALSE && $OUT->_display_cache($CFG, $URI) === TRUE) { exit; } diff --git a/system/core/Common.php b/system/core/Common.php index 0ea53c4e2..24315a054 100644 --- a/system/core/Common.php +++ b/system/core/Common.php @@ -130,7 +130,7 @@ if ( ! function_exists('load_class')) * @param string the class name prefix * @return object */ - function &load_class($class, $directory = 'libraries', $prefix = 'CI_') + function &load_class($class, $directory = 'libraries', $param = NULL) { static $_classes = array(); @@ -148,7 +148,7 @@ if ( ! function_exists('load_class')) { if (file_exists($path.$directory.'/'.$class.'.php')) { - $name = $prefix.$class; + $name = 'CI_'.$class; if (class_exists($name, FALSE) === FALSE) { @@ -166,7 +166,7 @@ if ( ! function_exists('load_class')) if (class_exists($name, FALSE) === FALSE) { - require_once(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php'); + require_once(APPPATH.$directory.'/'.$name.'.php'); } } @@ -183,8 +183,9 @@ if ( ! function_exists('load_class')) // Keep track of what we just loaded is_loaded($class); - $_classes[$class] = new $name(); - return $_classes[$class]; + return $_classes[$class] = isset($param) + ? new $name($param) + : new $name(); } } diff --git a/system/core/Input.php b/system/core/Input.php index fdb308b5a..620e50f63 100644 --- a/system/core/Input.php +++ b/system/core/Input.php @@ -126,14 +126,12 @@ class CI_Input { $this->_enable_csrf = (config_item('csrf_protection') === TRUE); $this->_sandardize_newlines = (bool) config_item('standardize_newlines'); - global $SEC; - $this->security =& $SEC; + $this->security =& load_class('Security', 'core'); // Do we need the UTF-8 class? if (UTF8_ENABLED === TRUE) { - global $UNI; - $this->uni =& $UNI; + $this->uni =& load_class('Utf8', 'core'); } // Sanitize global arrays diff --git a/system/core/Output.php b/system/core/Output.php index e8c1c3972..2811a73bc 100644 --- a/system/core/Output.php +++ b/system/core/Output.php @@ -401,10 +401,11 @@ class CI_Output { */ public function _display($output = '') { - // Note: We use globals because we can't use $CI =& get_instance() + // Note: We use load_class() because we can't use $CI =& get_instance() // since this function is sometimes called by the caching mechanism, // which happens before the CI super object is available. - global $BM, $CFG; + $BM =& load_class('Benchmark', 'core'); + $CFG =& load_class('Config', 'core'); // Grab the super object if we can. if (class_exists('CI_Controller', FALSE)) diff --git a/system/core/Router.php b/system/core/Router.php index 05263b153..2448d9c8c 100644 --- a/system/core/Router.php +++ b/system/core/Router.php @@ -109,10 +109,8 @@ class CI_Router { * * @return void */ - public function __construct() + public function __construct($routing = NULL) { - global $routing; - $this->config =& load_class('Config', 'core'); $this->uri =& load_class('URI', 'core'); @@ -120,7 +118,7 @@ class CI_Router { $this->_set_routing(); // Set any routing overrides that may exist in the main index file - if (isset($routing) && is_array($routing)) + if (is_array($routing)) { if (isset($routing['directory'])) { diff --git a/system/core/URI.php b/system/core/URI.php index 5f5a9ce42..9a545fd3a 100644 --- a/system/core/URI.php +++ b/system/core/URI.php @@ -628,9 +628,7 @@ class CI_URI { */ public function ruri_string() { - global $RTR; - - return ltrim($RTR->directory, '/').implode('/', $this->rsegments); + return ltrim(load_class('Router', 'core')->directory, '/').implode('/', $this->rsegments); } } diff --git a/tests/mocks/core/common.php b/tests/mocks/core/common.php index 9eb6b0954..5c32ca5c2 100644 --- a/tests/mocks/core/common.php +++ b/tests/mocks/core/common.php @@ -65,6 +65,7 @@ if ( ! function_exists('get_mimes')) // -------------------------------------------------------------------- +/* if ( ! function_exists('load_class')) { function load_class($class, $directory = 'libraries', $prefix = 'CI_') @@ -86,6 +87,7 @@ if ( ! function_exists('load_class')) return $obj; } } +*/ // Clean up error messages // -------------------------------------------------------------------- diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index b654bc51f..07ea85bcf 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -474,6 +474,7 @@ Release Date: Not Released - Added function :func:`is_cli()` to replace the ``CI_Input::is_cli_request()`` method. - Added function :func:`function_usable()` to work around a bug in `Suhosin `. - Removed the third (`$php_error`) argument from function :func:`log_message()`. + - Changed internal function ``load_class()`` to accept a constructor parameter instead of (previously unused) class name prefix. - :doc:`Output Library ` changes include: -- cgit v1.2.3-24-g4f1b