Welcome Guest, Not a member yet? Register   Sign In
Proper way to extend custom library
#1

[eluser]bretticus[/eluser]
Hi Guys,

I usually try to dig in and fix my own problems but this one has me scratching my head (perhaps I'm just doing this WRONG.)

So, I have used Phil Sturgeon's popular REST client/server library. I needed to modify the class slightly for a specific API. Since I wanted to keep his code intact for future updates, I extended his Rest and Curl classes to suit my needs (I did have to change visibility scope from private to protected but that's it.)

I'm having an issue with loading my extended library (the details are not really important, skip down):

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* @author REST Philip Sturgeon
* @author CB_REST Brett Millett
*/

require_once 'Rest.php';

class CB_REST extends REST {

    private $developer_key;
    private $clerk_key;

    function __construct($config = array())
    {
        $this->CI =& get_instance();
        log_message('debug', 'CB REST Class Initialized');

                // in order to get HEAD functionality, call clickbank extended curl instead.
        $this->CI->load->library('cb_curl', NULL, 'curl');

        // If a URL was passed to the library
        if(!empty($config))
        {
            $this->initialize($config);
        }
    }

    function  initialize($config) {

        $this->rest_server = @$config['server'];

        if(substr($this->rest_server, -1, 1) != '/')
        {
                $this->rest_server .= '/';
        }

        $this->http_auth        = isset($config['http_auth']) ? $config['http_auth'] : '';
        $this->http_user        = isset($config['http_user']) ? $config['http_user'] : '';
        $this->http_pass        = isset($config['http_pass']) ? $config['http_pass'] : '';
        $this->developer_key    = isset($config['developer_key']) ? $config['developer_key'] : '';
        $this->clerk_key        = isset($config['clerk_key']) ? $config['clerk_key'] : '';
        
    }

    // clickbank does not accept data in message body so we will send the post verb but add params to URI (just like GET.)
    public function post($uri, $params = array(), $format = NULL)
    {
        if(!empty($params))
        {
            $uri .= '?'.http_build_query($params);
        }

        return $this->_call('post', $uri, NULL, $format);
    }

    protected function _set_headers() {
        $this->CI->curl->http_header("Authorization: {$this->developer_key}:{$this->clerk_key}");
        $this->CI->curl->http_header('Accept: '.$this->mime_type);
    }
    

    protected function _call($method = 'get', $uri, $params = array(), $format = NULL) {
        if($format !== NULL) {
            $this->format($format);
        }

        $this->_set_headers();

        // Initialize cURL session
        $this->CI->curl->create($this->rest_server.$uri);

        // If authentication is enabled use it
        if($this->http_auth != '' && $this->http_user != '') {
            $this->CI->curl->http_login($this->http_user, $this->http_pass, $this->http_auth);
        }

        // We still want the response even if there is an error code over 400
        $this->CI->curl->option('failonerror', FALSE);

        // turn off verify peer since this fails for clickbank certificate (SSL is good enough)
        $this->CI->curl->option('ssl_verifypeer', FALSE);

        // Call the correct method with parameters
        $this->CI->curl->{$method}($params);

        // Execute and return the response from the REST server
        $response = $this->CI->curl->execute();

        if ( $response !== FALSE ) {

            // Format and return
            return $this->_format_response($response);

        }

        // return false on protocol error or connection timeout
        return FALSE;
    }  
  
    public function listorder($reciept)
    {
        return $this->get('rest/1.2/orders/' . $reciept, array(), 'json');
    }

    public function is_order_active($receipt)
    {

        // timeout quicker for this call.
        $this->CI->curl->option('timeout', 5);

        if ( ! $this->is_valid_receipt($receipt) )
                return FALSE;

        $response = $this->head('rest/1.2/orders/' . $receipt);

        if ( $response === FALSE ) //network error, just return true.
            return TRUE;

        // reset timeout.
        $this->CI->curl->option('timeout', 30);
        
        switch( (int) $this->CI->curl->info['http_code'] ) {
            case 204:
                return TRUE;
                break;
            default:
                return FALSE;
        }      
    }

    // head function
    public function head($uri, $params = array(), $format = NULL)
    {
        return $this->_call('head', $uri, $params, $format);
    }

    private function is_valid_receipt($receipt)
    {
        //... truncated
    }


}

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

I an getting the infamous error:


Quote:An Error Was Encountered

Unable to load the requested class: cb_rest

Strange thing is that I get no error on my development machine (OSX via MAMP.) This lib is being loaded for a login routine and the really strange thing is that for most users, I do not get this error. When I try the same user login on my development machine, it loads fine. I know that you (and I myself) suspect that this has something to do with code before this, but there are no variables that are affected from my debugging (I'm using subversion to push changes to production so I know they are exactly the same.) I've checked permissions. Not sure what is going on. I can run a debugger on development but not on production. I don't want to stick debugging code in core files either if possible (I suppose I could make a MY_Loader class and copy code and put in debugging code.) Anyways, if anyone sees some bad practices, please call me out!

Thanks!
#2

[eluser]danmontgomery[/eluser]
is CB_ your subclass_prefix?

(Are you loading CB_REST or REST?)
#3

[eluser]bretticus[/eluser]
[quote author="noctrum" date="1268445583"]is CB_ your subclass_prefix?

(Are you loading CB_REST or REST?)[/quote]

@noctrum. Thanks for taking the time to read all that.

No, I used a CB_ prefix arbitrarily.

I loaded CB_REST and that (as you can see) extends REST.

I did end up just renaming my files to Cb_rest.php and Cb_curl.php. In addition to that I renamed my class names to match. That got me to a real error:

Quote:CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set...Curl.php 203

I do have open_basedir in effect on production but not development. I simply commented that line out since I don't need that for the API's I'm currently using.

Everything runs smoothly after that.

Thanks again.




Theme © iAndrew 2016 - Forum software by © MyBB