fix travis build
[GitHub/Stricted/Domain-Control-Panel.git] / vendor / Zend / Stdlib / CallbackHandler.php
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
4 *
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
8 */
9
10 namespace Zend\Stdlib;
11
12 use ReflectionClass;
13
14 /**
15 * CallbackHandler
16 *
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).
20 *
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.
23 *
24 * @deprecated as of v2.7.4.
25 */
26 class CallbackHandler
27 {
28 /**
29 * @var string|array|callable PHP callback to invoke
30 */
31 protected $callback;
32
33 /**
34 * Callback metadata, if any
35 * @var array
36 */
37 protected $metadata;
38
39 /**
40 * Constructor
41 *
42 * @param string|array|object|callable $callback PHP callback
43 * @param array $metadata Callback metadata
44 */
45 public function __construct($callback, array $metadata = [])
46 {
47 $this->metadata = $metadata;
48 $this->registerCallback($callback);
49 }
50
51 /**
52 * Registers the callback provided in the constructor
53 *
54 * @param callable $callback
55 * @throws Exception\InvalidCallbackException
56 * @return void
57 */
58 protected function registerCallback($callback)
59 {
60 if (!is_callable($callback)) {
61 throw new Exception\InvalidCallbackException('Invalid callback provided; not callable');
62 }
63
64 $this->callback = $callback;
65 }
66
67 /**
68 * Retrieve registered callback
69 *
70 * @return callable
71 */
72 public function getCallback()
73 {
74 return $this->callback;
75 }
76
77 /**
78 * Invoke handler
79 *
80 * @param array $args Arguments to pass to callback
81 * @return mixed
82 */
83 public function call(array $args = [])
84 {
85 $callback = $this->getCallback();
86 $argCount = count($args);
87
88 if (is_string($callback)) {
89 $result = $this->validateStringCallbackFor54($callback);
90
91 if ($result !== true && $argCount <= 3) {
92 $callback = $result;
93 // Minor performance tweak, if the callback gets called more
94 // than once
95 $this->callback = $result;
96 }
97 }
98
99 // Minor performance tweak; use call_user_func() until > 3 arguments
100 // reached
101 switch ($argCount) {
102 case 0:
103 return $callback();
104 case 1:
105 return $callback(array_shift($args));
106 case 2:
107 $arg1 = array_shift($args);
108 $arg2 = array_shift($args);
109 return $callback($arg1, $arg2);
110 case 3:
111 $arg1 = array_shift($args);
112 $arg2 = array_shift($args);
113 $arg3 = array_shift($args);
114 return $callback($arg1, $arg2, $arg3);
115 default:
116 return call_user_func_array($callback, $args);
117 }
118 }
119
120 /**
121 * Invoke as functor
122 *
123 * @return mixed
124 */
125 public function __invoke()
126 {
127 return $this->call(func_get_args());
128 }
129
130 /**
131 * Get all callback metadata
132 *
133 * @return array
134 */
135 public function getMetadata()
136 {
137 return $this->metadata;
138 }
139
140 /**
141 * Retrieve a single metadatum
142 *
143 * @param string $name
144 * @return mixed
145 */
146 public function getMetadatum($name)
147 {
148 if (array_key_exists($name, $this->metadata)) {
149 return $this->metadata[$name];
150 }
151 return;
152 }
153
154 /**
155 * Validate a static method call
156 *
157 *
158 * @param string $callback
159 * @return true|array
160 * @throws Exception\InvalidCallbackException if invalid
161 */
162 protected function validateStringCallbackFor54($callback)
163 {
164 if (!strstr($callback, '::')) {
165 return true;
166 }
167
168 list($class, $method) = explode('::', $callback, 2);
169
170 if (!class_exists($class)) {
171 throw new Exception\InvalidCallbackException(sprintf(
172 'Static method call "%s" refers to a class that does not exist',
173 $callback
174 ));
175 }
176
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',
181 $callback
182 ));
183 }
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',
188 $callback
189 ));
190 }
191
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];
196 }
197 }