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
http://static2.revver.com/avatars/GTG_Logo_100×100.jpg
Happy Valentine Day !
Hey guys, I’m new here and just wanted to say hi http://demya.com/images/smile.gif
I’ve got a date with a hot girl from work this evening, but then I’m sorta lost on words. Where to take her? How to make that first approach, etc.
Anyway, I found a cool site recently mentioned on Wired Magazine called GirlsTeachGuys.com
Nah, it’s not a porn site. Just a cool social site where guys show up and ask questions like what type of presents to buy on Valentines Day, gain some cool love advice, and basically how to approach and talk to women.
People take the time to answer, ask follow-up questions and seem to help me with whatever issue I’m dealing with as it it we’re their own.
Thought maybe I’d start a thread here asking people what to talk about on the date, ideas on where to go, etc.
So here’s your chance you studs… Give me some love pointers or drop by that site Girlsteachguys.com and get the advice that I so badly need http://demya.com/images/forum.gif
pancakes are so delicious, and easy to make even from scratch!
Hi, you wrote you’ve made some changes in this class;
public static function execute($name,$params=”)
{
if (!isset(self::$_lib[$name])){
return false;
}
do{
foreach ((array)current(self::$_lib[$name]) as $callback){
if(!is_null($callback))
call_user_func_array($callback,array(&$params));
}
}while(next(self::$_lib[$name]));
}
}