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:  
  30. 30:  
  31. 31:  
  32. 32:     /**
  33. 33:      * @param  mixed   class, object, function, callback
  34. 34:      * @param  string  method
  35. 35:      */
  36. 36:     public function __construct($t$m NULL)
  37. 37:     {
  38. 38:         if ($m === NULL{
  39. 39:             $this->cb $t;
  40. 40:         else {
  41. 41:             $this->cb array($t$m);
  42. 42:         }
  43. 43:  
  44. 44:         
  45. 45:         // __invoke support
  46. 46:         if (is_object($this->cb)) {
  47. 47:             $this->cb array($this->cb'__invoke');
  48. 48:  
  49. 49:         elseif (self::$fix520{
  50. 50:             // explode 'Class::method' into array
  51. 51:             if (is_string($this->cb&& strpos($this->cb':')) {
  52. 52:                 $this->cb explode('::'$this->cb);
  53. 53:             }
  54. 54:  
  55. 55:             // remove class namespace
  56. 56:             if (is_array($this->cb&& is_string($this->cb[0]&& $a strrpos($this->cb[0]'\\')) {
  57. 57:                 $this->cb[0substr($this->cb[0]$a 1);
  58. 58:             }
  59. 59:  
  60. 60:         else {
  61. 61:             // remove class namespace
  62. 62:             if (is_string($this->cb&& $a strrpos($this->cb'\\')) {
  63. 63:                 $this->cb substr($this->cb$a 1);
  64. 64:  
  65. 65:             elseif (is_array($this->cb&& is_string($this->cb[0]&& $a strrpos($this->cb[0]'\\')) {
  66. 66:                 $this->cb[0substr($this->cb[0]$a 1);
  67. 67:             }
  68. 68:         }
  69. 69:         
  70. 70:  
  71. 71:         if (!is_callable($this->cbTRUE)) {
  72. 72:             throw new InvalidArgumentException("Invalid callback.");
  73. 73:         }
  74. 74:     }
  75. 75:  
  76. 76:  
  77. 77:  
  78. 78:     /**
  79. 79:      * Invokes callback. Do not call directly.
  80. 80:      * @return mixed 
  81. 81:      */
  82. 82:     public function __invoke()
  83. 83:     {
  84. 84:         if (!is_callable($this->cb)) {
  85. 85:             throw new InvalidStateException("Callback '$this' is not callable.");
  86. 86:         }
  87. 87:         $args func_get_args();
  88. 88:         return call_user_func_array($this->cb$args);
  89. 89:     }
  90. 90:  
  91. 91:  
  92. 92:  
  93. 93:     /**
  94. 94:      * Invokes callback.
  95. 95:      * @return mixed 
  96. 96:      */
  97. 97:     public function invoke()
  98. 98:     {
  99. 99:         if (!is_callable($this->cb)) {
  100. 100:             throw new InvalidStateException("Callback '$this' is not callable.");
  101. 101:         }
  102. 102:         $args func_get_args();
  103. 103:         return call_user_func_array($this->cb$args);
  104. 104:     }
  105. 105:  
  106. 106:  
  107. 107:  
  108. 108:     /**
  109. 109:      * Invokes callback with an array of parameters.
  110. 110:      * @param  array 
  111. 111:      * @return mixed 
  112. 112:      */
  113. 113:     public function invokeArgs(array $args)
  114. 114:     {
  115. 115:         if (!is_callable($this->cb)) {
  116. 116:             throw new InvalidStateException("Callback '$this' is not callable.");
  117. 117:         }
  118. 118:         return call_user_func_array($this->cb$args);
  119. 119:     }
  120. 120:  
  121. 121:  
  122. 122:  
  123. 123:     /**
  124. 124:      * Verifies that callback can be called.
  125. 125:      * @return bool 
  126. 126:      */
  127. 127:     public function isCallable()
  128. 128:     {
  129. 129:         return is_callable($this->cb);
  130. 130:     }
  131. 131:  
  132. 132:  
  133. 133:  
  134. 134:     /**
  135. 135:      * Returns PHP callback pseudotype.
  136. 136:      * @return callback 
  137. 137:      */
  138. 138:     public function getNative()
  139. 139:     {
  140. 140:         return $this->cb;
  141. 141:     }
  142. 142:  
  143. 143:  
  144. 144:  
  145. 145:     /**
  146. 146:      * @return string 
  147. 147:      */
  148. 148:     public function __toString()
  149. 149:     {
  150. 150:         is_callable($this->cbTRUE$textual);
  151. 151:         return $textual;
  152. 152:     }
  153. 153:  
  154. 155:  
  155. 156:  
  156. 157:  
  157. 158: Callback::$fix520 version_compare(PHP_VERSION '5.2.2''<');