Source for file Callback.php

Documentation is available at Callback.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  7. 7:  * @license    http://nettephp.com/license  Nette license
  8. 8:  * @link       http://nettephp.com
  9. 9:  * @category   Nette
  10. 10:  * @package    Nette
  11. 11:  */
  12. 12:  
  13. 13:  
  14. 14:  
  15. 15: /**
  16. 16:  * PHP callback encapsulation.
  17. 17:  *
  18. 18:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  19. 19:  * @package    Nette
  20. 20:  */
  21. 21: final class Callback extends Object
  22. 22: {
  23. 23:     /** @var callback */
  24. 24:     private $cb;
  25. 25:     
  26. 26:     /** @var bool */
  27. 27:     public static $fix520;
  28. 28:     
  29. 29:     /** @var bool */
  30. 30:     public static $checkImmediately FALSE;
  31. 31:  
  32. 32:  
  33. 33:  
  34. 34:     /**
  35. 35:      * @param  mixed   class, object, function, callback
  36. 36:      * @param  string  method
  37. 37:      */
  38. 38:     public function __construct($t$m NULL)
  39. 39:     {
  40. 40:         if ($m === NULL{
  41. 41:             $this->cb $t;
  42. 42:         else {
  43. 43:             $this->cb array($t$m);
  44. 44:         }
  45. 45:  
  46. 46:         
  47. 47:         // __invoke support
  48. 48:         if (is_object($this->cb)) {
  49. 49:             $this->cb array($this->cb'__invoke');
  50. 50:  
  51. 51:         elseif (self::$fix520{
  52. 52:             // explode 'Class::method' into array
  53. 53:             if (is_string($this->cb&& strpos($this->cb':')) {
  54. 54:                 $this->cb explode('::'$this->cb);
  55. 55:             }
  56. 56:  
  57. 57:             // remove class namespace
  58. 58:             if (is_array($this->cb&& is_string($this->cb[0]&& $a strrpos($this->cb[0]'\\')) {
  59. 59:                 $this->cb[0substr($this->cb[0]$a 1);
  60. 60:             }
  61. 61:  
  62. 62:         else {
  63. 63:             // remove class namespace
  64. 64:             if (is_string($this->cb&& $a strrpos($this->cb'\\')) {
  65. 65:                 $this->cb substr($this->cb$a 1);
  66. 66:  
  67. 67:             elseif (is_array($this->cb&& is_string($this->cb[0]&& $a strrpos($this->cb[0]'\\')) {
  68. 68:                 $this->cb[0substr($this->cb[0]$a 1);
  69. 69:             }
  70. 70:         }
  71. 71:         
  72. 72:  
  73. 73:         if (!is_callable($this->cb!self::$checkImmediately)) {
  74. 74:             throw new InvalidArgumentException("Invalid callback.");
  75. 75:         }
  76. 76:     }
  77. 77:  
  78. 78:  
  79. 79:  
  80. 80:     /**
  81. 81:      * Invokes callback. Do not call directly.
  82. 82:      * @return mixed 
  83. 83:      */
  84. 84:     public function __invoke()
  85. 85:     {
  86. 86:         if (!is_callable($this->cb)) {
  87. 87:             throw new InvalidStateException("Callback '$this' is not callable.");
  88. 88:         }
  89. 89:         $args func_get_args();
  90. 90:         return call_user_func_array($this->cb$args);
  91. 91:     }
  92. 92:  
  93. 93:  
  94. 94:  
  95. 95:     /**
  96. 96:      * Invokes callback.
  97. 97:      * @return mixed 
  98. 98:      */
  99. 99:     public function invoke()
  100. 100:     {
  101. 101:         if (!is_callable($this->cb)) {
  102. 102:             throw new InvalidStateException("Callback '$this' is not callable.");
  103. 103:         }
  104. 104:         $args func_get_args();
  105. 105:         return call_user_func_array($this->cb$args);
  106. 106:     }
  107. 107:  
  108. 108:  
  109. 109:  
  110. 110:     /**
  111. 111:      * Invokes callback with an array of parameters.
  112. 112:      * @param  array 
  113. 113:      * @return mixed 
  114. 114:      */
  115. 115:     public function invokeArgs(array $args)
  116. 116:     {
  117. 117:         if (!is_callable($this->cb)) {
  118. 118:             throw new InvalidStateException("Callback '$this' is not callable.");
  119. 119:         }
  120. 120:         return call_user_func_array($this->cb$args);
  121. 121:     }
  122. 122:  
  123. 123:  
  124. 124:  
  125. 125:     /**
  126. 126:      * Verifies that callback can be called.
  127. 127:      * @return bool 
  128. 128:      */
  129. 129:     public function isCallable()
  130. 130:     {
  131. 131:         return is_callable($this->cb);
  132. 132:     }
  133. 133:  
  134. 134:  
  135. 135:  
  136. 136:     /**
  137. 137:      * Returns PHP callback pseudotype.
  138. 138:      * @return callback 
  139. 139:      */
  140. 140:     public function getNative()
  141. 141:     {
  142. 142:         return $this->cb;
  143. 143:     }
  144. 144:  
  145. 145:  
  146. 146:  
  147. 147:     /**
  148. 148:      * @return string 
  149. 149:      */
  150. 150:     public function __toString()
  151. 151:     {
  152. 152:         is_callable($this->cbTRUE$textual);
  153. 153:         return $textual;
  154. 154:     }
  155. 155:  
  156. 157:  
  157. 158:  
  158. 159:  
  159. 160: Callback::$fix520 version_compare(PHP_VERSION '5.2.2''<');