-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c924d22
commit db6f195
Showing
27 changed files
with
89 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
/* | ||
* This file is part of the jiannei/laravel-response. | ||
* | ||
* (c) Jiannei <[email protected]> | ||
* (c) Jiannei <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
|
@@ -33,7 +33,6 @@ | |
'code' => 422, | ||
], | ||
\Illuminate\Auth\AuthenticationException::class => [ | ||
|
||
], | ||
\Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class => [ | ||
'message' => '', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
/* | ||
* This file is part of the jiannei/laravel-response. | ||
* | ||
* (c) Jiannei <[email protected]> | ||
* (c) Jiannei <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
/* | ||
* This file is part of the jiannei/laravel-response. | ||
* | ||
* (c) Jiannei <[email protected]> | ||
* (c) Jiannei <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
|
@@ -19,14 +19,15 @@ class Etag | |
/** | ||
* Implement Etag support. | ||
* | ||
* @param \Illuminate\Http\Request $request the HTTP request | ||
* @param \Closure $next closure for the response | ||
* @param \Illuminate\Http\Request $request the HTTP request | ||
* @param \Closure $next closure for the response | ||
* | ||
* @return mixed | ||
*/ | ||
public function handle(Request $request, Closure $next) | ||
{ | ||
// If this was not a get or head request, just return | ||
if (! $request->isMethod('get') && ! $request->isMethod('head')) { | ||
if (!$request->isMethod('get') && !$request->isMethod('head')) { | ||
return $next($request); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
/* | ||
* This file is part of the jiannei/laravel-response. | ||
* | ||
* (c) Jiannei <[email protected]> | ||
* (c) Jiannei <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
/* | ||
* This file is part of the jiannei/laravel-response. | ||
* | ||
* (c) Jiannei <[email protected]> | ||
* (c) Jiannei <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
|
@@ -42,10 +42,11 @@ public function __construct(RateLimiter $limiter) | |
/** | ||
* Handle an incoming request. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param int|string $maxAttempts | ||
* @param float|int $decayMinutes | ||
* @param string $prefix | ||
* @param \Illuminate\Http\Request $request | ||
* @param int|string $maxAttempts | ||
* @param float|int $decayMinutes | ||
* @param string $prefix | ||
* | ||
* @return \Symfony\Component\HttpFoundation\Response | ||
* | ||
* @throws \Illuminate\Http\Exceptions\ThrottleRequestsException | ||
|
@@ -73,8 +74,9 @@ public function handle($request, Closure $next, $maxAttempts = 60, $decayMinutes | |
/** | ||
* Resolve the number of attempts if the user is authenticated or not. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param int|string $maxAttempts | ||
* @param \Illuminate\Http\Request $request | ||
* @param int|string $maxAttempts | ||
* | ||
* @return int | ||
*/ | ||
protected function resolveMaxAttempts($request, $maxAttempts) | ||
|
@@ -83,7 +85,7 @@ protected function resolveMaxAttempts($request, $maxAttempts) | |
$maxAttempts = explode('|', $maxAttempts, 2)[$request->user() ? 1 : 0]; | ||
} | ||
|
||
if (! is_numeric($maxAttempts) && $request->user()) { | ||
if (!is_numeric($maxAttempts) && $request->user()) { | ||
$maxAttempts = $request->user()->{$maxAttempts}; | ||
} | ||
|
||
|
@@ -93,7 +95,8 @@ protected function resolveMaxAttempts($request, $maxAttempts) | |
/** | ||
* Resolve request signature. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Illuminate\Http\Request $request | ||
* | ||
* @return string | ||
* | ||
* @throws \RuntimeException | ||
|
@@ -110,8 +113,9 @@ protected function resolveRequestSignature($request) | |
/** | ||
* Create a 'too many attempts' exception. | ||
* | ||
* @param string $key | ||
* @param int $maxAttempts | ||
* @param string $key | ||
* @param int $maxAttempts | ||
* | ||
* @return \Illuminate\Http\Exceptions\ThrottleRequestsException | ||
*/ | ||
protected function buildException($key, $maxAttempts) | ||
|
@@ -132,7 +136,8 @@ protected function buildException($key, $maxAttempts) | |
/** | ||
* Get the number of seconds until the next retry. | ||
* | ||
* @param string $key | ||
* @param string $key | ||
* | ||
* @return int | ||
*/ | ||
protected function getTimeUntilNextRetry($key) | ||
|
@@ -143,9 +148,10 @@ protected function getTimeUntilNextRetry($key) | |
/** | ||
* Add the limit header information to the given response. | ||
* | ||
* @param int $maxAttempts | ||
* @param int $remainingAttempts | ||
* @param int|null $retryAfter | ||
* @param int $maxAttempts | ||
* @param int $remainingAttempts | ||
* @param int|null $retryAfter | ||
* | ||
* @return \Symfony\Component\HttpFoundation\Response | ||
*/ | ||
protected function addHeaders(Response $response, $maxAttempts, $remainingAttempts, $retryAfter = null) | ||
|
@@ -160,9 +166,10 @@ protected function addHeaders(Response $response, $maxAttempts, $remainingAttemp | |
/** | ||
* Get the limit headers information. | ||
* | ||
* @param int $maxAttempts | ||
* @param int $remainingAttempts | ||
* @param int|null $retryAfter | ||
* @param int $maxAttempts | ||
* @param int $remainingAttempts | ||
* @param int|null $retryAfter | ||
* | ||
* @return array | ||
*/ | ||
protected function getHeaders($maxAttempts, $remainingAttempts, $retryAfter = null) | ||
|
@@ -172,7 +179,7 @@ protected function getHeaders($maxAttempts, $remainingAttempts, $retryAfter = nu | |
'X-RateLimit-Remaining' => $remainingAttempts, | ||
]; | ||
|
||
if (! is_null($retryAfter)) { | ||
if (!is_null($retryAfter)) { | ||
$headers['Retry-After'] = $retryAfter; | ||
$headers['X-RateLimit-Reset'] = $this->availableAt($retryAfter); | ||
} | ||
|
@@ -183,9 +190,10 @@ protected function getHeaders($maxAttempts, $remainingAttempts, $retryAfter = nu | |
/** | ||
* Calculate the number of remaining attempts. | ||
* | ||
* @param string $key | ||
* @param int $maxAttempts | ||
* @param int|null $retryAfter | ||
* @param string $key | ||
* @param int $maxAttempts | ||
* @param int|null $retryAfter | ||
* | ||
* @return int | ||
*/ | ||
protected function calculateRemainingAttempts($key, $maxAttempts, $retryAfter = null) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
/* | ||
* This file is part of the jiannei/laravel-response. | ||
* | ||
* (c) Jiannei <[email protected]> | ||
* (c) Jiannei <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
/* | ||
* This file is part of the jiannei/laravel-response. | ||
* | ||
* (c) Jiannei <[email protected]> | ||
* (c) Jiannei <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
/* | ||
* This file is part of the jiannei/laravel-response. | ||
* | ||
* (c) Jiannei <[email protected]> | ||
* (c) Jiannei <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
/* | ||
* This file is part of the jiannei/laravel-response. | ||
* | ||
* (c) Jiannei <[email protected]> | ||
* (c) Jiannei <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
|
@@ -21,11 +21,11 @@ | |
|
||
/** | ||
* @method static \Jiannei\Response\Laravel\Support\Format data(mixed $data = null, string $message = '', int|\BackedEnum $code = 200, $error = null) | ||
* @method static array|null get() | ||
* @method static array paginator(AbstractPaginator|AbstractCursorPaginator|Paginator $resource) | ||
* @method static array resourceCollection(ResourceCollection $collection) | ||
* @method static array jsonResource(JsonResource $resource) | ||
* @method static JsonResponse response() | ||
* @method static array|null get() | ||
* @method static array paginator(AbstractPaginator|AbstractCursorPaginator|Paginator $resource) | ||
* @method static array resourceCollection(ResourceCollection $collection) | ||
* @method static array jsonResource(JsonResource $resource) | ||
* @method static JsonResponse response() | ||
* | ||
* @see \Jiannei\Response\Laravel\Support\Format | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
/* | ||
* This file is part of the jiannei/laravel-response. | ||
* | ||
* (c) Jiannei <[email protected]> | ||
* (c) Jiannei <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
/* | ||
* This file is part of the jiannei/laravel-response. | ||
* | ||
* (c) Jiannei <[email protected]> | ||
* (c) Jiannei <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
|
@@ -60,8 +60,9 @@ public function get(): ?array | |
/** | ||
* Core format. | ||
* | ||
* @param null $data | ||
* @param null $error | ||
* @param null $data | ||
* @param null $error | ||
* | ||
* @return Format | ||
*/ | ||
public function data(mixed $data = null, string $message = '', int|\BackedEnum $code = 200, $error = null): static | ||
|
@@ -132,7 +133,7 @@ protected function formatMessage(int $code, string $message = ''): ?string | |
$localizationKey = implode('.', [Config::get('response.locale', 'enums'), $code]); | ||
|
||
return match (true) { | ||
! $message && Lang::has($localizationKey) => Lang::get($localizationKey), | ||
!$message && Lang::has($localizationKey) => Lang::get($localizationKey), | ||
default => $message | ||
}; | ||
} | ||
|
@@ -235,7 +236,7 @@ protected function formatDataFields(array $data): array | |
{ | ||
return tap($data, function (&$item) { | ||
foreach ($this->config as $key => $config) { | ||
if (! Arr::has($item, $key)) { | ||
if (!Arr::has($item, $key)) { | ||
continue; | ||
} | ||
|
||
|
@@ -248,7 +249,7 @@ protected function formatDataFields(array $data): array | |
$key = $alias; | ||
} | ||
|
||
if (! $show) { | ||
if (!$show) { | ||
$item = Arr::except($item, $key); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
/* | ||
* This file is part of the jiannei/laravel-response. | ||
* | ||
* (c) Jiannei <[email protected]> | ||
* (c) Jiannei <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
|
@@ -27,7 +27,8 @@ trait ExceptionTrait | |
/** | ||
* Custom Normal Exception response. | ||
* | ||
* @param Throwable|Exception $e | ||
* @param Throwable|Exception $e | ||
* | ||
* @return JsonResponse | ||
*/ | ||
protected function prepareJsonResponse($request, $e) | ||
|
@@ -36,7 +37,7 @@ protected function prepareJsonResponse($request, $e) | |
// 或者是 ajax 请求,header 中包含 X-Requested-With:XMLHttpRequest; | ||
$exceptionConfig = Config::get('response.exception.'.get_class($e)); | ||
|
||
if ($exceptionConfig === false) { | ||
if (false === $exceptionConfig) { | ||
return parent::prepareJsonResponse($request, $e); | ||
} | ||
|
||
|
@@ -78,14 +79,15 @@ protected function buildFailedValidationResponse(Request $request, array $errors | |
/** | ||
* Custom Failed Validation Response for Laravel. | ||
* | ||
* @param Request $request | ||
* @param Request $request | ||
* | ||
* @return JsonResponse | ||
*/ | ||
protected function invalidJson($request, ValidationException $exception) | ||
{ | ||
$exceptionConfig = Config::get('response.exception.'.ValidationException::class); | ||
|
||
return $exceptionConfig !== false ? Response::fail( | ||
return false !== $exceptionConfig ? Response::fail( | ||
$exception->validator->errors()->first(), | ||
Arr::get($exceptionConfig, 'code', 422), | ||
$exception->errors() | ||
|
@@ -95,14 +97,15 @@ protected function invalidJson($request, ValidationException $exception) | |
/** | ||
* Custom Failed Authentication Response for Laravel. | ||
* | ||
* @param Request $request | ||
* @return \Illuminate\Http\RedirectResponse | JsonResponse | ||
* @param Request $request | ||
* | ||
* @return \Illuminate\Http\RedirectResponse|JsonResponse | ||
*/ | ||
protected function unauthenticated($request, AuthenticationException $exception) | ||
{ | ||
$exceptionConfig = Config::get('response.exception.'.AuthenticationException::class); | ||
|
||
return $exceptionConfig !== false && $request->expectsJson() | ||
return false !== $exceptionConfig && $request->expectsJson() | ||
? Response::errorUnauthorized($exceptionConfig['message'] ?? $exception->getMessage()) | ||
: parent::unauthenticated($request, $exception); | ||
} | ||
|
Oops, something went wrong.