Return to Snippet

Revision: 1381
at October 3, 2006 05:03 by ekobudisetiyo


Initial Code
<?php
class fuselogic_include_path
{
   function __construct($path = null)
   {
      if(!isset($this->setting_name))
      {
         $this->setting_name = 'include_path';
         $this->_get_separator();
      }
      if(isset($path)) $this->add($path);
   }

   function add($variables)
   {
      $array = Array();
      if(is_array($variables))
      {
         $array = array_merge($this->to_array(),$variables);
      }else
      {
         $array = array_merge($this->to_array(),array($variables));
      }
        
      $this->clear();
      $this->_unlink('.');
      for($i=0;$i<count($array);$i++) $array[$i] = str_replace('\\','/',$array[$i]);
      $unique  = array_unique($array);
      foreach($unique as $variable)
      {
        if($variable !== '.') $this->_link($variable);
      }
   }

   function remove($variables)
   {
      if(is_array($variables))
      {
         foreach($variables as $variable) $this->_unlink($variable);
      }else
      {
         $this->_unlink($variables);
      }
   }
    
   function clear()
   {
      ini_set($this->setting_name,'.');
   }
                
   function to_array()
   {
      return explode($this->_separator,ini_get($this->setting_name));
   }

   function get_setting($setting = '')
   {
      if(empty($setting)) $setting = ini_get($this->setting_name);
      $setting = str_replace('\\','/',$setting);
      return $setting;
   }

   function _link($directory = '')
   {
      if(!empty($directory))
      {
         $directory = str_replace('\\','/',$directory);
         $setting = $this->get_setting();
         $setting = str_replace($this->_separator.$directory,'',$setting);
         $setting .= $this->_separator.$directory;
         ini_set($this->setting_name,$setting);
      }
   }

   function _get_separator()
   {
      $count = substr_count(ini_get($this->setting_name),';');
      if($count > 0)
      {
         $this->_separator = ';';
      }else{
         $this->_separator = ':';
      }
   }

   function _unlink($directory = '')
   {
      if(!empty($directory))
      {
         $directory = str_replace('\\','/',$directory);
         $setting = $this->get_setting();
         $setting = str_replace($directory,'',$setting);
         $setting = str_replace($this->_separator.$this->_separator,$this->_separator,$setting);
         $setting .= $this->_separator.$this->_separator;
         $setting = str_replace($this->_separator.$this->_separator.$this->_separator,'',$setting);
         $setting = str_replace($this->_separator.$this->_separator,'',$setting);
         ini_set($this->setting_name,$setting);
      }
   }
}
?>

Initial URL
http://fuselogic.haltebis.com

Initial Description
Include Path PHP 5 class.

Usage:
$path = new fuselogic_include_path();
$path->add('/home/ekobudi/www/fuselogic');
$path->add('/home/ekobudi/www/pear');
$path->remove('/home/ekobudi/www/old_fuselgic');

Initial Title
PHP5 Include Path

Initial Tags
php

Initial Language
PHP