/usr/share/php/Nette/Utils/Callback.php is in php-nette 2.4-20160731-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | <?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
namespace Nette\Utils;
use Nette;
/**
* PHP callable tools.
*/
class Callback
{
use Nette\StaticClass;
/**
* @param mixed class, object, callable
* @param string method
* @return \Closure
*/
public static function closure($callable, $m = NULL)
{
if ($m !== NULL) {
$callable = [$callable, $m];
} elseif (is_string($callable) && count($tmp = explode('::', $callable)) === 2) {
$callable = $tmp;
} elseif ($callable instanceof \Closure) {
return $callable;
} elseif (is_object($callable)) {
$callable = [$callable, '__invoke'];
}
if (is_string($callable) && function_exists($callable)) {
return (new \ReflectionFunction($callable))->getClosure();
} elseif (is_array($callable) && method_exists($callable[0], $callable[1])) {
return (new \ReflectionMethod($callable[0], $callable[1]))->getClosure($callable[0]);
}
self::check($callable);
$_callable_ = $callable;
return function (...$args) use ($_callable_) {
return $_callable_(...$args);
};
}
/**
* Invokes callback.
* @return mixed
*/
public static function invoke($callable, ...$args)
{
self::check($callable);
return call_user_func_array($callable, $args);
}
/**
* Invokes callback with an array of parameters.
* @return mixed
*/
public static function invokeArgs($callable, array $args = [])
{
self::check($callable);
return call_user_func_array($callable, $args);
}
/**
* Invokes internal PHP function with own error handler.
* @param string
* @return mixed
*/
public static function invokeSafe($function, array $args, $onError)
{
$prev = set_error_handler(function ($severity, $message, $file) use ($onError, & $prev, $function) {
if ($file === '' && defined('HHVM_VERSION')) { // https://github.com/facebook/hhvm/issues/4625
$file = func_get_arg(5)[1]['file'];
}
if ($file === __FILE__) {
$msg = preg_replace("#^$function\(.*?\): #", '', $message);
if ($onError($msg, $severity) !== FALSE) {
return;
}
}
return $prev ? $prev(...func_get_args()) : FALSE;
});
try {
$res = $function(...$args);
restore_error_handler();
return $res;
} catch (\Throwable $e) {
restore_error_handler();
throw $e;
} catch (\Exception $e) {
restore_error_handler();
throw $e;
}
}
/**
* @return callable
*/
public static function check($callable, $syntax = FALSE)
{
if (!is_callable($callable, $syntax)) {
throw new Nette\InvalidArgumentException($syntax
? 'Given value is not a callable type.'
: sprintf("Callback '%s' is not callable.", self::toString($callable))
);
}
return $callable;
}
/**
* @return string
*/
public static function toString($callable)
{
if ($callable instanceof \Closure) {
$inner = self::unwrap($callable);
return '{closure' . ($inner instanceof \Closure ? '}' : ' ' . self::toString($inner) . '}');
} elseif (is_string($callable) && $callable[0] === "\0") {
return '{lambda}';
} else {
is_callable($callable, TRUE, $textual);
return $textual;
}
}
/**
* @return \ReflectionMethod|\ReflectionFunction
*/
public static function toReflection($callable)
{
if ($callable instanceof \Closure) {
$callable = self::unwrap($callable);
} elseif ($callable instanceof Nette\Callback) {
trigger_error('Nette\Callback is deprecated.', E_USER_DEPRECATED);
$callable = $callable->getNative();
}
$class = class_exists(Nette\Reflection\Method::class) ? Nette\Reflection\Method::class : 'ReflectionMethod';
if (is_string($callable) && strpos($callable, '::')) {
return new $class($callable);
} elseif (is_array($callable)) {
return new $class($callable[0], $callable[1]);
} elseif (is_object($callable) && !$callable instanceof \Closure) {
return new $class($callable, '__invoke');
} else {
$class = class_exists(Nette\Reflection\GlobalFunction::class) ? Nette\Reflection\GlobalFunction::class : 'ReflectionFunction';
return new $class($callable);
}
}
/**
* @return bool
*/
public static function isStatic($callable)
{
return is_array($callable) ? is_string($callable[0]) : is_string($callable);
}
/**
* Unwraps closure created by self::closure()
* @internal
* @return callable
*/
public static function unwrap(\Closure $closure)
{
$r = new \ReflectionFunction($closure);
if (substr($r->getName(), -1) === '}') {
$vars = $r->getStaticVariables();
return isset($vars['_callable_']) ? $vars['_callable_'] : $closure;
} elseif ($obj = $r->getClosureThis()) {
return [$obj, $r->getName()];
} elseif ($class = $r->getClosureScopeClass()) {
return [$class->getName(), $r->getName()];
} else {
return $r->getName();
}
}
}
|