/usr/share/php/Nette/Utils/Callback.php is in php-nette 2.1.0-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 | <?php
/**
* This file is part of the Nette Framework (http://nette.org)
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
*/
namespace Nette\Utils;
use Nette;
/**
* PHP callable tools.
*
* @author David Grudl
*/
class Callback
{
/**
* @param mixed class, object, callable
* @param string method
* @return \Closure
*/
public static function closure($callable, $m = NULL)
{
if ($m !== NULL) {
$callable = array($callable, $m);
} elseif ($callable instanceof \Closure) {
return $callable;
}
self::check($callable, TRUE);
$_callable_ = $callable;
return function() use ($_callable_) {
Callback::check($_callable_);
return call_user_func_array($_callable_, func_get_args());
};
}
/**
* Invokes callback.
* @return mixed
*/
public static function invoke($callable)
{
self::check($callable);
return call_user_func_array($callable, array_slice(func_get_args(), 1));
}
/**
* Invokes callback with an array of parameters.
* @return mixed
*/
public static function invokeArgs($callable, array $args = array())
{
self::check($callable);
return call_user_func_array($callable, $args);
}
/**
* @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.'
: "Callback '" . self::toString($callable) . "' is not callable."
);
}
return $callable;
}
/**
* @return string
*/
public static function toString($callable)
{
if ($callable instanceof \Closure) {
if ($inner = self::unwrap($callable)) {
return '{closure ' . self::toString($inner) . '}';
}
return '{closure}';
} elseif (is_string($callable) && $callable[0] === "\0") {
return '{lambda}';
} else {
is_callable($callable, TRUE, $textual);
return $textual;
}
}
/**
* @return Nette\Reflection\GlobalFunction|Nette\Reflection\Method
*/
public static function toReflection($callable)
{
if ($callable instanceof \Closure && $inner = self::unwrap($callable)) {
$callable = $inner;
} elseif ($callable instanceof Nette\Callback) {
$callable = $callable->getNative();
}
if (is_string($callable) && strpos($callable, '::')) {
return new Nette\Reflection\Method($callable);
} elseif (is_array($callable)) {
return new Nette\Reflection\Method($callable[0], $callable[1]);
} elseif (is_object($callable) && !$callable instanceof \Closure) {
return new Nette\Reflection\Method($callable, '__invoke');
} else {
return new Nette\Reflection\GlobalFunction($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(), used i.e. by ObjectMixin in PHP < 5.4
* @internal
* @return callable
*/
public static function unwrap(\Closure $closure)
{
$rm = new \ReflectionFunction($closure);
$vars = $rm->getStaticVariables();
return isset($vars['_callable_']) ? $vars['_callable_'] : NULL;
}
}
|