Created
December 1, 2010 15:31
-
-
Save stdclass/723639 to your computer and use it in GitHub Desktop.
callerProxy.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Caller Proxy Pattern | |
* author: [email protected] | |
**/ | |
class callerProxyException extends Exception{} | |
interface iCallerProxy{ | |
public static function add( $name, $proxy ); | |
public static function rename( $name, $proxy ); | |
public static function remove( $name ); | |
public static function checkPassthrough( ); | |
} | |
class callerProxy implements iCallerProxy{ | |
private static $proxies = array(); | |
private function __construct(){} | |
private function __clone(){} | |
public static function add( $name, $proxy ){ | |
if( ! self::$proxies[ $name ] ) | |
self::_add( $name, $proxy ); | |
else | |
return self::_error( "Proxy '$name' already exists" ); | |
} | |
public static function remove( $name ){ | |
if( self::$proxies[ $name ] ){ | |
unset( self::$proxies[ $name ] ); | |
return true; | |
} | |
return false; | |
} | |
public static function rename( $name, $proxy ){ | |
if( self::$proxies[ $name ] ){ | |
self::$proxies[ $name ] = $proxy; | |
return true; | |
} | |
return false; | |
} | |
private static function _add( $name, $proxy, $rename = false ){ | |
self::$proxies[ $name ] = $proxy; | |
if( function_exists( $name ) ) | |
return self::_error( "Function '$name' already exists" ); | |
if( ctype_alpha( $name ) ) | |
eval( "function $name(){ callerProxy::__passThrough(); }"); | |
else | |
return self::_error( "Invalid Function Name '$name' "); | |
} | |
public static function __passThrough(){ | |
$trace = debug_backtrace(); | |
$call = $trace[1]; | |
if( self::$proxies[ $call[ "function" ] ] ) | |
call_user_func_array( self::$proxies[ $call[ "function" ] ], $call[ "args" ] ); | |
else | |
return self::_error( "Function '{$call["function"]}' does not exist" ); | |
} | |
public static function checkPassthrough(){ | |
$trace = debug_backtrace(); | |
if( $trace[ 3 ][ "function" ] != "__passThrough" ) | |
return self::_error( "Invalid Function Call" ); | |
return true; | |
} | |
private static function _error( $message ){ | |
throw new callerProxyException( $message ); | |
return false; | |
} | |
} | |
/*************************************************** | |
* usage * | |
***************************************************/ | |
function orginal_test( $name ){ | |
callerProxy::checkPassthrough(); | |
echo "name: $name<br />"; | |
} | |
function another_test( $name ){ | |
callerProxy::checkPassthrough(); | |
echo "another name: $name<br />"; | |
} | |
/* add Proxy | |
***************************************************/ | |
callerProxy::add( "Test", "orginal_test" ); | |
Test( "test" ); | |
/* rename Proxy | |
***************************************************/ | |
callerProxy::rename( "Test", "another_test" ); | |
Test( "another test" ); | |
/* remove Proxy | |
***************************************************/ | |
callerProxy::remove( "Test" ); | |
try{ | |
Test( "another test" ); | |
}catch( callerProxyException $e ){ | |
echo "something went wrong: <b>" . $e->getMessage() . "</b><br />"; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment