CallbackHandler.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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-2015 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-2015 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. set_error_handler(array($this, 'errorHandler'), E_WARNING);
  98. $callable = class_exists('WeakRef');
  99. restore_error_handler();
  100. if (!$callable || $this->error) {
  101. $this->callback = $callback;
  102. return;
  103. }
  104. // If WeakRef exists, we want to use it.
  105. // If we have a non-closure object, pass it to WeakRef, and then
  106. // register it.
  107. if (is_object($callback) && !$callback instanceof Closure) {
  108. $this->callback = new WeakRef($callback);
  109. return;
  110. }
  111. // If we have a string or closure, register as-is
  112. if (!is_array($callback)) {
  113. $this->callback = $callback;
  114. return;
  115. }
  116. list($target, $method) = $callback;
  117. // If we have an array callback, and the first argument is not an
  118. // object, register as-is
  119. if (!is_object($target)) {
  120. $this->callback = $callback;
  121. return;
  122. }
  123. // We have an array callback with an object as the first argument;
  124. // pass it to WeakRef, and then register the new callback
  125. $target = new WeakRef($target);
  126. $this->callback = array($target, $method);
  127. }
  128. /**
  129. * Retrieve registered callback
  130. *
  131. * @return Callable
  132. */
  133. public function getCallback()
  134. {
  135. $callback = $this->callback;
  136. // String callbacks -- simply return
  137. if (is_string($callback)) {
  138. return $callback;
  139. }
  140. // WeakRef callbacks -- pull it out of the object and return it
  141. if ($callback instanceof WeakRef) {
  142. return $callback->get();
  143. }
  144. // Non-WeakRef object callback -- return it
  145. if (is_object($callback)) {
  146. return $callback;
  147. }
  148. // Array callback with WeakRef object -- retrieve the object first, and
  149. // then return
  150. list($target, $method) = $callback;
  151. if ($target instanceof WeakRef) {
  152. return array($target->get(), $method);
  153. }
  154. // Otherwise, return it
  155. return $callback;
  156. }
  157. /**
  158. * Invoke handler
  159. *
  160. * @param array $args Arguments to pass to callback
  161. * @return mixed
  162. */
  163. public function call(array $args = array())
  164. {
  165. $callback = $this->getCallback();
  166. $isPhp54 = version_compare(PHP_VERSION, '5.4.0rc1', '>=');
  167. if ($isPhp54 && is_string($callback)) {
  168. $this->validateStringCallbackFor54($callback);
  169. }
  170. // Minor performance tweak; use call_user_func() until > 3 arguments
  171. // reached
  172. switch (count($args)) {
  173. case 0:
  174. if ($isPhp54) {
  175. return $callback();
  176. }
  177. return call_user_func($callback);
  178. case 1:
  179. if ($isPhp54) {
  180. return $callback(array_shift($args));
  181. }
  182. return call_user_func($callback, array_shift($args));
  183. case 2:
  184. $arg1 = array_shift($args);
  185. $arg2 = array_shift($args);
  186. if ($isPhp54) {
  187. return $callback($arg1, $arg2);
  188. }
  189. return call_user_func($callback, $arg1, $arg2);
  190. case 3:
  191. $arg1 = array_shift($args);
  192. $arg2 = array_shift($args);
  193. $arg3 = array_shift($args);
  194. if ($isPhp54) {
  195. return $callback($arg1, $arg2, $arg3);
  196. }
  197. return call_user_func($callback, $arg1, $arg2, $arg3);
  198. default:
  199. return call_user_func_array($callback, $args);
  200. }
  201. }
  202. /**
  203. * Invoke as functor
  204. *
  205. * @return mixed
  206. */
  207. public function __invoke()
  208. {
  209. return $this->call(func_get_args());
  210. }
  211. /**
  212. * Get all callback metadata
  213. *
  214. * @return array
  215. */
  216. public function getMetadata()
  217. {
  218. return $this->metadata;
  219. }
  220. /**
  221. * Retrieve a single metadatum
  222. *
  223. * @param string $name
  224. * @return mixed
  225. */
  226. public function getMetadatum($name)
  227. {
  228. if (array_key_exists($name, $this->metadata)) {
  229. return $this->metadata[$name];
  230. }
  231. return null;
  232. }
  233. /**
  234. * Validate a static method call
  235. *
  236. * Validates that a static method call in PHP 5.4 will actually work
  237. *
  238. * @param string $callback
  239. * @return true
  240. * @throws Zend_Stdlib_Exception_InvalidCallbackException if invalid
  241. */
  242. protected function validateStringCallbackFor54($callback)
  243. {
  244. if (!strstr($callback, '::')) {
  245. return true;
  246. }
  247. list($class, $method) = explode('::', $callback, 2);
  248. if (!class_exists($class)) {
  249. require_once 'Zend/Stdlib/Exception/InvalidCallbackException.php';
  250. throw new Zend_Stdlib_Exception_InvalidCallbackException(sprintf(
  251. 'Static method call "%s" refers to a class that does not exist',
  252. $callback
  253. ));
  254. }
  255. $r = new ReflectionClass($class);
  256. if (!$r->hasMethod($method)) {
  257. require_once 'Zend/Stdlib/Exception/InvalidCallbackException.php';
  258. throw new Zend_Stdlib_Exception_InvalidCallbackException(sprintf(
  259. 'Static method call "%s" refers to a method that does not exist',
  260. $callback
  261. ));
  262. }
  263. $m = $r->getMethod($method);
  264. if (!$m->isStatic()) {
  265. require_once 'Zend/Stdlib/Exception/InvalidCallbackException.php';
  266. throw new Zend_Stdlib_Exception_InvalidCallbackException(sprintf(
  267. 'Static method call "%s" refers to a method that is not static',
  268. $callback
  269. ));
  270. }
  271. return true;
  272. }
  273. }