Repository navigation
Expand file tree
/
Copy pathFunctions.php
More file actions
338 lines (306 loc) · 7.53 KB
/
Copy pathFunctions.php
File metadata and controls
338 lines (306 loc) · 7.53 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Support;
use BackedEnum;
use Carbon\Carbon;
use Closure;
use DateTimeZone;
use Hyperf\Collection\Arr;
use Hyperf\Context\ApplicationContext;
use Hyperf\Di\Container;
use Hyperf\Stringable\StrCache;
use Hyperf\Support\Backoff\ArrayBackoff;
use Throwable;
use UnitEnum;
/**
* Return the default value of the given value.
* @template TValue
* @template TReturn
*
* @param (Closure(TValue):TReturn)|TValue $value
* @return ($value is Closure ? TReturn : TValue)
*/
function value(mixed $value, ...$args)
{
return $value instanceof Closure ? $value(...$args) : $value;
}
/**
* Gets the value of an environment variable.
*
* @param string $key
* @param null|mixed $default
*/
function env($key, $default = null)
{
$value = getenv($key);
if ($value === false) {
return value($default);
}
switch (strtolower($value)) {
case 'true':
case '(true)':
return true;
case 'false':
case '(false)':
return false;
case 'empty':
case '(empty)':
return '';
case 'null':
case '(null)':
return null;
}
if (($valueLength = strlen($value)) > 1 && $value[0] === '"' && $value[$valueLength - 1] === '"') {
return substr($value, 1, -1);
}
return $value;
}
/**
* Retry an operation a given number of times.
*
* @template TReturn
*
* @param float|int|int[] $times
* @param callable(int):TReturn $callback
* @param int $sleep millisecond
* @return TReturn
* @throws Throwable
*/
function retry($times, callable $callback, int $sleep = 0)
{
$attempts = 0;
if (is_array($times)) {
$backoff = new ArrayBackoff($times);
$times = count($times);
} else {
$backoff = new Backoff($sleep);
}
beginning:
try {
return $callback(++$attempts);
} catch (Throwable $e) {
if (--$times < 0) {
throw $e;
}
$backoff->sleep();
goto beginning;
}
}
/**
* Return the given value, optionally passed through the given callback.
*
* @template TValue
* @template TReturn
*
* @param TValue $value
* @param null|(callable(TValue):TReturn) $callback
* @return ($callback is null ? TValue : TReturn)
*/
function with($value, ?callable $callback = null)
{
return is_null($callback) ? $value : $callback($value);
}
/**
* Call a callback with the arguments.
*
* @param mixed $callback
* @return null|mixed
*/
function call($callback, array $args = [])
{
$result = null;
if ($callback instanceof Closure) {
$result = $callback(...$args);
} elseif (is_object($callback) || (is_string($callback) && function_exists($callback))) {
$result = $callback(...$args);
} elseif (is_array($callback)) {
[$object, $method] = $callback;
$result = is_object($object) ? $object->{$method}(...$args) : $object::$method(...$args);
} else {
$result = call_user_func_array($callback, $args);
}
return $result;
}
/**
* Get the class "basename" of the given object / class.
*
* @param object|string $class
* @return string
*/
function class_basename($class)
{
$class = is_object($class) ? get_class($class) : $class;
return basename(str_replace('\\', '/', $class));
}
/**
* Returns all traits used by a trait and its traits.
*
* @param object|string $trait
* @return array
*/
function trait_uses_recursive($trait)
{
$traits = class_uses($trait);
foreach ($traits as $trait) {
$traits += trait_uses_recursive($trait);
}
return $traits;
}
/**
* Returns all traits used by a class, its parent classes and trait of their traits.
*
* @param object|string $class
* @return array
*/
function class_uses_recursive($class)
{
if (is_object($class)) {
$class = get_class($class);
}
$results = [];
/* @phpstan-ignore-next-line */
foreach (array_reverse(class_parents($class) ?: []) + [$class => $class] as $class) {
$results += trait_uses_recursive($class);
}
return array_unique($results);
}
/**
* Create a setter string.
*/
function setter(string $property): string
{
return 'set' . StrCache::studly($property);
}
/**
* Create a getter string.
*/
function getter(string $property): string
{
return 'get' . StrCache::studly($property);
}
/**
* Create an object instance, if the DI container exist in ApplicationContext,
* then the object will be created by DI container via `make()` method, if not,
* the object will create by `new` keyword.
*
* @template TClass
*
* @param class-string<TClass>|string $name
* @return ($name is class-string<TClass> ? TClass : mixed)
*/
function make(string $name, array $parameters = [])
{
if (ApplicationContext::hasContainer()) {
/** @var Container $container */
$container = ApplicationContext::getContainer();
if (method_exists($container, 'make')) {
return $container->make($name, $parameters);
}
}
$parameters = array_values($parameters);
return new $name(...$parameters);
}
/**
* Return the default swoole hook flags, you can rewrite it by defining `SWOOLE_HOOK_FLAGS`.
*/
function swoole_hook_flags(): int
{
return defined('SWOOLE_HOOK_FLAGS') ? SWOOLE_HOOK_FLAGS : SWOOLE_HOOK_ALL;
}
/**
* Provide access to optional objects.
* @template TValue
* @template TReturn
*
* @param TValue $value
* @param null|(callable(TValue):TReturn) $callback
* @return ($callback is null ? Optional<TValue> : ($value is null ? null : TReturn))
*/
function optional($value = null, ?callable $callback = null)
{
if (is_null($callback)) {
return new Optional($value);
}
if (! is_null($value)) {
return $callback($value);
}
return null;
}
/**
* Build SQL contain bind.
*/
function build_sql(string $sql, array $bindings = []): string
{
if (! Arr::isAssoc($bindings)) {
$position = 0;
foreach ($bindings as $value) {
$position = strpos($sql, '?', $position);
if ($position === false) {
break;
}
$value = (string) match (gettype($value)) {
'integer', 'double' => $value,
'boolean' => (int) $value,
default => sprintf("'%s'", $value),
};
$sql = substr_replace($sql, $value, $position, 1);
$position += strlen($value);
}
}
return $sql;
}
/**
* Sleep milliseconds.
*/
function msleep(int $milliSeconds): void
{
usleep($milliSeconds * 1000);
}
/**
* Create a new Carbon instance for the current time.
*
* @param null|DateTimeZone|string $tz
* @return Carbon
*/
function now($tz = null)
{
return Carbon::now($tz);
}
/**
* Create a new Carbon instance for the current date.
*
* @param null|DateTimeZone|string $tz
* @return Carbon
*/
function today($tz = null)
{
return Carbon::today($tz);
}
/**
* Return a scalar value for the given value that might be an enum.
*
* @internal
*
* @template TValue
* @template TDefault
*
* @param TValue $value
* @param callable(TValue): TDefault|TDefault $default
* @return ($value is empty ? TDefault : mixed)
*/
function enum_value($value, $default = null)
{
return match (true) {
$value instanceof BackedEnum => $value->value,
$value instanceof UnitEnum => $value->name,
default => $value ?? value($default),
};
}