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

[eluser]helmutbjorg[/eluser]
Hey guys,

Thought i might give something back to the community. Spark is a simple, dynamic third party library loading for a codeigniter application. I got the idea when reading about Elliot Haughin's inferno class http://www.haughin.com/2008/08/18/introd...libraries/. Which is cool but i thought there must be an easier way to port third party libraries without having to do a whole heap of work integrating them

Usage Example:

1. You download a third party class and want to use it within codeigniter.
2. Instead of porting it, changing the class code or extending it to use it within
codeigniter you simply copy the class file into the application/libraries folder.
3. You load the class using the spark library like so:


Code:
// Load the spark library. der...
$this->load->library('spark');

// Get spark to load it and pass optional parameters to construct it
$this->spark->load('downloadedclass.php', $params);

// Presto! You can use it now through codeigniter
$this->downloadedclass->function();

And you can even pass in an alias like when you load codeigniter libraries:
Code:
$this->spark->load('downloadedclass.php', $params, 'myalias');
$this->myalias->function();


Can be downloaded from http://github.com/helmut/Spark

Any thoughts or suggestions are welcome as this is only the first basic version and i'm sure there are ways to improve it.
#2

[eluser]n0xie[/eluser]
It looks pretty cool but I don't really get what the use is. I'm not trying to be negative here, because maybe I missed something.

Afaik the $this->load-> convention of CodeIgniter only instantiates a class. So if you include a third-party class file, it should technically just work like any 'normal' library. Could you explain to me what I'm missing because if I do miss something, this would be very useful.
#3

[eluser]helmutbjorg[/eluser]
Generally when you load a library using the $this->load->library('class') method you can't pass any parameters to it for use in the constructor (or you can only pass a single array which is passed as a single array). Many third party scripts will have a constructor like so

Code:
class ThirdParty {

    function ThirdParty($username, $password, $host='') {
        // Construct
    }

}

Using the $this->load->library() method will throw an error for this class because the username and password vars are not being supplied.

With the spark library you can pass the parameters when loading the class like so:

Code:
$this->spark->load('thirdparty', array('myusername', 'mypassword', 'andevenmyhost'));

Does that make sense?
#4

[eluser]helmutbjorg[/eluser]
Edit: changed github username so link is now - http://github.com/helmut/Spark
#5

[eluser]n0xie[/eluser]
Aah that makes sense. I was thinking that since the loader class allows you to add a config parameter, you would be able to pass variables to a constructor, but obviously this wouldn't work if they needed to be in a specific order.
#6

[eluser]ortenheim[/eluser]
Hi! really like this library it helped me integrate a third party library, however, how would i be able to integrate my third party library directly with my codeigniter models? is it possible from their respecive points without inputting the data as variables in the controller as you were showing in your examples? or do i have to input this data from my controller?

PS. my external library is several models connected through
Code:
Include_once('file.php);

thx

/mikael
#7

[eluser]helmutbjorg[/eluser]
Not exactly sure what you mean. Can you tell me more about your problem? Either way... the example I gave above will work inside models... You are not restricted to using it just in a controller.
#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 */




Theme © iAndrew 2016 - Forum software by © MyBB