@vituum/vite-plugin-latte 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/package.json +6 -6
  2. package/vendor/autoload.php +1 -1
  3. package/vendor/composer/autoload_classmap.php +49 -0
  4. package/vendor/composer/autoload_real.php +4 -4
  5. package/vendor/composer/autoload_static.php +51 -2
  6. package/vendor/composer/installed.json +102 -12
  7. package/vendor/composer/installed.php +14 -5
  8. package/vendor/latte/latte/composer.json +6 -5
  9. package/vendor/latte/latte/readme.md +27 -9
  10. package/vendor/latte/latte/src/Bridges/Tracy/templates/LattePanel.panel.phtml +4 -1
  11. package/vendor/latte/latte/src/Latte/Compiler/TagLexer.php +1 -1
  12. package/vendor/latte/latte/src/Latte/Compiler/TagParserData.php +129 -129
  13. package/vendor/latte/latte/src/Latte/Compiler/TemplateGenerator.php +1 -1
  14. package/vendor/latte/latte/src/Latte/Compiler/TemplateParser.php +1 -1
  15. package/vendor/latte/latte/src/Latte/Engine.php +22 -2
  16. package/vendor/latte/latte/src/Latte/Essential/CachingIterator.php +2 -3
  17. package/vendor/latte/latte/src/Latte/Essential/CoreExtension.php +9 -1
  18. package/vendor/latte/latte/src/Latte/Essential/Filters.php +110 -10
  19. package/vendor/latte/latte/src/Latte/Essential/Nodes/ImportNode.php +8 -2
  20. package/vendor/latte/latte/src/Latte/Essential/Nodes/VarNode.php +14 -18
  21. package/vendor/latte/latte/src/Latte/Essential/TranslatorExtension.php +1 -1
  22. package/vendor/latte/latte/src/Latte/Loaders/FileLoader.php +5 -4
  23. package/vendor/latte/latte/src/Latte/Runtime/Template.php +1 -1
  24. package/vendor/latte/latte/src/Latte/Sandbox/Nodes/FunctionCallNode.php +0 -1
  25. package/vendor/nette/utils/.phpstorm.meta.php +13 -0
  26. package/vendor/nette/utils/composer.json +51 -0
  27. package/vendor/nette/utils/license.md +60 -0
  28. package/vendor/nette/utils/readme.md +55 -0
  29. package/vendor/nette/utils/src/HtmlStringable.php +22 -0
  30. package/vendor/nette/utils/src/Iterators/CachingIterator.php +150 -0
  31. package/vendor/nette/utils/src/Iterators/Mapper.php +33 -0
  32. package/vendor/nette/utils/src/SmartObject.php +140 -0
  33. package/vendor/nette/utils/src/StaticClass.php +34 -0
  34. package/vendor/nette/utils/src/Translator.php +25 -0
  35. package/vendor/nette/utils/src/Utils/ArrayHash.php +106 -0
  36. package/vendor/nette/utils/src/Utils/ArrayList.php +136 -0
  37. package/vendor/nette/utils/src/Utils/Arrays.php +553 -0
  38. package/vendor/nette/utils/src/Utils/Callback.php +137 -0
  39. package/vendor/nette/utils/src/Utils/DateTime.php +140 -0
  40. package/vendor/nette/utils/src/Utils/FileInfo.php +69 -0
  41. package/vendor/nette/utils/src/Utils/FileSystem.php +326 -0
  42. package/vendor/nette/utils/src/Utils/Finder.php +510 -0
  43. package/vendor/nette/utils/src/Utils/Floats.php +107 -0
  44. package/vendor/nette/utils/src/Utils/Helpers.php +104 -0
  45. package/vendor/nette/utils/src/Utils/Html.php +839 -0
  46. package/vendor/nette/utils/src/Utils/Image.php +831 -0
  47. package/vendor/nette/utils/src/Utils/ImageColor.php +75 -0
  48. package/vendor/nette/utils/src/Utils/ImageType.php +25 -0
  49. package/vendor/nette/utils/src/Utils/Iterables.php +238 -0
  50. package/vendor/nette/utils/src/Utils/Json.php +84 -0
  51. package/vendor/nette/utils/src/Utils/ObjectHelpers.php +229 -0
  52. package/vendor/nette/utils/src/Utils/Paginator.php +245 -0
  53. package/vendor/nette/utils/src/Utils/Random.php +52 -0
  54. package/vendor/nette/utils/src/Utils/Reflection.php +322 -0
  55. package/vendor/nette/utils/src/Utils/ReflectionMethod.php +36 -0
  56. package/vendor/nette/utils/src/Utils/Strings.php +728 -0
  57. package/vendor/nette/utils/src/Utils/Type.php +267 -0
  58. package/vendor/nette/utils/src/Utils/Validators.php +416 -0
  59. package/vendor/nette/utils/src/Utils/exceptions.php +50 -0
  60. package/vendor/nette/utils/src/compatibility.php +32 -0
  61. package/vendor/nette/utils/src/exceptions.php +109 -0
@@ -0,0 +1,553 @@
1
+ <?php
2
+
3
+ /**
4
+ * This file is part of the Nette Framework (https://nette.org)
5
+ * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
+ */
7
+
8
+ declare(strict_types=1);
9
+
10
+ namespace Nette\Utils;
11
+
12
+ use JetBrains\PhpStorm\Language;
13
+ use Nette;
14
+ use function is_array, is_int, is_object, count;
15
+
16
+
17
+ /**
18
+ * Array tools library.
19
+ */
20
+ class Arrays
21
+ {
22
+ use Nette\StaticClass;
23
+
24
+ /**
25
+ * Returns item from array. If it does not exist, it throws an exception, unless a default value is set.
26
+ * @template T
27
+ * @param array<T> $array
28
+ * @param array-key|array-key[] $key
29
+ * @param ?T $default
30
+ * @return ?T
31
+ * @throws Nette\InvalidArgumentException if item does not exist and default value is not provided
32
+ */
33
+ public static function get(array $array, string|int|array $key, mixed $default = null): mixed
34
+ {
35
+ foreach (is_array($key) ? $key : [$key] as $k) {
36
+ if (is_array($array) && array_key_exists($k, $array)) {
37
+ $array = $array[$k];
38
+ } else {
39
+ if (func_num_args() < 3) {
40
+ throw new Nette\InvalidArgumentException("Missing item '$k'.");
41
+ }
42
+
43
+ return $default;
44
+ }
45
+ }
46
+
47
+ return $array;
48
+ }
49
+
50
+
51
+ /**
52
+ * Returns reference to array item. If the index does not exist, new one is created with value null.
53
+ * @template T
54
+ * @param array<T> $array
55
+ * @param array-key|array-key[] $key
56
+ * @return ?T
57
+ * @throws Nette\InvalidArgumentException if traversed item is not an array
58
+ */
59
+ public static function &getRef(array &$array, string|int|array $key): mixed
60
+ {
61
+ foreach (is_array($key) ? $key : [$key] as $k) {
62
+ if (is_array($array) || $array === null) {
63
+ $array = &$array[$k];
64
+ } else {
65
+ throw new Nette\InvalidArgumentException('Traversed item is not an array.');
66
+ }
67
+ }
68
+
69
+ return $array;
70
+ }
71
+
72
+
73
+ /**
74
+ * Recursively merges two fields. It is useful, for example, for merging tree structures. It behaves as
75
+ * the + operator for array, ie. it adds a key/value pair from the second array to the first one and retains
76
+ * the value from the first array in the case of a key collision.
77
+ * @template T1
78
+ * @template T2
79
+ * @param array<T1> $array1
80
+ * @param array<T2> $array2
81
+ * @return array<T1|T2>
82
+ */
83
+ public static function mergeTree(array $array1, array $array2): array
84
+ {
85
+ $res = $array1 + $array2;
86
+ foreach (array_intersect_key($array1, $array2) as $k => $v) {
87
+ if (is_array($v) && is_array($array2[$k])) {
88
+ $res[$k] = self::mergeTree($v, $array2[$k]);
89
+ }
90
+ }
91
+
92
+ return $res;
93
+ }
94
+
95
+
96
+ /**
97
+ * Returns zero-indexed position of given array key. Returns null if key is not found.
98
+ */
99
+ public static function getKeyOffset(array $array, string|int $key): ?int
100
+ {
101
+ return Helpers::falseToNull(array_search(self::toKey($key), array_keys($array), strict: true));
102
+ }
103
+
104
+
105
+ /**
106
+ * @deprecated use getKeyOffset()
107
+ */
108
+ public static function searchKey(array $array, $key): ?int
109
+ {
110
+ return self::getKeyOffset($array, $key);
111
+ }
112
+
113
+
114
+ /**
115
+ * Tests an array for the presence of value.
116
+ */
117
+ public static function contains(array $array, mixed $value): bool
118
+ {
119
+ return in_array($value, $array, true);
120
+ }
121
+
122
+
123
+ /**
124
+ * Returns the first item (matching the specified predicate if given). If there is no such item, it returns result of invoking $else or null.
125
+ * @template K of int|string
126
+ * @template V
127
+ * @param array<K, V> $array
128
+ * @param ?callable(V, K, array<K, V>): bool $predicate
129
+ * @return ?V
130
+ */
131
+ public static function first(array $array, ?callable $predicate = null, ?callable $else = null): mixed
132
+ {
133
+ $key = self::firstKey($array, $predicate);
134
+ return $key === null
135
+ ? ($else ? $else() : null)
136
+ : $array[$key];
137
+ }
138
+
139
+
140
+ /**
141
+ * Returns the last item (matching the specified predicate if given). If there is no such item, it returns result of invoking $else or null.
142
+ * @template K of int|string
143
+ * @template V
144
+ * @param array<K, V> $array
145
+ * @param ?callable(V, K, array<K, V>): bool $predicate
146
+ * @return ?V
147
+ */
148
+ public static function last(array $array, ?callable $predicate = null, ?callable $else = null): mixed
149
+ {
150
+ $key = self::lastKey($array, $predicate);
151
+ return $key === null
152
+ ? ($else ? $else() : null)
153
+ : $array[$key];
154
+ }
155
+
156
+
157
+ /**
158
+ * Returns the key of first item (matching the specified predicate if given) or null if there is no such item.
159
+ * @template K of int|string
160
+ * @template V
161
+ * @param array<K, V> $array
162
+ * @param ?callable(V, K, array<K, V>): bool $predicate
163
+ * @return ?K
164
+ */
165
+ public static function firstKey(array $array, ?callable $predicate = null): int|string|null
166
+ {
167
+ if (!$predicate) {
168
+ return array_key_first($array);
169
+ }
170
+ foreach ($array as $k => $v) {
171
+ if ($predicate($v, $k, $array)) {
172
+ return $k;
173
+ }
174
+ }
175
+ return null;
176
+ }
177
+
178
+
179
+ /**
180
+ * Returns the key of last item (matching the specified predicate if given) or null if there is no such item.
181
+ * @template K of int|string
182
+ * @template V
183
+ * @param array<K, V> $array
184
+ * @param ?callable(V, K, array<K, V>): bool $predicate
185
+ * @return ?K
186
+ */
187
+ public static function lastKey(array $array, ?callable $predicate = null): int|string|null
188
+ {
189
+ return $predicate
190
+ ? self::firstKey(array_reverse($array, preserve_keys: true), $predicate)
191
+ : array_key_last($array);
192
+ }
193
+
194
+
195
+ /**
196
+ * Inserts the contents of the $inserted array into the $array immediately after the $key.
197
+ * If $key is null (or does not exist), it is inserted at the beginning.
198
+ */
199
+ public static function insertBefore(array &$array, string|int|null $key, array $inserted): void
200
+ {
201
+ $offset = $key === null ? 0 : (int) self::getKeyOffset($array, $key);
202
+ $array = array_slice($array, 0, $offset, preserve_keys: true)
203
+ + $inserted
204
+ + array_slice($array, $offset, count($array), preserve_keys: true);
205
+ }
206
+
207
+
208
+ /**
209
+ * Inserts the contents of the $inserted array into the $array before the $key.
210
+ * If $key is null (or does not exist), it is inserted at the end.
211
+ */
212
+ public static function insertAfter(array &$array, string|int|null $key, array $inserted): void
213
+ {
214
+ if ($key === null || ($offset = self::getKeyOffset($array, $key)) === null) {
215
+ $offset = count($array) - 1;
216
+ }
217
+
218
+ $array = array_slice($array, 0, $offset + 1, preserve_keys: true)
219
+ + $inserted
220
+ + array_slice($array, $offset + 1, count($array), preserve_keys: true);
221
+ }
222
+
223
+
224
+ /**
225
+ * Renames key in array.
226
+ */
227
+ public static function renameKey(array &$array, string|int $oldKey, string|int $newKey): bool
228
+ {
229
+ $offset = self::getKeyOffset($array, $oldKey);
230
+ if ($offset === null) {
231
+ return false;
232
+ }
233
+
234
+ $val = &$array[$oldKey];
235
+ $keys = array_keys($array);
236
+ $keys[$offset] = $newKey;
237
+ $array = array_combine($keys, $array);
238
+ $array[$newKey] = &$val;
239
+ return true;
240
+ }
241
+
242
+
243
+ /**
244
+ * Returns only those array items, which matches a regular expression $pattern.
245
+ * @param string[] $array
246
+ * @return string[]
247
+ */
248
+ public static function grep(
249
+ array $array,
250
+ #[Language('RegExp')]
251
+ string $pattern,
252
+ bool|int $invert = false,
253
+ ): array
254
+ {
255
+ $flags = $invert ? PREG_GREP_INVERT : 0;
256
+ return Strings::pcre('preg_grep', [$pattern, $array, $flags]);
257
+ }
258
+
259
+
260
+ /**
261
+ * Transforms multidimensional array to flat array.
262
+ */
263
+ public static function flatten(array $array, bool $preserveKeys = false): array
264
+ {
265
+ $res = [];
266
+ $cb = $preserveKeys
267
+ ? function ($v, $k) use (&$res): void { $res[$k] = $v; }
268
+ : function ($v) use (&$res): void { $res[] = $v; };
269
+ array_walk_recursive($array, $cb);
270
+ return $res;
271
+ }
272
+
273
+
274
+ /**
275
+ * Checks if the array is indexed in ascending order of numeric keys from zero, a.k.a list.
276
+ * @return ($value is list ? true : false)
277
+ */
278
+ public static function isList(mixed $value): bool
279
+ {
280
+ return is_array($value) && (PHP_VERSION_ID < 80100
281
+ ? !$value || array_keys($value) === range(0, count($value) - 1)
282
+ : array_is_list($value)
283
+ );
284
+ }
285
+
286
+
287
+ /**
288
+ * Reformats table to associative tree. Path looks like 'field|field[]field->field=field'.
289
+ * @param string|string[] $path
290
+ */
291
+ public static function associate(array $array, $path): array|\stdClass
292
+ {
293
+ $parts = is_array($path)
294
+ ? $path
295
+ : preg_split('#(\[\]|->|=|\|)#', $path, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
296
+
297
+ if (!$parts || $parts === ['->'] || $parts[0] === '=' || $parts[0] === '|') {
298
+ throw new Nette\InvalidArgumentException("Invalid path '$path'.");
299
+ }
300
+
301
+ $res = $parts[0] === '->' ? new \stdClass : [];
302
+
303
+ foreach ($array as $rowOrig) {
304
+ $row = (array) $rowOrig;
305
+ $x = &$res;
306
+
307
+ for ($i = 0; $i < count($parts); $i++) {
308
+ $part = $parts[$i];
309
+ if ($part === '[]') {
310
+ $x = &$x[];
311
+
312
+ } elseif ($part === '=') {
313
+ if (isset($parts[++$i])) {
314
+ $x = $row[$parts[$i]];
315
+ $row = null;
316
+ }
317
+ } elseif ($part === '->') {
318
+ if (isset($parts[++$i])) {
319
+ if ($x === null) {
320
+ $x = new \stdClass;
321
+ }
322
+
323
+ $x = &$x->{$row[$parts[$i]]};
324
+ } else {
325
+ $row = is_object($rowOrig) ? $rowOrig : (object) $row;
326
+ }
327
+ } elseif ($part !== '|') {
328
+ $x = &$x[(string) $row[$part]];
329
+ }
330
+ }
331
+
332
+ if ($x === null) {
333
+ $x = $row;
334
+ }
335
+ }
336
+
337
+ return $res;
338
+ }
339
+
340
+
341
+ /**
342
+ * Normalizes array to associative array. Replace numeric keys with their values, the new value will be $filling.
343
+ */
344
+ public static function normalize(array $array, mixed $filling = null): array
345
+ {
346
+ $res = [];
347
+ foreach ($array as $k => $v) {
348
+ $res[is_int($k) ? $v : $k] = is_int($k) ? $filling : $v;
349
+ }
350
+
351
+ return $res;
352
+ }
353
+
354
+
355
+ /**
356
+ * Returns and removes the value of an item from an array. If it does not exist, it throws an exception,
357
+ * or returns $default, if provided.
358
+ * @template T
359
+ * @param array<T> $array
360
+ * @param ?T $default
361
+ * @return ?T
362
+ * @throws Nette\InvalidArgumentException if item does not exist and default value is not provided
363
+ */
364
+ public static function pick(array &$array, string|int $key, mixed $default = null): mixed
365
+ {
366
+ if (array_key_exists($key, $array)) {
367
+ $value = $array[$key];
368
+ unset($array[$key]);
369
+ return $value;
370
+
371
+ } elseif (func_num_args() < 3) {
372
+ throw new Nette\InvalidArgumentException("Missing item '$key'.");
373
+
374
+ } else {
375
+ return $default;
376
+ }
377
+ }
378
+
379
+
380
+ /**
381
+ * Tests whether at least one element in the array passes the test implemented by the provided function.
382
+ * @template K of int|string
383
+ * @template V
384
+ * @param array<K, V> $array
385
+ * @param callable(V, K, array<K, V>): bool $predicate
386
+ */
387
+ public static function some(iterable $array, callable $predicate): bool
388
+ {
389
+ foreach ($array as $k => $v) {
390
+ if ($predicate($v, $k, $array)) {
391
+ return true;
392
+ }
393
+ }
394
+
395
+ return false;
396
+ }
397
+
398
+
399
+ /**
400
+ * Tests whether all elements in the array pass the test implemented by the provided function.
401
+ * @template K of int|string
402
+ * @template V
403
+ * @param array<K, V> $array
404
+ * @param callable(V, K, array<K, V>): bool $predicate
405
+ */
406
+ public static function every(iterable $array, callable $predicate): bool
407
+ {
408
+ foreach ($array as $k => $v) {
409
+ if (!$predicate($v, $k, $array)) {
410
+ return false;
411
+ }
412
+ }
413
+
414
+ return true;
415
+ }
416
+
417
+
418
+ /**
419
+ * Returns a new array containing all key-value pairs matching the given $predicate.
420
+ * @template K of int|string
421
+ * @template V
422
+ * @param array<K, V> $array
423
+ * @param callable(V, K, array<K, V>): bool $predicate
424
+ * @return array<K, V>
425
+ */
426
+ public static function filter(array $array, callable $predicate): array
427
+ {
428
+ $res = [];
429
+ foreach ($array as $k => $v) {
430
+ if ($predicate($v, $k, $array)) {
431
+ $res[$k] = $v;
432
+ }
433
+ }
434
+ return $res;
435
+ }
436
+
437
+
438
+ /**
439
+ * Returns an array containing the original keys and results of applying the given transform function to each element.
440
+ * @template K of int|string
441
+ * @template V
442
+ * @template R
443
+ * @param array<K, V> $array
444
+ * @param callable(V, K, array<K, V>): R $transformer
445
+ * @return array<K, R>
446
+ */
447
+ public static function map(iterable $array, callable $transformer): array
448
+ {
449
+ $res = [];
450
+ foreach ($array as $k => $v) {
451
+ $res[$k] = $transformer($v, $k, $array);
452
+ }
453
+
454
+ return $res;
455
+ }
456
+
457
+
458
+ /**
459
+ * Returns an array containing new keys and values generated by applying the given transform function to each element.
460
+ * If the function returns null, the element is skipped.
461
+ * @template K of int|string
462
+ * @template V
463
+ * @template ResK of int|string
464
+ * @template ResV
465
+ * @param array<K, V> $array
466
+ * @param callable(V, K, array<K, V>): ?array{ResK, ResV} $transformer
467
+ * @return array<ResK, ResV>
468
+ */
469
+ public static function mapWithKeys(array $array, callable $transformer): array
470
+ {
471
+ $res = [];
472
+ foreach ($array as $k => $v) {
473
+ $pair = $transformer($v, $k, $array);
474
+ if ($pair) {
475
+ $res[$pair[0]] = $pair[1];
476
+ }
477
+ }
478
+
479
+ return $res;
480
+ }
481
+
482
+
483
+ /**
484
+ * Invokes all callbacks and returns array of results.
485
+ * @param callable[] $callbacks
486
+ */
487
+ public static function invoke(iterable $callbacks, ...$args): array
488
+ {
489
+ $res = [];
490
+ foreach ($callbacks as $k => $cb) {
491
+ $res[$k] = $cb(...$args);
492
+ }
493
+
494
+ return $res;
495
+ }
496
+
497
+
498
+ /**
499
+ * Invokes method on every object in an array and returns array of results.
500
+ * @param object[] $objects
501
+ */
502
+ public static function invokeMethod(iterable $objects, string $method, ...$args): array
503
+ {
504
+ $res = [];
505
+ foreach ($objects as $k => $obj) {
506
+ $res[$k] = $obj->$method(...$args);
507
+ }
508
+
509
+ return $res;
510
+ }
511
+
512
+
513
+ /**
514
+ * Copies the elements of the $array array to the $object object and then returns it.
515
+ * @template T of object
516
+ * @param T $object
517
+ * @return T
518
+ */
519
+ public static function toObject(iterable $array, object $object): object
520
+ {
521
+ foreach ($array as $k => $v) {
522
+ $object->$k = $v;
523
+ }
524
+
525
+ return $object;
526
+ }
527
+
528
+
529
+ /**
530
+ * Converts value to array key.
531
+ */
532
+ public static function toKey(mixed $value): int|string
533
+ {
534
+ return key([$value => null]);
535
+ }
536
+
537
+
538
+ /**
539
+ * Returns copy of the $array where every item is converted to string
540
+ * and prefixed by $prefix and suffixed by $suffix.
541
+ * @param string[] $array
542
+ * @return string[]
543
+ */
544
+ public static function wrap(array $array, string $prefix = '', string $suffix = ''): array
545
+ {
546
+ $res = [];
547
+ foreach ($array as $k => $v) {
548
+ $res[$k] = $prefix . $v . $suffix;
549
+ }
550
+
551
+ return $res;
552
+ }
553
+ }
@@ -0,0 +1,137 @@
1
+ <?php
2
+
3
+ /**
4
+ * This file is part of the Nette Framework (https://nette.org)
5
+ * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
+ */
7
+
8
+ declare(strict_types=1);
9
+
10
+ namespace Nette\Utils;
11
+
12
+ use Nette;
13
+ use function is_array, is_object, is_string;
14
+
15
+
16
+ /**
17
+ * PHP callable tools.
18
+ */
19
+ final class Callback
20
+ {
21
+ use Nette\StaticClass;
22
+
23
+ /**
24
+ * Invokes internal PHP function with own error handler.
25
+ */
26
+ public static function invokeSafe(string $function, array $args, callable $onError): mixed
27
+ {
28
+ $prev = set_error_handler(function ($severity, $message, $file) use ($onError, &$prev, $function): ?bool {
29
+ if ($file === __FILE__) {
30
+ $msg = ini_get('html_errors')
31
+ ? Html::htmlToText($message)
32
+ : $message;
33
+ $msg = preg_replace("#^$function\\(.*?\\): #", '', $msg);
34
+ if ($onError($msg, $severity) !== false) {
35
+ return null;
36
+ }
37
+ }
38
+
39
+ return $prev ? $prev(...func_get_args()) : false;
40
+ });
41
+
42
+ try {
43
+ return $function(...$args);
44
+ } finally {
45
+ restore_error_handler();
46
+ }
47
+ }
48
+
49
+
50
+ /**
51
+ * Checks that $callable is valid PHP callback. Otherwise throws exception. If the $syntax is set to true, only verifies
52
+ * that $callable has a valid structure to be used as a callback, but does not verify if the class or method actually exists.
53
+ * @return callable
54
+ * @throws Nette\InvalidArgumentException
55
+ */
56
+ public static function check(mixed $callable, bool $syntax = false)
57
+ {
58
+ if (!is_callable($callable, $syntax)) {
59
+ throw new Nette\InvalidArgumentException(
60
+ $syntax
61
+ ? 'Given value is not a callable type.'
62
+ : sprintf("Callback '%s' is not callable.", self::toString($callable)),
63
+ );
64
+ }
65
+
66
+ return $callable;
67
+ }
68
+
69
+
70
+ /**
71
+ * Converts PHP callback to textual form. Class or method may not exists.
72
+ */
73
+ public static function toString(mixed $callable): string
74
+ {
75
+ if ($callable instanceof \Closure) {
76
+ $inner = self::unwrap($callable);
77
+ return '{closure' . ($inner instanceof \Closure ? '}' : ' ' . self::toString($inner) . '}');
78
+ } else {
79
+ is_callable(is_object($callable) ? [$callable, '__invoke'] : $callable, true, $textual);
80
+ return $textual;
81
+ }
82
+ }
83
+
84
+
85
+ /**
86
+ * Returns reflection for method or function used in PHP callback.
87
+ * @param callable $callable type check is escalated to ReflectionException
88
+ * @throws \ReflectionException if callback is not valid
89
+ */
90
+ public static function toReflection($callable): \ReflectionMethod|\ReflectionFunction
91
+ {
92
+ if ($callable instanceof \Closure) {
93
+ $callable = self::unwrap($callable);
94
+ }
95
+
96
+ if (is_string($callable) && str_contains($callable, '::')) {
97
+ return new ReflectionMethod(...explode('::', $callable, 2));
98
+ } elseif (is_array($callable)) {
99
+ return new ReflectionMethod($callable[0], $callable[1]);
100
+ } elseif (is_object($callable) && !$callable instanceof \Closure) {
101
+ return new ReflectionMethod($callable, '__invoke');
102
+ } else {
103
+ return new \ReflectionFunction($callable);
104
+ }
105
+ }
106
+
107
+
108
+ /**
109
+ * Checks whether PHP callback is function or static method.
110
+ */
111
+ public static function isStatic(callable $callable): bool
112
+ {
113
+ return is_string(is_array($callable) ? $callable[0] : $callable);
114
+ }
115
+
116
+
117
+ /**
118
+ * Unwraps closure created by Closure::fromCallable().
119
+ */
120
+ public static function unwrap(\Closure $closure): callable|array
121
+ {
122
+ $r = new \ReflectionFunction($closure);
123
+ $class = $r->getClosureScopeClass()?->name;
124
+ if (str_ends_with($r->name, '}')) {
125
+ return $closure;
126
+
127
+ } elseif (($obj = $r->getClosureThis()) && $obj::class === $class) {
128
+ return [$obj, $r->name];
129
+
130
+ } elseif ($class) {
131
+ return [$class, $r->name];
132
+
133
+ } else {
134
+ return $r->name;
135
+ }
136
+ }
137
+ }