Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. <?php
  2.  
  3.     /**
  4.     *
  5.     * PHP TOOLCASE HANDYMAN CLASS v 0.3
  6.     *
  7.     * PHP version 5
  8.     *
  9.     * @category     General
  10.     * @package      PhpToolCase
  11.     * @author       Irony <carlo@salapc.com>
  12.     * @license      http://www.gnu.org/copyleft/gpl.html GNU General Public License
  13.     * @link         http://www.w3tools.info/2011/09/php-handyman.html
  14.     *
  15.     * @changes:
  16.     *
  17.     *     -    changed the way the debug loader is called, added a new var for that purpouse "$debug"
  18.     *    -    changed the __constructor to check if a tool has been included and call $this->add($tool) in stead of toolsEngine()
  19.     *    -    added the possibility to add functions to the already initialized obejct
  20.     *    -    introduced array_push to pass the class as obj to external functions
  21.     *    -    added functionality to check if tool exists before addding to the toolBelt
  22.     *
  23.     */
  24.  
  25.     class PtcHandyMan            
  26.     {
  27.         /**
  28.         *
  29.         * @param array/string    $tool     adds classes to the main obejct, can be an array or a string
  30.         * @param boolean         $debug    start the debugger class :)
  31.         *
  32.         **/
  33.        
  34.         public function __construct($tool="",$debug=false)
  35.         {
  36.             if($debug==true)    # add ptcDebug class if present(for debugging only)
  37.            {
  38.                 if(class_exists('PtcDebug',true)){ PtcDebug::debugLoader(); }
  39.             }
  40.             if(!empty($tool)){ $this->add($tool); }
  41.         }
  42.        
  43.         public $toolBelt=array();    # A place to keep our tools
  44.        
  45.         /**
  46.         *
  47.         * @param string        $action    action param
  48.         * @param array/string     $tool    can be an array of classes/functions or just a string
  49.         *
  50.         **/
  51.        
  52.         protected function toolsEngine($action,$tool)
  53.         {
  54.             if($action!="remove" && array_key_exists($tool,$this->toolBelt)){ trigger_error($tool." already loaded!", E_USER_ERROR); }
  55.             else if($action=="addCls")                                                        # add class to toolbelt
  56.            {
  57.                 $this->toolBelt[get_class(new $tool)]=new $tool;
  58.             }
  59.             else if($action=="addFunc")                                                        # add function to toolbelt
  60.            {
  61.                 $this->toolBelt[$tool]=$tool;
  62.             }
  63.             else if($action=="remove")                                                        # remove tool from toolbelt
  64.            {
  65.                 if(is_array($tool))
  66.                 {
  67.                     $tools=$tool;
  68.                     foreach($tools as $tool){ unset($this->toolBelt[$tool]); }
  69.                 }
  70.                 else{ unset($this->toolBelt[$tool]); }
  71.             }
  72.             return $this;
  73.         }
  74.        
  75.         /**
  76.         *
  77.         * @param string        $method_name    method to call inside this scope
  78.         * @param string         $args            parameters to pass to method
  79.         *
  80.         **/
  81.        
  82.         public function __call($method_name,$args)
  83.         {
  84.             foreach($this->toolBelt as $tool)
  85.             {
  86.                 if(is_callable(array($tool,$method_name)))
  87.                 {
  88.                     return call_user_func_array(array($tool,$method_name),$args);
  89.                 }
  90.                 else if(in_array($method_name,$this->toolBelt))
  91.                 {
  92.                     array_push($args,$this);
  93.                     return call_user_func_array($method_name,$args);
  94.                 }
  95.                
  96.             }
  97.             trigger_error("Do not have a tool that can ".$method_name."!", E_USER_ERROR);
  98.         }
  99.        
  100.         /**
  101.         *
  102.         * @param array/string     $tool    add tools to the toolBelt, can be a function or a class
  103.         *
  104.         **/
  105.        
  106.         public function add($tool)        # add tools to already initialized class(can be an array of tools or just a tool)
  107.        {
  108.             if(is_array($tool))
  109.             {
  110.                 $tools=$tool;
  111.                 foreach($tools as $tool)
  112.                 {
  113.                     if(class_exists($tool)){ $this->toolsEngine('addCls',$tool); }
  114.                     else{ $this->toolsEngine('addFunc',$tool); }
  115.                 }
  116.             }
  117.             else if(class_exists($tool)){ $this->toolsEngine('addCls',$tool);}
  118.             else{ $this->toolsEngine('addFunc',$tool); }
  119.         }
  120.        
  121.         /**
  122.         *
  123.         * @param array/string     $tool    remove tools from the toolBelt
  124.         *
  125.         **/
  126.        
  127.         public function remove($tool)        # remove tools to already initialized class(can be an array of tools or just a tool)
  128.        {
  129.             if(is_array($tool))
  130.             {
  131.                 $tools=$tool;
  132.                 foreach($tools as $tool)
  133.                 {
  134.                     if(class_exists($tool)){ $this->toolsEngine('remove',$tool); }
  135.                     else{ $this->toolsEngine('remove',$tool); }
  136.                 }
  137.             }
  138.             else{ $this->toolsEngine('remove',$tool); }
  139.         }
  140.     }
  141.    
  142.     function __autoload($Name)        # Autoload classes, no need to include or require (does not work for functions)
  143.    {
  144.         $seperators=array('.', '-', '', '_');
  145.         $namingConventions=array('Ptc[SEP]'.$Name,$Name.'[SEP]Ptc',$Name);
  146.         $includePath=array
  147.         (
  148.             dirname(__FILE__).'/',        # path to directory with classes to be included
  149.            //'/secondary/path/'            # extra path for more classes
  150.         );
  151.         foreach($includePath as $path)
  152.         {
  153.             foreach($seperators as $sep)
  154.             {
  155.                 foreach($namingConventions as $convention)
  156.                 {
  157.                     if(is_file($file=$path.str_replace('[SEP]',$sep,$convention).'.php')){ require_once($file); }
  158.                 }
  159.             }
  160.         }
  161.     }
  162.  ?>
  163.