Welcome Guest, Not a member yet? Register   Sign In
Spark Library - Dynamic Third Party Class Loading
#8

[eluser]helmutbjorg[/eluser]
FYI: I have updated this library and changed it so that it is an extension to the loader class which is kind of what I wanted to do in the first place but couldn't think of a clean way to do it. I like this better than having it as a separate library as we don't really need another library for this purpose if we don't have to!

/application/libraries/MY_Loader.php
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
* MY_Loader Class
*
* @package        CodeIgniter
* @category    Libraries
* @version        1.0
*
* Extending the Loader library to allow for loading of third
* party classes with the option of feeding the parameters.
*
* For example sometimes you want to load a class and pass
* values to the constructor like so:
*
* $object = new Object('username', 'password');
*
* However with the standard library loader you can only pass
* a single array to the constructor. With the new param $feed_params
* set to true, the library will be loaded with the parameters fed
* to the contructor. If false (default) the loading will be passed
* back to the regular load function.
*
* Using the example above you would load the class like so:
*
* $this->load->library('object', array('myusername', 'mypassword'), null, true);
*
* And presto!
*
*/
class MY_Loader extends CI_Loader {
    
    // Custom library loading with optional feeding of parameters to constructor
    function library($library = '', $params = NULL, $object_name = NULL, $feed_params = FALSE) {

        // If not feeding the params we can pass to parent function
        if($feed_params==false) return parent::library($library, $params, $object_name);
        
        // Otherwise load the class by feeding the parameters
        if($library=='') return false;    
        
        // Clean up class name
        $library = str_replace(EXT, '', trim($library, '/')); // Get the class name, and while we're at it trim any slashes
        $subdir = ''; // We may need to handle a subdirectory or two
                
        // Was the path included with the class name?        
        if(strpos($library, '/') !== false) {
            $x = explode('/', $library); // Separate the filename from the path
            $library = end($x); // Reset the variable now that we know the actual filename
            unset($x[count($x)-1]); // Kill the filename from the array
            $subdir = implode($x, '/').'/'; // Glue the path back together, sans filename
        }    
        
        // If no object name use the class name
        if(empty($object_name)) $object_name = $library;
        
        // Grab a codeigniter instance
        $CI =& get_instance();
                
        // Has this class already been loaded into the codeigniter object
        if(isset($CI->$object_name) && strtolower(get_class($CI->$object_name))==strtolower($library)) return true;
        
        // Is this position free in the codeigniter object
        if(isset($CI->$object_name)) show_error('Unable to load '.$object_name.' as object name already exists.');
        
        // Does the file need to be included
        if(!class_exists($library)) {        
            // Path to the class file
            $filepath = APPPATH.'libraries/'.$subdir.$library.EXT;    
            // Does the file exist
            if(!file_exists($filepath)) show_error('Unable to load requested class file: '.$library);
            // Include the file
            include_once($filepath);
        }
        
        // Create a reflected object
        $object = new ReflectionClass($library);
        
        // Force parameters to be an array
        if(!is_array($params)) $params = array();
        
        // Instantiate the object with dynamic parameters
        $CI->$object_name = $object->newInstanceArgs($params);
        
        return true;
        
    }    

}

/* End of file MY_Loader.php */
/* Location: ./application/libraries/MY_Loader.php */


Messages In This Thread
Spark Library - Dynamic Third Party Class Loading - by El Forum - 03-20-2010, 06:32 PM
Spark Library - Dynamic Third Party Class Loading - by El Forum - 03-20-2010, 06:50 PM
Spark Library - Dynamic Third Party Class Loading - by El Forum - 03-20-2010, 06:58 PM
Spark Library - Dynamic Third Party Class Loading - by El Forum - 03-20-2010, 06:59 PM
Spark Library - Dynamic Third Party Class Loading - by El Forum - 03-20-2010, 07:35 PM
Spark Library - Dynamic Third Party Class Loading - by El Forum - 03-29-2010, 09:30 AM
Spark Library - Dynamic Third Party Class Loading - by El Forum - 03-29-2010, 05:20 PM
Spark Library - Dynamic Third Party Class Loading - by El Forum - 04-18-2010, 01:28 AM



Theme © iAndrew 2016 - Forum software by © MyBB