3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Stdlib
;
17 * A handler for an event, event, filterchain, etc. Abstracts PHP callbacks,
18 * primarily to allow for lazy-loading and ensuring availability of default
19 * arguments (currying).
21 * This was primarily used in zend-eventmanager for managing listeners; as that
22 * component removes its usage of this class for v3, it is deprecated.
24 * @deprecated as of v2.7.4.
29 * @var string|array|callable PHP callback to invoke
34 * Callback metadata, if any
42 * @param string|array|object|callable $callback PHP callback
43 * @param array $metadata Callback metadata
45 public function __construct($callback, array $metadata = [])
47 $this->metadata
= $metadata;
48 $this->registerCallback($callback);
52 * Registers the callback provided in the constructor
54 * @param callable $callback
55 * @throws Exception\InvalidCallbackException
58 protected function registerCallback($callback)
60 if (!is_callable($callback)) {
61 throw new Exception\
InvalidCallbackException('Invalid callback provided; not callable');
64 $this->callback
= $callback;
68 * Retrieve registered callback
72 public function getCallback()
74 return $this->callback
;
80 * @param array $args Arguments to pass to callback
83 public function call(array $args = [])
85 $callback = $this->getCallback();
86 $argCount = count($args);
88 if (is_string($callback)) {
89 $result = $this->validateStringCallbackFor54($callback);
91 if ($result !== true && $argCount <= 3) {
93 // Minor performance tweak, if the callback gets called more
95 $this->callback
= $result;
99 // Minor performance tweak; use call_user_func() until > 3 arguments
105 return $callback(array_shift($args));
107 $arg1 = array_shift($args);
108 $arg2 = array_shift($args);
109 return $callback($arg1, $arg2);
111 $arg1 = array_shift($args);
112 $arg2 = array_shift($args);
113 $arg3 = array_shift($args);
114 return $callback($arg1, $arg2, $arg3);
116 return call_user_func_array($callback, $args);
125 public function __invoke()
127 return $this->call(func_get_args());
131 * Get all callback metadata
135 public function getMetadata()
137 return $this->metadata
;
141 * Retrieve a single metadatum
143 * @param string $name
146 public function getMetadatum($name)
148 if (array_key_exists($name, $this->metadata
)) {
149 return $this->metadata
[$name];
155 * Validate a static method call
158 * @param string $callback
160 * @throws Exception\InvalidCallbackException if invalid
162 protected function validateStringCallbackFor54($callback)
164 if (!strstr($callback, '::')) {
168 list($class, $method) = explode('::', $callback, 2);
170 if (!class_exists($class)) {
171 throw new Exception\
InvalidCallbackException(sprintf(
172 'Static method call "%s" refers to a class that does not exist',
177 $r = new ReflectionClass($class);
178 if (!$r->hasMethod($method)) {
179 throw new Exception\
InvalidCallbackException(sprintf(
180 'Static method call "%s" refers to a method that does not exist',
184 $m = $r->getMethod($method);
185 if (!$m->isStatic()) {
186 throw new Exception\
InvalidCallbackException(sprintf(
187 'Static method call "%s" refers to a method that is not static',
192 // returning a non boolean value may not be nice for a validate method,
193 // but that allows the usage of a static string callback without using
194 // the call_user_func function.
195 return [$class, $method];