[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/core/src/ -> lmbDelegate.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  
  10  /**
  11   * Object form of invoking an object method
  12   * @package core
  13   * @version $Id$
  14   */
  15  class lmbDelegate
  16  {
  17    /**

  18    * @var mixed PHP callback

  19    */
  20    protected $php_callback;
  21    /**

  22     * @var bool cached validity check result

  23     */
  24    protected $is_valid;
  25  
  26    /**

  27    * Constructor.

  28    * @param mixed Object which method will be invoked

  29    * @param string Object method to call

  30    */
  31    function __construct($object, $method = null)
  32    {
  33      if(is_array($object))
  34      {
  35        $this->php_callback = $object;
  36      }
  37      else
  38      {
  39        if(!$method)
  40          $this->php_callback = $object;
  41        else
  42          $this->php_callback = array($object, $method);
  43      }
  44    }
  45  
  46    /**

  47     * Returns PHP callback

  48     * @return mixed PHP callback

  49     */
  50    function getCallback()
  51    {
  52      return $this->php_callback;
  53    }
  54  
  55    /**

  56    * Invokes object method with $args

  57    */
  58    function invoke()
  59    {
  60      if(!$this->isValid())
  61        throw new lmbException("Invalid callback", array('callback' => $this->php_callback));
  62  
  63      $args = func_get_args();
  64      return call_user_func_array($this->php_callback, $args);
  65    }
  66  
  67    function invokeArray($args = array())
  68    {
  69      if(!$this->isValid())
  70        throw new lmbException("Invalid callback", array('callback' => $this->php_callback));
  71      return call_user_func_array($this->php_callback, $args);
  72    }
  73  
  74    function isValid()
  75    {
  76      if($this->is_valid !== null)
  77        return $this->is_valid;
  78      $this->is_valid = is_callable($this->php_callback);
  79      return $this->is_valid;
  80    }
  81  
  82    static function objectify($delegate)
  83    {
  84      if(is_object($delegate) && $delegate instanceof lmbDelegate)
  85        return $delegate;
  86      return new lmbDelegate($delegate);
  87    }
  88  
  89    /**

  90    * Invokes all delegates in a list with some args

  91    * @param array Array of lmbDelegate objects that

  92    * @param array Invoke arguments

  93    */
  94    static function invokeAll($list, $args = array())
  95    {
  96      foreach($list as $item)
  97        $item->invokeArray($args);
  98    }
  99  
 100    /**

 101    * Invokes delegates in a list one by one. Stops invoking if delegate return a not null result.

 102    * @param array Array of lmbDelegate objects

 103    * @param array Invoke arguments

 104    */
 105    static function invokeChain($list, $args = array())
 106    {
 107      foreach($list as $item)
 108      {
 109        $result = $item->invokeArray($args);
 110        if(!is_null($result))
 111          return $result;
 112      }
 113    }
 114  }
 115  ?>


Generated: Fri Aug 29 04:49:26 2008 Cross-referenced by PHPXref 0.7