CallbackHandler.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Stdlib
  17. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * CallbackHandler
  22. *
  23. * A handler for a event, event, filterchain, etc. Abstracts PHP callbacks,
  24. * primarily to allow for lazy-loading and ensuring availability of default
  25. * arguments (currying).
  26. *
  27. * @category Zend
  28. * @package Zend_Stdlib
  29. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Stdlib_CallbackHandler
  33. {
  34. /**
  35. * @var string|array PHP callback to invoke
  36. */
  37. protected $callback;
  38. /**
  39. * Did an error occur when testing the validity of the callback?
  40. * @var bool
  41. */
  42. protected $error = false;
  43. /**
  44. * Callback metadata, if any
  45. * @var array
  46. */
  47. protected $metadata;
  48. /**
  49. * Constructor
  50. *
  51. * @param string $event Event to which slot is subscribed
  52. * @param string|array|object $callback PHP callback
  53. * @param array $options Options used by the callback handler (e.g., priority)
  54. * @return void
  55. */
  56. public function __construct($callback, array $metadata = array())
  57. {
  58. $this->metadata = $metadata;
  59. $this->registerCallback($callback);
  60. }
  61. /**
  62. * Error handler
  63. *
  64. * Used by registerCallback() when calling is_callable() to capture engine warnings.
  65. *
  66. * @param int $errno
  67. * @param string $errstr
  68. * @return void
  69. */
  70. public function errorHandler($errno, $errstr)
  71. {
  72. $this->error = true;
  73. }
  74. /**
  75. * Registers the callback provided in the constructor
  76. *
  77. * If you have pecl/weakref {@see http://pecl.php.net/weakref} installed,
  78. * this method provides additional behavior.
  79. *
  80. * If a callback is a functor, or an array callback composing an object
  81. * instance, this method will pass the object to a WeakRef instance prior
  82. * to registering the callback.
  83. *
  84. * @param Callable $callback
  85. * @return void
  86. */
  87. protected function registerCallback($callback)
  88. {
  89. set_error_handler(array($this, 'errorHandler'), E_STRICT);
  90. $callable = is_callable($callback);
  91. restore_error_handler();
  92. if (!$callable || $this->error) {
  93. require_once 'Zend/Stdlib/Exception/InvalidCallbackException.php';
  94. throw new Zend_Stdlib_Exception_InvalidCallbackException('Invalid callback provided; not callable');
  95. }
  96. // If pecl/weakref is not installed, simply store the callback and return
  97. if (!class_exists('WeakRef')) {
  98. $this->callback = $callback;
  99. return;
  100. }
  101. // If WeakRef exists, we want to use it.
  102. // If we have a non-closure object, pass it to WeakRef, and then
  103. // register it.
  104. if (is_object($callback) && !$callback instanceof Closure) {
  105. $this->callback = new WeakRef($callback);
  106. return;
  107. }
  108. // If we have a string or closure, register as-is
  109. if (!is_array($callback)) {
  110. $this->callback = $callback;
  111. return;
  112. }
  113. list($target, $method) = $callback;
  114. // If we have an array callback, and the first argument is not an
  115. // object, register as-is
  116. if (!is_object($target)) {
  117. $this->callback = $callback;
  118. return;
  119. }
  120. // We have an array callback with an object as the first argument;
  121. // pass it to WeakRef, and then register the new callback
  122. $target = new WeakRef($target);
  123. $this->callback = array($target, $method);
  124. }
  125. /**
  126. * Retrieve registered callback
  127. *
  128. * @return Callable
  129. */
  130. public function getCallback()
  131. {
  132. $callback = $this->callback;
  133. // String callbacks -- simply return
  134. if (is_string($callback)) {
  135. return $callback;
  136. }
  137. // WeakRef callbacks -- pull it out of the object and return it
  138. if ($callback instanceof WeakRef) {
  139. return $callback->get();
  140. }
  141. // Non-WeakRef object callback -- return it
  142. if (is_object($callback)) {
  143. return $callback;
  144. }
  145. // Array callback with WeakRef object -- retrieve the object first, and
  146. // then return
  147. list($target, $method) = $callback;
  148. if ($target instanceof WeakRef) {
  149. return array($target->get(), $method);
  150. }
  151. // Otherwise, return it
  152. return $callback;
  153. }
  154. /**
  155. * Invoke handler
  156. *
  157. * @param array $args Arguments to pass to callback
  158. * @return mixed
  159. */
  160. public function call(array $args = array())
  161. {
  162. $callback = $this->getCallback();
  163. $isPhp54 = version_compare(PHP_VERSION, '5.4.0rc1', '>=');
  164. if ($isPhp54 && is_string($callback)) {
  165. $this->validateStringCallbackFor54($callback);
  166. }
  167. // Minor performance tweak; use call_user_func() until > 3 arguments
  168. // reached
  169. switch (count($args)) {
  170. case 0:
  171. if ($isPhp54) {
  172. return $callback();
  173. }
  174. return call_user_func($callback);
  175. case 1:
  176. if ($isPhp54) {
  177. return $callback(array_shift($args));
  178. }
  179. return call_user_func($callback, array_shift($args));
  180. case 2:
  181. $arg1 = array_shift($args);
  182. $arg2 = array_shift($args);
  183. if ($isPhp54) {
  184. return $callback($arg1, $arg2);
  185. }
  186. return call_user_func($callback, $arg1, $arg2);
  187. case 3:
  188. $arg1 = array_shift($args);
  189. $arg2 = array_shift($args);
  190. $arg3 = array_shift($args);
  191. if ($isPhp54) {
  192. return $callback($arg1, $arg2, $arg3);
  193. }
  194. return call_user_func($callback, $arg1, $arg2, $arg3);
  195. default:
  196. return call_user_func_array($callback, $args);
  197. }
  198. }
  199. /**
  200. * Invoke as functor
  201. *
  202. * @return mixed
  203. */
  204. public function __invoke()
  205. {
  206. return $this->call(func_get_args());
  207. }
  208. /**
  209. * Get all callback metadata
  210. *
  211. * @return array
  212. */
  213. public function getMetadata()
  214. {
  215. return $this->metadata;
  216. }
  217. /**
  218. * Retrieve a single metadatum
  219. *
  220. * @param string $name
  221. * @return mixed
  222. */
  223. public function getMetadatum($name)
  224. {
  225. if (array_key_exists($name, $this->metadata)) {
  226. return $this->metadata[$name];
  227. }
  228. return null;
  229. }
  230. /**
  231. * Validate a static method call
  232. *
  233. * Validates that a static method call in PHP 5.4 will actually work
  234. *
  235. * @param string $callback
  236. * @return true
  237. * @throws Zend_Stdlib_Exception_InvalidCallbackException if invalid
  238. */
  239. protected function validateStringCallbackFor54($callback)
  240. {
  241. if (!strstr($callback, '::')) {
  242. return true;
  243. }
  244. list($class, $method) = explode('::', $callback, 2);
  245. if (!class_exists($class)) {
  246. require_once 'Zend/Stdlib/Exception/InvalidCallbackException.php';
  247. throw new Zend_Stdlib_Exception_InvalidCallbackException(sprintf(
  248. 'Static method call "%s" refers to a class that does not exist',
  249. $callback
  250. ));
  251. }
  252. $r = new ReflectionClass($class);
  253. if (!$r->hasMethod($method)) {
  254. require_once 'Zend/Stdlib/Exception/InvalidCallbackException.php';
  255. throw new Zend_Stdlib_Exception_InvalidCallbackException(sprintf(
  256. 'Static method call "%s" refers to a method that does not exist',
  257. $callback
  258. ));
  259. }
  260. $m = $r->getMethod($method);
  261. if (!$m->isStatic()) {
  262. require_once 'Zend/Stdlib/Exception/InvalidCallbackException.php';
  263. throw new Zend_Stdlib_Exception_InvalidCallbackException(sprintf(
  264. 'Static method call "%s" refers to a method that is not static',
  265. $callback
  266. ));
  267. }
  268. return true;
  269. }
  270. }