Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 150 | Ganze Datei anzeigen | Leerzeichen ignorieren | Details | Blame | Letzte Änderung | Log anzeigen | RSS feed

Revision 150 Revision 399
Zeile 19... Zeile 19...
19
final class Callback
19
final class Callback
20
{
20
{
21
	use Nette\StaticClass;
21
	use Nette\StaticClass;
Zeile 22... Zeile 22...
22
 
22
 
23
	/**
-
 
24
	 * @param  string|object|callable  $callable  class, object, callable
-
 
25
	 * @deprecated use Closure::fromCallable()
-
 
26
	 */
-
 
27
	public static function closure($callable, ?string $method = null): \Closure
-
 
28
	{
-
 
29
		trigger_error(__METHOD__ . '() is deprecated, use Closure::fromCallable().', E_USER_DEPRECATED);
-
 
30
		try {
-
 
31
			return \Closure::fromCallable($method === null ? $callable : [$callable, $method]);
-
 
32
		} catch (\TypeError $e) {
-
 
33
			throw new Nette\InvalidArgumentException($e->getMessage());
-
 
34
		}
-
 
35
	}
-
 
36
 
-
 
37
 
-
 
38
	/**
-
 
39
	 * Invokes callback.
-
 
40
	 * @return mixed
-
 
41
	 * @deprecated
-
 
42
	 */
-
 
43
	public static function invoke($callable, ...$args)
-
 
44
	{
-
 
45
		trigger_error(__METHOD__ . '() is deprecated, use native invoking.', E_USER_DEPRECATED);
-
 
46
		self::check($callable);
-
 
47
		return $callable(...$args);
-
 
48
	}
-
 
49
 
-
 
50
 
-
 
51
	/**
-
 
52
	 * Invokes callback with an array of parameters.
-
 
53
	 * @return mixed
-
 
54
	 * @deprecated
-
 
55
	 */
-
 
56
	public static function invokeArgs($callable, array $args = [])
-
 
57
	{
-
 
58
		trigger_error(__METHOD__ . '() is deprecated, use native invoking.', E_USER_DEPRECATED);
-
 
59
		self::check($callable);
-
 
60
		return $callable(...$args);
-
 
61
	}
-
 
62
 
-
 
63
 
-
 
64
	/**
23
	/**
65
	 * Invokes internal PHP function with own error handler.
-
 
66
	 * @return mixed
24
	 * Invokes internal PHP function with own error handler.
67
	 */
25
	 */
68
	public static function invokeSafe(string $function, array $args, callable $onError)
26
	public static function invokeSafe(string $function, array $args, callable $onError): mixed
69
	{
27
	{
70
		$prev = set_error_handler(function ($severity, $message, $file) use ($onError, &$prev, $function): ?bool {
28
		$prev = set_error_handler(function ($severity, $message, $file) use ($onError, &$prev, $function): ?bool {
71
			if ($file === __FILE__) {
29
			if ($file === __FILE__) {
72
				$msg = ini_get('html_errors')
30
				$msg = ini_get('html_errors')
Zeile 90... Zeile 48...
90
 
48
 
91
 
49
 
92
	/**
50
	/**
93
	 * Checks that $callable is valid PHP callback. Otherwise throws exception. If the $syntax is set to true, only verifies
-
 
94
	 * that $callable has a valid structure to be used as a callback, but does not verify if the class or method actually exists.
51
	 * Checks that $callable is valid PHP callback. Otherwise throws exception. If the $syntax is set to true, only verifies
95
	 * @param  mixed  $callable
52
	 * that $callable has a valid structure to be used as a callback, but does not verify if the class or method actually exists.
96
	 * @return callable
53
	 * @return callable
97
	 * @throws Nette\InvalidArgumentException
54
	 * @throws Nette\InvalidArgumentException
98
	 */
55
	 */
99
	public static function check($callable, bool $syntax = false)
56
	public static function check(mixed $callable, bool $syntax = false)
100
	{
57
	{
101
		if (!is_callable($callable, $syntax)) {
58
		if (!is_callable($callable, $syntax)) {
102
			throw new Nette\InvalidArgumentException(
59
			throw new Nette\InvalidArgumentException(
103
				$syntax
60
				$syntax
104
				? 'Given value is not a callable type.'
61
				? 'Given value is not a callable type.'
105
				: sprintf("Callback '%s' is not callable.", self::toString($callable))
62
				: sprintf("Callback '%s' is not callable.", self::toString($callable)),
Zeile 106... Zeile 63...
106
			);
63
			);
107
		}
64
		}
Zeile 108... Zeile 65...
108
 
65
 
109
		return $callable;
66
		return $callable;
110
	}
-
 
111
 
67
	}
112
 
68
 
113
	/**
69
 
114
	 * Converts PHP callback to textual form. Class or method may not exists.
70
	/**
115
	 * @param  mixed  $callable
71
	 * Converts PHP callback to textual form. Class or method may not exists.
116
	 */
72
	 */
117
	public static function toString($callable): string
-
 
118
	{
-
 
119
		if ($callable instanceof \Closure) {
73
	public static function toString(mixed $callable): string
120
			$inner = self::unwrap($callable);
74
	{
121
			return '{closure' . ($inner instanceof \Closure ? '}' : ' ' . self::toString($inner) . '}');
75
		if ($callable instanceof \Closure) {
122
		} elseif (is_string($callable) && $callable[0] === "\0") {
76
			$inner = self::unwrap($callable);
123
			return '{lambda}';
77
			return '{closure' . ($inner instanceof \Closure ? '}' : ' ' . self::toString($inner) . '}');
Zeile 124... Zeile 78...
124
		} else {
78
		} else {
125
			is_callable(is_object($callable) ? [$callable, '__invoke'] : $callable, true, $textual);
79
			is_callable(is_object($callable) ? [$callable, '__invoke'] : $callable, true, $textual);
126
			return $textual;
80
			return $textual;
127
		}
-
 
128
	}
81
		}
129
 
82
	}
130
 
83
 
131
	/**
84
 
132
	 * Returns reflection for method or function used in PHP callback.
85
	/**
133
	 * @param  callable  $callable  type check is escalated to ReflectionException
86
	 * Returns reflection for method or function used in PHP callback.
134
	 * @return \ReflectionMethod|\ReflectionFunction
87
	 * @param  callable  $callable  type check is escalated to ReflectionException
Zeile 135... Zeile 88...
135
	 * @throws \ReflectionException  if callback is not valid
88
	 * @throws \ReflectionException  if callback is not valid
136
	 */
89
	 */
137
	public static function toReflection($callable): \ReflectionFunctionAbstract
90
	public static function toReflection($callable): \ReflectionMethod|\ReflectionFunction
138
	{
91
	{
139
		if ($callable instanceof \Closure) {
92
		if ($callable instanceof \Closure) {
140
			$callable = self::unwrap($callable);
93
			$callable = self::unwrap($callable);
Zeile 161... Zeile 114...
161
	}
114
	}
Zeile 162... Zeile 115...
162
 
115
 
163
 
116
 
164
	/**
-
 
165
	 * Unwraps closure created by Closure::fromCallable().
117
	/**
166
	 * @return callable|array
118
	 * Unwraps closure created by Closure::fromCallable().
167
	 */
119
	 */
168
	public static function unwrap(\Closure $closure)
120
	public static function unwrap(\Closure $closure): callable|array
169
	{
121
	{
170
		$r = new \ReflectionFunction($closure);
122
		$r = new \ReflectionFunction($closure);
Zeile 171... Zeile 123...
171
		if (substr($r->name, -1) === '}') {
123
		if (str_ends_with($r->name, '}')) {
172
			return $closure;
124
			return $closure;