<?php
/**
*
* PHP TOOLCASE HANDYMAN CLASS v 0.3
*
* PHP version 5
*
* @category General
* @package PhpToolCase
* @author Irony <carlo@salapc.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
* @link http://www.w3tools.info/2011/09/php-handyman.html
*
* @changes:
*
* - changed the way the debug loader is called, added a new var for that purpouse "$debug"
* - changed the __constructor to check if a tool has been included and call $this->add($tool) in stead of toolsEngine()
* - added the possibility to add functions to the already initialized obejct
* - introduced array_push to pass the class as obj to external functions
* - added functionality to check if tool exists before addding to the toolBelt
*
*/
class PtcHandyMan
{
/**
*
* @param array/string $tool adds classes to the main obejct, can be an array or a string
* @param boolean $debug start the debugger class :)
*
**/
public function __construct($tool="",$debug=false)
{
if($debug==true) # add ptcDebug class if present(for debugging only)
{
if(class_exists('PtcDebug',true)){ PtcDebug::debugLoader(); }
}
if(!empty($tool)){ $this->add($tool); }
}
public $toolBelt=array(); # A place to keep our tools
/**
*
* @param string $action action param
* @param array/string $tool can be an array of classes/functions or just a string
*
**/
protected function toolsEngine($action,$tool)
{
if($action!="remove" && array_key_exists($tool,$this->toolBelt)){ trigger_error($tool." already loaded!", E_USER_ERROR); }
else if($action=="addCls") # add class to toolbelt
{
$this->toolBelt[get_class(new $tool)]=new $tool;
}
else if($action=="addFunc") # add function to toolbelt
{
$this->toolBelt[$tool]=$tool;
}
else if($action=="remove") # remove tool from toolbelt
{
if(is_array($tool))
{
$tools=$tool;
foreach($tools as $tool){ unset($this->toolBelt[$tool]); }
}
else{ unset($this->toolBelt[$tool]); }
}
return $this;
}
/**
*
* @param string $method_name method to call inside this scope
* @param string $args parameters to pass to method
*
**/
public function __call($method_name,$args)
{
foreach($this->toolBelt as $tool)
{
if(is_callable(array($tool,$method_name)))
{
return call_user_func_array(array($tool,$method_name),$args);
}
else if(in_array($method_name,$this->toolBelt))
{
array_push($args,$this);
return call_user_func_array($method_name,$args);
}
}
trigger_error("Do not have a tool that can ".$method_name."!", E_USER_ERROR);
}
/**
*
* @param array/string $tool add tools to the toolBelt, can be a function or a class
*
**/
public function add($tool) # add tools to already initialized class(can be an array of tools or just a tool)
{
if(is_array($tool))
{
$tools=$tool;
foreach($tools as $tool)
{
if(class_exists($tool)){ $this->toolsEngine('addCls',$tool); }
else{ $this->toolsEngine('addFunc',$tool); }
}
}
else if(class_exists($tool)){ $this->toolsEngine('addCls',$tool);}
else{ $this->toolsEngine('addFunc',$tool); }
}
/**
*
* @param array/string $tool remove tools from the toolBelt
*
**/
public function remove($tool) # remove tools to already initialized class(can be an array of tools or just a tool)
{
if(is_array($tool))
{
$tools=$tool;
foreach($tools as $tool)
{
if(class_exists($tool)){ $this->toolsEngine('remove',$tool); }
else{ $this->toolsEngine('remove',$tool); }
}
}
else{ $this->toolsEngine('remove',$tool); }
}
}
function __autoload($Name) # Autoload classes, no need to include or require (does not work for functions)
{
$seperators=array('.', '-', '', '_');
$namingConventions=array('Ptc[SEP]'.$Name,$Name.'[SEP]Ptc',$Name);
$includePath=array
(
dirname(__FILE__).'/', # path to directory with classes to be included
//'/secondary/path/' # extra path for more classes
);
foreach($includePath as $path)
{
foreach($seperators as $sep)
{
foreach($namingConventions as $convention)
{
if(is_file($file=$path.str_replace('[SEP]',$sep,$convention).'.php')){ require_once($file); }
}
}
}
}
?>