Part of the EllisLab Network
This thread is a discussion for the wiki article: PEAR integration
   
 
PEAR integration
Posted: 31 March 2009 02:26 PM   [ Ignore ]  
Summer Student
Total Posts:  10
Joined  03-28-2009

I’m trying to incorporate text differencing into my code igniter application, so I’m trying to import the PEAR Text_Diff library. However, the Text_Diff constructor requires two arguments, while the Pearloader only allows one.

From PEAR Documentation:
(see http://pear.php.net/package/Text_Diff/docs/latest/Text_Diff/Text_Diff.html)

Text_Diff Text_Diffstring $engine, array $params

My CI code:

$this->load->library('pearloader');
$textdiff $this->pearloader->load('Text''Diff'$arg1); 

What I’d like to do:

$this->load->library('pearloader');
$textdiff $this->pearloader->load('Text''Diff'$arg1$arg2); 

How do I pass two or more arguments into PEAR classes loaded by the pearloader library?

Profile
 
 
Posted: 31 March 2009 04:21 PM   [ Ignore ]   [ # 1 ]  
Summer Student
Total Posts:  10
Joined  03-28-2009

I solved the need for multiple constructor arguments by invoking the constructor with an array of arguments. I’m new to PHP, and used the following documentation to invoke the constructor with reflection:
http://us.php.net/call_user_func_array
http://us.php.net/func_get_args

Here’s the modified pearloader:

<?php
// Based on PEAR-to-CodeIgniter integration instructions at http://codeigniter.com/wiki/PEAR_integration/
// Modified to support variable argument length in PEAR constructor calls
if (!defined('BASEPATH')) exit('No direct script access allowed');

class 
Pearloader{
  
function load($package$class){
   
require_once($package.'/'.$class.'.php');
   
$classname $package."_".$class;
    
$args func_get_args();
   return 
call_user_func_array(
        array(new 
ReflectionClass($classname), 'newInstance'),
         
array_slice($args2)
    );
  
}
}

?> 

Here’s how I used the modified Pearloader in a CI controller. The PEAR Text_Diff constructor’s signature is Text_Diff( string $engine, array $params):

class MyController() {
...
function 
test() {
  $this
->load->library('pearloader');
  
$engine 'auto';
  
$params = array(file($file1)file($file2));
  
$textdiff $this->pearloader->load('Text''Diff'$engine$params);
}
...

Any feedback or suggestions would be welcome, especially since I’m not a regular PHP user.

Profile
 
 
Posted: 31 March 2009 04:55 PM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  10
Joined  03-28-2009

The previous solution didn’t support pear packages with classes in nested directories. I didn’t realize PEAR used ‘_’ underscore characters in class names to delimit directory paths. Here’s a better version that supports both nested classpaths and variable arguments:

<?php
// Based on PEAR-to-CodeIgniter integration instructions at http://codeigniter.com/wiki/PEAR_integration/
// Modified to support variable argument length in PEAR constructor calls
if (!defined('BASEPATH')) exit('No direct script access allowed');

class 
Pearloader{
    
function load($classpath){
        $filename 
str_replace('_''/'$classpath).'.php';
        require_once(
$filename);
        
$args func_get_args();
        return 
call_user_func_array(
            array(new 
ReflectionClass($classpath), 'newInstance'),
            
array_slice($args1)
        );
  
}
}

?> 

Here is a usage example where the first argument to pearloader->load() is the class name and subsequent arguments are used as constructor arguments:

$this->load->library('pearloader');
$diff $this->pearloader->load('Text_Diff'$file1$file2);
$renderer $this->pearloader->load('Text_Diff_Renderer_inline'$diff);
echo 
$renderer->render($diff); 
Profile
 
 
Posted: 15 March 2010 04:01 PM   [ Ignore ]   [ # 3 ]  
Grad Student
Avatar
Rank
Total Posts:  57
Joined  02-11-2010

Thanks for this, worked great after I added your addition.  I was thinking that perhaps you shoul update the wiki article so it has the new code in it.

Profile
 
 
Posted: 13 May 2010 06:21 AM   [ Ignore ]   [ # 4 ]  
Summer Student
Total Posts:  8
Joined  05-13-2010

Hi,

I’d also like to suggest the following amendment, which accounts for people who rename or move their “application” folder out of the “system” folder, for example, when following http://codeigniter.com/wiki/Better_Server_Setup_for_CI/

Before:

class Pear_hook{
  
function index(){
  
// OS independent
 
ini_set('include_path',ini_get('include_path').PATH_SEPARATOR.BASEPATH.'application/pear/');
  
// on Apache
  // ini_set('include_path',ini_get('include_path').':'.BASEPATH.'application/pear/');
  // on Windows
  // ini_set('include_path',ini_get('include_path').';'.BASEPATH.'application/pear/');
  

After:

class Pear_hook{
  
function index(){
  
// OS independent
  
ini_set('include_path',ini_get('include_path').PATH_SEPARATOR.APPPATH.'/pear/');
  
// on Apache
  // ini_set('include_path',ini_get('include_path').':'.APPPATH.'/pear/');
  // on Windows
  // ini_set('include_path',ini_get('include_path').';'.APPPATH.'/pear/');
  

I attempted to add this into the Wiki, but I don’t have the correct rights to do so!!

Profile
 
 
Posted: 13 May 2010 11:44 AM   [ Ignore ]   [ # 5 ]  
Grad Student
Avatar
Rank
Total Posts:  57
Joined  02-11-2010

Nice addition

Profile
 
 
Posted: 13 May 2010 01:30 PM   [ Ignore ]   [ # 6 ]  
Summer Student
Total Posts:  8
Joined  05-13-2010

Thanks. Since I can’t add it onto the Wiki hopefully it will be useful to someone else.

Works a treat too by the way, I’ve got Net_IPv4 working fine with my amended method.

Profile
 
 
Posted: 13 May 2010 04:01 PM   [ Ignore ]   [ # 7 ]  
Grad Student
Avatar
Rank
Total Posts:  57
Joined  02-11-2010

BTW in your comments you have on apache and on windows, is that apache and iis or linux and windows?

Profile
 
 
Posted: 15 May 2010 12:18 PM   [ Ignore ]   [ # 8 ]  
Summer Student
Total Posts:  8
Joined  05-13-2010

I just copied it out of the wiki and adjusted each instance, so I don’t know smile

Profile
 
 
Posted: 07 July 2010 09:22 AM   [ Ignore ]   [ # 9 ]  
Grad Student
Avatar
Rank
Total Posts:  57
Joined  02-11-2010

The original article mentioned copying files over, and I just tried this on my local machine (Mac osx) and I’m guessing this would work on a linux server as well. But instead of copying the PEAR directory (also thereby losing all the built in pear commands like update, etc) you can just symlink the pear folder into the application folder:

e.g. (Starting in your application directory from the command line)

ln -s /PATH TO PEAR/PEAR pear

Profile
 
 
Posted: 04 December 2010 08:06 AM   [ Ignore ]   [ # 10 ]  
Summer Student
Total Posts:  5
Joined  12-04-2010

Thanks. Since I can’t add it onto the Wiki hopefully it will be useful to someone else.

 Signature 

loteria

Profile
 
 
Posted: 07 February 2012 09:43 AM   [ Ignore ]   [ # 11 ]  
Summer Student
Total Posts:  5
Joined  02-07-2012

legal, muito obrigado pelo post!

http://www.comoperderabarriga.com.br

 Signature 

Como perder barriga é meu e-book de sucesso que vai lhe auxiliar a perder peso e emagrecer com saúde. Exercícios e dietas fazem você aprender a como perder barriga

Profile