Register Global Callback functions (updated) June 18, 2006
Posted by rossoft in CakePHP.trackback
That's an idea after reading this post. The helpers / components / etc. can have global callback functions, that functions are registered in another component / helper / vendor class.
For Example: in my extension to CakeLog class, you can do:
//Callback extension
vendor('callbacks/callbacks');
$callbacks=& Callbacks::get_instance();
$ret=$callbacks->execute('CakeLog',array('that is a param',55);
if ($ret) $output.=join("\n",$ret);
It executes all the callbacks registered with the name 'CakeLog' with 2 parameter values: a string and a number.
Then in your beforeFilter of your AppController, you can do:
vendor('callbacks/callbacks');
$callbacks=& Callbacks::get_instance();
$code='return "some code here";';
$callbacks->register_anonymous('CakeLog','$param1,$param2',$code);
$code='return "Your params: $param1 - $param2";';
$callbacks->register_anonymous('CakeLog','$param1,$param2',$code);
Then when you log something with $this->log('something'), the result will be:
18-06-06 10:18:51 [ /app/controllers/test_controller.php:36 TestController->myaction() ] Error: something
some code here
Your params: that is a param - 55
Also you can use $callbacks->register_method('CakeLog',$this,'beforeLog');
and create the method beforeLog() in the controller for be called before logging.
Copy to vendors/callbacks/callbacks.php
<?php
/**
* Callbacks Class
* @author RosSoft
* @version 0.2
* @license MIT
*/
class Callbacks
{
var $_lib=array();
/**
* Singleton instance
* @return object Callbacks
*/
function & get_instance()
{
static $instance=array(); //must be an array for doing singleton
if (! $instance)
{
$instance[0]=& new Callbacks();
}
return $instance[0];
}
/**
* Register an anonymous callback function (function dynamically created)
*
* @param string $name Callback name
* (you can register more than one callback with same name, they will be executed in register order)
* @param string $params Parameters names like '$name,$value' (always single quotes!)
* @param string $code PHP Code of the function
*
* Example:
* register_anonymous('CakeLog','$a,$b','return "Value of a: $a - Value of b: $b" )
*/
function register_anonymous($name,$params,$code)
{
$this->_register($name,create_function($params,$code));
}
/**
* Register an object method callback
*
* @param string $name Callback name
* @param object $object Instance of the class object
* @param string $method Name of method of the object
*
* Example:
* register_method('CakeLog',$this,'test') : will register $this->test
*/
function register_method($name,&$object,$method)
{
$this->_register($name,array($object,$method));
}
/**
* Execute a callback name
* @param string $name Callback name
* @param array $params Array of parameter values like array('john',606)
* @return array. Array of returned values by all the callbacks with the given name. If null, then no callback registered
*/
function execute($name,$params=array())
{
if (isset($this->_lib[$name]))
{
$ret=array();
foreach ($this->_lib[$name] as $callback)
{
$ret[]=call_user_func_array($callback,$params);
}
return $ret;
}
else
{
return null;
}
}
/**
* Registers a function
* @param string $name Callback name
* @param mixed $function Callback function
*/
function _register($name,$function)
{
if (!isset($this->_lib[$name]))
{
$this->_lib[$name]=array();
}
$this->_lib[$name][]=& $function;
}
}
?>
Callback-вызовы в CakePHP
Нашел потрясающее решение по организации calback-вызовов в Cake. Да и не только в нем, а вообще
…
tis is the best mxtabs
gay dick rape