Skip to content

Commit 7a94980

Browse files
committed
feat: 添加获取客户端IP和时间微调功能
1 parent df6c41b commit 7a94980

1 file changed

Lines changed: 43 additions & 10 deletions

File tree

src/Functions.php

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use BackedEnum;
1515
use Carbon\Carbon;
16+
use Carbon\CarbonInterval;
1617
use Closure;
1718
use Countable;
1819
use DateTimeZone;
@@ -299,6 +300,16 @@ function fluent($value): Fluent
299300
return new Fluent($value);
300301
}
301302

303+
/**
304+
* Get client IP.
305+
*/
306+
function get_client_ip(): string
307+
{
308+
/** @var RequestInterface $request */
309+
$request = di(RequestInterface::class);
310+
return $request->getHeaderLine('x-real-ip') ?: $request->server('remote_addr');
311+
}
312+
302313
/**
303314
* @param string|Stringable $message
304315
*/
@@ -349,6 +360,30 @@ function logs(string $name = 'hyperf', string $group = 'default'): LoggerInterfa
349360
return di(LoggerFactory::class)->get($name, $group);
350361
}
351362

363+
/**
364+
* Get the current date / time plus the given number of microseconds.
365+
*/
366+
function microseconds(int $microseconds): CarbonInterval
367+
{
368+
return CarbonInterval::microseconds($microseconds);
369+
}
370+
371+
/**
372+
* Get the current date / time plus the given number of months.
373+
*/
374+
function months(int $months): CarbonInterval
375+
{
376+
return CarbonInterval::months($months);
377+
}
378+
379+
/**
380+
* Get the current date / time plus the given number of milliseconds.
381+
*/
382+
function milliseconds(int $milliseconds): CarbonInterval
383+
{
384+
return CarbonInterval::milliseconds($milliseconds);
385+
}
386+
352387
/**
353388
* Create a new Carbon instance for the current time.
354389
*
@@ -624,6 +659,14 @@ function validator(array $data = [], array $rules = [], array $messages = [], ar
624659
return $factory->make($data, $rules, $messages, $customAttributes);
625660
}
626661

662+
/**
663+
* Get the current date / time plus the given number of weeks.
664+
*/
665+
function weeks(int $weeks): CarbonInterval
666+
{
667+
return CarbonInterval::weeks($weeks);
668+
}
669+
627670
/**
628671
* @param mixed $expr
629672
* @param mixed $value
@@ -636,13 +679,3 @@ function when($expr, $value = null, $default = null)
636679

637680
return $result instanceof Closure ? $result($expr) : $result;
638681
}
639-
640-
/**
641-
* Get client IP.
642-
*/
643-
function get_client_ip(): string
644-
{
645-
/** @var RequestInterface $request */
646-
$request = di(RequestInterface::class);
647-
return $request->getHeaderLine('x-real-ip') ?: $request->server('remote_addr');
648-
}

0 commit comments

Comments
 (0)