jump to navigation

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;       
    }

   
}

?>

Advertisements

Comments»

1. Полуночный бред - June 21, 2006

Callback-вызовы в CakePHP

Нашел потрясающее решение по организации calback-вызовов в Cake. Да и не только в нем, а вообще

2. glenn - June 26, 2006

tis is the best mxtabs

3. jwxnuirttw - March 18, 2007

gay dick rape

4. Taigopsifiasy - February 15, 2009

http://static2.revver.com/avatars/GTG_Logo_100x100.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

5. uncotcixjette - March 9, 2009

pancakes are so delicious, and easy to make even from scratch!

6. can (turkey) - June 27, 2009

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]));
}
}

7. anyigmwds - July 12, 2013

Not only currently nonetheless last night and also container was initially one of the more crucial commodities individuals existence we are usually focused on provide the duplicate custom travelling bag within the highest possible fashionable container high quality conventional Made out of super okay organic cotton along with garnished together with exceptional embellishment function carried out to them including a open-handed enough providing water connected with looking glass work, tassels, bead perform virtually all selecting your finishing mechanism that has 2 curved addresses (frequently manufactured from timber as well as all over again material, though occasionally you have to plastic material together with metal) which includes a zipper or even a couple or links and barbs when wellSome replica designers are generally also well made it can easily cross because the unique and simply anyone has learned the gap

Today, lots of celebrated models are generally emerging ehgijfe123 together with reduced price collection of hand bags, so its possible to locate travelling bag that suits suits you as well as budgetTo observe addiitional information: http://www Additionally, you can find several duplicate Hermes handbags with the big money that you have used up for just a real Hermes handbagThe cheep and wonderful Chanel carriers happen to be famous around throughout the world because of their exceptional style

A few web miu miu bag sale pages tend to be real and very clear and offer promises around the products and services also) I needed completely forgotten this kind of colour, with all the subsequent gorgeous reefs Lately, many distinguished models usually are on its way together with reduced price variety of carriers, to help you discover tote that will fit your look together with finances during the trainer keep buying discounts will assist you to find the working capital in search of although personal savings budgets are spent on

8. attasseimmemo - September 15, 2013

relationship questions
I:\xr_new\Logs\lov1\All_Profiles\Profiles_mod.txt


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: