/system/ci-improved-1.0/core/CII_Loader.php
PHP | 614 lines | 350 code | 89 blank | 175 comment | 77 complexity | 0df5699d55995d4fb2a40febbd67fbd4 MD5 | raw file
Possible License(s): GPL-3.0
- <?php
- class CII_Loader extends CI_Loader
- {
- // Config instance holder
- public $config;
- // Controller instance holder (set at CII_Controller)
- public $controller;
- // Global vars array for views
- public $vars = array();
- // Global vars array for views
- public $_use_cache = TRUE;
-
- public function __construct()
- {
- parent::__construct();
-
- $this->config =& $this->_ci_get_component('config');
-
- $paths = array_keys( CII::paths() ); // @todo CORREGIRRRRRRRRRRRRRRRRRRRRRRRRRR!!!!!!!!!!!!!
- $this->_ci_library_paths = $paths;
- $this->_ci_helper_paths = $paths;
- $this->_ci_model_paths = $paths;
- array_shift( $paths ); // Get rid of BASEPATH
- $this->config->_config_paths = $paths;
- $this->_ci_view_paths = array_reverse( $paths );
- }
-
- /**
- * Load View used to load a "view" file
- *
- * @param string $view The name of the "view" file to be included
- * @param array $vars An associative array of data to be extracted for use in the view
- * @param bool $return Whether to return the data or load it
- * @param bool $cache Whether to cache $vars for future use or not
- * @return void|string Data loaded if $return=TRUE
- */
- function view($view, $vars = array(), $return = FALSE, $cache = TRUE )
- {
- if ( ! $cache ) $old_cache_status = $this->cache( FALSE );
- $rs = parent::view( $view, $vars, $return );
- if ( ! $cache ) $this->cache( $old_cache_status );
-
- return $rs;
- }
- /**
- * Load View used to load a "view" file with a full path
- *
- * @param string $path The path were the "view" file belongs
- * @param string $view The name of the "view" file to be included
- * @param array $vars An associative array of data to be extracted for use in the view
- * @param bool $return Whether to return the data or load it
- * @param bool $cache Whether to cache $vars for future use or not
- * @return void|string Data loaded if $return=TRUE
- */
- function viewFullPath ( $path, $view, $vars = array(), $return = FALSE, $cache = TRUE )
- {
- if ( ! $cache ) $old_cache_status = $this->cache( FALSE );
- $rs = $this->_ci_load(array('_ci_path' => $path, '_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
- if ( ! $cache ) $this->cache( $old_cache_status );
-
- return $rs;
- }
-
- /**
- * View loader for current theme
- *
- * @param String $view The view to load from current theme
- * @param Array $vars An array of variables to parse
- * @param Bool $return Returns as string or parses to browser
- * @return mixed
- */
- public function theme ( $view, $vars=array(), $return=FALSE, $cache = TRUE )
- {
- return $this->view( get_theme()."/".$view, $vars, $return, $cache );
- }
- /**
- * Form loader
- *
- * @param String $form The form view to load
- * @param Array $vars An array of variables to parse
- * @param Bool $return Returns as string or parses to browser
- * @return mixed
- */
- public function form ( $form, $vars=array(), $return=FALSE, $cache = TRUE )
- {
- return $this->view( "forms/".$form, $vars, $return, $cache );
- }
-
- /**
- * Library loader
- *
- * @param String $library The library file name to load
- * @param Mixed $params Optional parameters for class instance
- * If FALSE or NULL, then just includes the file
- * @param String $object_name An optional object name
- * @return void
- */
- public function library ( $library, $params = NULL, $object_name = NULL )
- {
- if ( is_array($library) )
- {
- foreach ( $library as $class )
- $this->library( $class, $params );
- return;
- }
-
- if ( $library == '' || isset($this->_base_classes[$library]) )
- return TRUE;
-
- // Safety: Was the class already loaded by a previous call?
- if ( in_array($library,$this->_ci_loaded_files) )
- {
- // Before we deem this to be a duplicate request, let's see
- // if a custom object name is being supplied. If so, we'll
- // return a new instance of the object
- if ( ! is_null($object_name) )
- {
- $CI =& get_instance();
- if ( ! isset($CI->$object_name) )
- return $this->_ci_init_class( $this->_ci_library_paths, 'libraries/'.$library, $params, $object_name );
- }
- log_message('debug', $library." class already loaded.");
- return TRUE;
- }
-
- $rs = $this->_ci_init_class( $this->_ci_library_paths, 'libraries/'.$library, $params, $object_name );
- if ( ! $rs )
- show_error('Unable to locate the library you have specified: '.$library);
-
- $this->_ci_loaded_files[] = $library;
- return $rs;
- }
- /**
- * Model Loader
- *
- * @param String $model The library file name to load
- * @param String $name Optional name for the model
- * @param Bool $db_con Database connection
- * @param Bool $instantiate Whether to instantiate the class or just include it
- * @return void
- */
- function model ( $model, $name = NULL, $params = array(), $db_conn = FALSE )
- {
- if ( is_array($model) )
- {
- foreach ( $model as $babe )
- $this->model( $babe, $name, $params, $db_conn );
- return;
- }
-
- $CI =& get_instance();
- if ( $name && isset($CI->$name) )
- show_error('The model name you are loading is the name of a resource that is already being used: '.$name);
- // Is the model in a sub-folder? If so, parse out the filename and path.
- $path = '';
- if (($last_slash = strrpos($model, '/')) !== FALSE)
- {
- // The path is in front of the last slash
- $path = substr($model, 0, $last_slash + 1);
- // And the model name behind it
- $model = substr($model, $last_slash + 1);
- }
- $instanciate = FALSE;
- if ( $name )
- $instanciate = TRUE;
- else
- $name = $model;
- if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
- {
- if ($db_conn === TRUE)
- {
- $db_conn = '';
- }
- $CI->load->database($db_conn, FALSE, TRUE);
- }
- if ( ! class_exists('CI_Model') )
- load_class('Model', 'core', NULL, FALSE );
- // We don't need an instance!
- if ( ! $instanciate )
- return _load_class( CII::paths(), $model, 'models/'.$path, FALSE );
- // OK, let's instanciate the model
- $instance = _load_class( CII::paths(), $model, 'models/'.$path, $params );
- if ( ! $instance ) // couldn't find the model
- show_error('Unable to locate the model you have specified: '.$model);
-
- $this->_ci_models[] = $name;
- return $instance;
- }
-
- /**
- * Load Helper
- *
- * This function loads the specified helper file.
- *
- * @access public
- * @param mixed
- * @return void
- */
- function helper ( $helpers = array() )
- {
- foreach ($this->_ci_prep_filename($helpers, '_helper') as $helper)
- {
- if (isset($this->_ci_helpers[$helper]))
- continue;
-
- if ( _load_class( array_reverse(CII::paths()), $helper, 'helpers', FALSE ) )
- {
- $this->_ci_helpers[$helper] = TRUE;
- log_message('debug', 'Helper loaded: '.$helper);
- }
- // unable to load the helper
- if ( ! isset($this->_ci_helpers[$helper]))
- show_error('Unable to load the requested file: helpers/'.$helper.EXT);
- }
- }
-
- /**
- * Database Loader
- *
- * @access public
- * @param string the DB credentials
- * @param bool whether to return the DB object
- * @param bool whether to enable active record (this allows us to override the config setting)
- * @return object
- */
- function database( $params = '', $return = FALSE, $active_record = NULL , $new_instance = FALSE )
- {
- // Grab the super object
- $CI =& get_instance();
- // Do we even need to load the database class?
- if (class_exists('CI_DB') AND $return == FALSE AND $active_record == NULL AND isset($CI->db) AND is_object($CI->db))
- {
- return FALSE;
- }
- // Include the DB main function
- load_one_file( 'DB', 'database' );
- if ($return === TRUE)
- {
- return DB($params, $active_record, $new_instance);
- }
- // Initialize the db variable. Needed to prevent
- // reference errors with some configurations
- $CI->db = '';
- // Load the DB class
- $CI->db =& DB($params, $active_record, $new_instance);
- }
-
- /**
- * Driver
- *
- * Loads a driver library
- *
- * @param string the name of the class
- * @param mixed the optional parameters
- * @param string an optional object name
- * @return void
- */
- function driver($library = '', $params = NULL, $object_name = NULL)
- {
- if ( ! class_exists('CI_Driver_Library'))
- {
- // we aren't instantiating an object here, that'll be done by the Library itself
- load_file( 'Driver', 'libraries' );
- }
- // We can save the loader some time since Drivers will *always* be in a subfolder,
- // and typically identically named to the library
- if ( ! strpos($library, '/'))
- {
- $library = ucfirst($library).'/'.$library;
- }
- return $this->library($library, $params, $object_name);
- }
-
- /**
- * Set Variables
- *
- * Once variables are set they become available within
- * the controller class and its "view" files.
- *
- * @param array
- * @return array
- */
- function vars( $vars = array(), $val = '' )
- {
- if ( ! $vars )
- return $this->_ci_cached_vars;
-
- if ( ! $val && is_string($vars) )
- return $this->_ci_cached_vars[ $vars ];
-
- return parent::vars( $vars, $val );
- }
-
- /**
- * Enables or disables the internal vars cache
- *
- * @param bool $enable
- * @return bool The original cache status
- */
- function cache ( $enable )
- {
- $old = $this->_use_cache;
- $this->_use_cache = (bool)$enable;
-
- return $old;
- }
-
- function _ci_load ( $_ci_data )
- {
- // Set the default data variables
- foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
- {
- $$_ci_val = ( ! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val];
- }
- // Set the path to the requested file
- if ($_ci_path == '')
- {
- $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
- $_ci_file = ($_ci_ext == '') ? $_ci_view.EXT : $_ci_view;
- foreach ( $this->_ci_view_paths as $path )
- {
- $_ci_path = $path . 'views/' . $_ci_file;
- if ( file_exists($_ci_path) )
- break;
- }
- }
- else if ( $_ci_path && $_ci_view ) // Full path support
- {
- $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
- $_ci_file = ($_ci_ext == '') ? $_ci_view.EXT : $_ci_view;
- $_ci_path .= "/".$_ci_file;
- }
- else
- {
- $_ci_x = explode('/', $_ci_path);
- $_ci_file = end($_ci_x);
- }
- if ( ! file_exists($_ci_path))
- {
- show_error('Unable to load the requested file: '.$_ci_file);
- }
- // This allows anything loaded using $this->load (views, files, etc.)
- // to become accessible from within the Controller and Model functions.
- $_ci_CI =& get_instance();
- foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
- {
- if ( ! isset($this->$_ci_key))
- {
- $this->$_ci_key =& $_ci_CI->$_ci_key;
- }
- }
- /*
- * Extract and cache variables
- *
- * You can either set variables using the dedicated $this->load_vars()
- * function or via the second parameter of this function. We'll merge
- * the two types and cache them so that views that are embedded within
- * other views can have access to these variables.
- */
- if ( $this->_use_cache && is_array($_ci_vars) )
- $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
- if ( ( $vars = ($this->_use_cache ? $this->_ci_cached_vars : $_ci_vars) ) )
- extract( $vars );unset( $vars );
-
- /*
- * Buffer the output
- *
- * We buffer the output for two reasons:
- * 1. Speed. You get a significant speed boost.
- * 2. So that the final rendered template can be
- * post-processed by the output class. Why do we
- * need post processing? For one thing, in order to
- * show the elapsed page load time. Unless we
- * can intercept the content right before it's sent to
- * the browser and then stop the timer it won't be accurate.
- */
- ob_start();
- // If the PHP installation does not support short tags we'll
- // do a little string replacement, changing the short tags
- // to standard PHP echo statements.
- if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
- {
- echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
- }
- else
- {
- include($_ci_path); // include() vs include_once() allows for multiple views with the same name
- }
- log_message('debug', 'File loaded: '.$_ci_path);
- // Return the file data if requested
- if ($_ci_return === TRUE)
- {
- $buffer = ob_get_contents();
- @ob_end_clean();
- return $buffer;
- }
- /*
- * Flush the buffer... or buff the flusher?
- *
- * In order to permit views to be nested within
- * other views, we need to flush the content back out whenever
- * we are beyond the first level of output buffering so that
- * it can be seen and included properly by the first included
- * template and any subsequent ones. Oy!
- *
- */
- if (ob_get_level() > $this->_ci_ob_level + 1)
- {
- ob_end_flush();
- }
- else
- {
- $_ci_CI->output->append_output(ob_get_contents());
- @ob_end_clean();
- }
-
- return TRUE;
- }
-
- function _ci_init_class ( $paths, $class, $config = NULL, $object_name = NULL )
- {
- list( $path, $class ) = $this->_get_path_class( $class );
- $name = strtolower( $class );
-
- // Is there an associated config file for this class? Note: these should always be lowercase
- if ( $config === NULL )
- {
- foreach ( (array)$this->config->_config_paths as $cpath )
- {
- if ( defined('ENVIRONMENT') && file_exists($cpath .'config/'.ENVIRONMENT.'/'.$name.EXT) )
- require( $cpath .'config/'.ENVIRONMENT.'/'.$name.EXT );
- else if ( file_exists($cpath .'config/'.$name.EXT) )
- require( $cpath .'config/'.$name.EXT );
- }
-
- if ( ! $config && ${$name} )
- $config = ${$name};
- }
-
- if ( $config ) // Just one parameter for class constructor (an array with config values)
- $config = array( $config );
- else if ( $object_name !== FALSE )
- $config = array();
-
- $scopes = CII::paths();
- foreach ( (array)$paths as $apath )
- if ( $scopes[$apath] )
- $load_paths[ $apath ] = $scopes[ $apath ];
-
- $instance =& _load_class( $load_paths, ucfirst($class), $path, $config );
- if ( ! $instance ) // No instance found? Try without ucfirst!
- $instance =& _load_class( $load_paths, $class, $path, $config );
-
- if ( ! $instance )
- {
- log_message('error', "Non-existent class: ".$path.$class);
- return $instance;
- }
-
- // Set the variable name we will assign the class to
- // Was a custom class name supplied? If so we'll use it
- $class = strtolower($class);
-
- if ( $object_name === FALSE ) // Just load the class, don't instanciate!
- return TRUE;
-
- if (is_null($object_name))
- {
- $classvar = ( ! isset($this->_ci_varmap[$class])) ? $class : $this->_ci_varmap[$class];
- }
- else
- {
- $classvar = $object_name;
- }
- // Save the class name and object name
- $this->_ci_classes[$class] = $classvar;
- // Map the class
- $CI =& get_instance();
- $CI->$classvar = $instance;
-
- return $instance;
- }
-
- function _ci_autoloader ( )
- {
- $autoload =& load_file_vars( 'autoload', 'config'.(defined('ENVIRONMENT')?'/'.ENVIRONMENT:'') );
-
- if ( ! isset($autoload) )
- return FALSE;
- // Autoload packages
- if ( isset($autoload['packages']) )
- foreach ( $autoload['packages'] as $package_path )
- $this->add_package_path( $package_path );
- // Load any custom config file
- if ( count($autoload['config']) > 0 )
- {
- $CI =& get_instance();
- foreach ( $autoload['config'] as $key => $val )
- $CI->config->load($val);
- }
- // Autoload helpers and languages
- foreach ( array('helper', 'language') as $type )
- if ( isset($autoload[$type]) AND count($autoload[$type]) > 0 )
- $this->$type( $autoload[$type] );
- // A little tweak to remain backward compatible
- // The $autoload['core'] item was deprecated
- if ( ! isset($autoload['libraries']) AND isset($autoload['core']) )
- $autoload['libraries'] = $autoload['core'];
- // Load libraries
- if ( isset($autoload['libraries']) AND count($autoload['libraries']) > 0 )
- {
- // Load the database driver.
- if ( in_array('database', $autoload['libraries']) )
- {
- $this->database();
- $autoload['libraries'] = array_diff( $autoload['libraries'], array('database') );
- }
- // Load all other libraries
- foreach ( $autoload['libraries'] as $item )
- $this->library($item);
- }
- // Autoload models
- if ( isset($autoload['model']) )
- $this->model( $autoload['model'] );
- }
-
- function add_package_path ( $path )
- {
- $path = rtrim($path, '/').'/';
- $this->_ci_library_paths[ ] = $path;
- $this->_ci_model_paths[ ] = $path;
- $this->_ci_helper_paths[ ] = $path;
- // Add config file path
- $this->config->_config_paths[ ] = $path;
- }
- function remove_package_path ( $path, $remove_config_path = TRUE )
- {
- $path = rtrim($path, '/').'/';
- foreach (array('_ci_library_paths', '_ci_model_paths', '_ci_helper_paths') as $var)
- if ( ($pos=array_search($path,$this->{$var})) !== FALSE )
- unset( $this->{$var}[ $pos ] );
- if ( ($pos=array_search($path,$this->config->_config_paths)) !== FALSE )
- unset( $this->config->_config_paths[$pos] );
- // make sure the application default paths are still in the array
- $paths = CII::paths();
- $this->_ci_library_paths = array_keys(array_unique(array_merge($this->_ci_library_paths, $paths)));
- $this->_ci_helper_paths = array_keys(array_unique(array_merge($this->_ci_helper_paths, $paths)));
- $this->_ci_model_paths = array_keys(array_unique(array_merge($this->_ci_model_paths, $paths)));
- array_shift( $paths ); // Get rid of BASEPATH
- $this->config->_config_paths = array_keys(array_unique(array_merge($this->config->_config_paths, $paths)));
- $this->_ci_view_paths = array_reverse(array_keys(array_unique(array_merge($this->_ci_view_paths, $paths))));
- }
-
- private function _get_path_class ( $class )
- {
- $path = '';
- if ( ($last_slash = strrpos($class, '/')) !== FALSE )
- {
- // The path is in front of the last slash
- $path = substr( $class, 0, $last_slash + 1);
- // And the class name behind it
- $class = substr( $class, $last_slash + 1);
- }
-
- return array( trim($path,'/'), $class );
- }
- }