Upgrade to Laravel 9 (#4413)

Co-authored-by: DaneEveritt <dane@daneeveritt.com>
This commit is contained in:
Matthew Penner 2022-10-14 10:59:20 -06:00 committed by GitHub
parent 95e15d2c8a
commit cbcf62086f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
573 changed files with 4387 additions and 9411 deletions

View file

@ -1,11 +1,4 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Exceptions;

View file

@ -1,11 +1,4 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Exceptions;

View file

@ -4,9 +4,12 @@ namespace Pterodactyl\Exceptions;
use Exception;
use Throwable;
use Illuminate\Http\Request;
use Psr\Log\LoggerInterface;
use Illuminate\Http\Response;
use Illuminate\Http\JsonResponse;
use Illuminate\Container\Container;
use Illuminate\Http\RedirectResponse;
use Prologue\Alerts\AlertsMessageBag;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
@ -18,44 +21,24 @@ class DisplayException extends PterodactylException implements HttpExceptionInte
public const LEVEL_ERROR = 'error';
/**
* @var string
* DisplayException constructor.
*/
protected $level;
/**
* Exception constructor.
*
* @param string $message
* @param string $level
* @param int $code
*/
public function __construct($message, Throwable $previous = null, $level = self::LEVEL_ERROR, $code = 0)
public function __construct(string $message, ?Throwable $previous = null, protected string $level = self::LEVEL_ERROR, int $code = 0)
{
parent::__construct($message, $code, $previous);
$this->level = $level;
}
/**
* @return string
*/
public function getErrorLevel()
public function getErrorLevel(): string
{
return $this->level;
}
/**
* @return int
*/
public function getStatusCode()
public function getStatusCode(): int
{
return Response::HTTP_BAD_REQUEST;
}
/**
* @return array
*/
public function getHeaders()
public function getHeaders(): array
{
return [];
}
@ -64,12 +47,8 @@ class DisplayException extends PterodactylException implements HttpExceptionInte
* Render the exception to the user by adding a flashed message to the session
* and then redirecting them back to the page that they came from. If the
* request originated from an API hit, return the error in JSONAPI spec format.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
*/
public function render($request)
public function render(Request $request): JsonResponse|RedirectResponse
{
if ($request->expectsJson()) {
return response()->json(Handler::toArray($this), $this->getStatusCode(), $this->getHeaders());
@ -84,9 +63,7 @@ class DisplayException extends PterodactylException implements HttpExceptionInte
* Log the exception to the logs using the defined error level only if the previous
* exception is set.
*
* @return mixed
*
* @throws \Exception
* @throws \Throwable
*/
public function report()
{
@ -96,7 +73,7 @@ class DisplayException extends PterodactylException implements HttpExceptionInte
try {
$logger = Container::getInstance()->make(LoggerInterface::class);
} catch (Exception $ex) {
} catch (Exception) {
throw $this->getPrevious();
}

View file

@ -7,18 +7,20 @@ use Throwable;
use PDOException;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Swift_TransportException;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Collection;
use Illuminate\Container\Container;
use Illuminate\Database\Connection;
use Illuminate\Http\RedirectResponse;
use Illuminate\Foundation\Application;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Session\TokenMismatchException;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpFoundation\Response;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\Mailer\Exception\TransportException;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
@ -26,7 +28,7 @@ use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
class Handler extends ExceptionHandler
{
/**
* Laravel's validation parser formats custom rules using the class name
* The validation parser in Laravel formats custom rules using the class name
* resulting in some weird rule names. This string will be parsed out and
* replaced with 'p_' in the response code.
*/
@ -34,8 +36,6 @@ class Handler extends ExceptionHandler
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
AuthenticationException::class,
@ -50,8 +50,6 @@ class Handler extends ExceptionHandler
/**
* Maps exceptions to a specific response code. This handles special exception
* types that don't have a defined response code.
*
* @var array<string, int>
*/
protected static array $exceptionResponseCodes = [
AuthenticationException::class => 401,
@ -61,8 +59,6 @@ class Handler extends ExceptionHandler
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'token',
@ -89,7 +85,7 @@ class Handler extends ExceptionHandler
$ex = $this->generateCleanedExceptionStack($ex);
});
$this->reportable(function (Swift_TransportException $ex) {
$this->reportable(function (TransportException $ex) {
$ex = $this->generateCleanedExceptionStack($ex);
});
}
@ -125,11 +121,9 @@ class Handler extends ExceptionHandler
*
* @param \Illuminate\Http\Request $request
*
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Throwable
*/
public function render($request, Throwable $exception)
public function render($request, Throwable $e): Response
{
$connections = $this->container->make(Connection::class);
@ -146,7 +140,7 @@ class Handler extends ExceptionHandler
$connections->rollBack(0);
}
return parent::render($request, $exception);
return parent::render($request, $e);
}
/**
@ -154,10 +148,8 @@ class Handler extends ExceptionHandler
* calls to the API.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\JsonResponse
*/
public function invalidJson($request, ValidationException $exception)
public function invalidJson($request, ValidationException $exception): JsonResponse
{
$codes = Collection::make($exception->validator->failed())->mapWithKeys(function ($reasons, $field) {
$cleaned = [];
@ -197,21 +189,21 @@ class Handler extends ExceptionHandler
/**
* Return the exception as a JSONAPI representation for use on API requests.
*/
protected function convertExceptionToArray(Throwable $exception, array $override = []): array
protected function convertExceptionToArray(Throwable $e, array $override = []): array
{
$match = self::$exceptionResponseCodes[get_class($exception)] ?? null;
$match = self::$exceptionResponseCodes[get_class($e)] ?? null;
$error = [
'code' => class_basename($exception),
'status' => method_exists($exception, 'getStatusCode')
? strval($exception->getStatusCode())
'code' => class_basename($e),
'status' => method_exists($e, 'getStatusCode')
? strval($e->getStatusCode())
: strval($match ?? '500'),
'detail' => $exception instanceof HttpExceptionInterface || !is_null($match)
? $exception->getMessage()
'detail' => $e instanceof HttpExceptionInterface || !is_null($match)
? $e->getMessage()
: 'An unexpected error was encountered while processing this request, please try again.',
];
if ($exception instanceof ModelNotFoundException || $exception->getPrevious() instanceof ModelNotFoundException) {
if ($e instanceof ModelNotFoundException || $e->getPrevious() instanceof ModelNotFoundException) {
// Show a nicer error message compared to the standard "No query results for model"
// response that is normally returned. If we are in debug mode this will get overwritten
// with a more specific error message to help narrow down things.
@ -220,17 +212,17 @@ class Handler extends ExceptionHandler
if (config('app.debug')) {
$error = array_merge($error, [
'detail' => $exception->getMessage(),
'detail' => $e->getMessage(),
'source' => [
'line' => $exception->getLine(),
'file' => str_replace(Application::getInstance()->basePath(), '', $exception->getFile()),
'line' => $e->getLine(),
'file' => str_replace(Application::getInstance()->basePath(), '', $e->getFile()),
],
'meta' => [
'trace' => Collection::make($exception->getTrace())
'trace' => Collection::make($e->getTrace())
->map(fn ($trace) => Arr::except($trace, ['args']))
->all(),
'previous' => Collection::make($this->extractPrevious($exception))
->map(fn ($exception) => $exception->getTrace())
'previous' => Collection::make($this->extractPrevious($e))
->map(fn ($exception) => $e->getTrace())
->map(fn ($trace) => Arr::except($trace, ['args']))
->all(),
],
@ -252,10 +244,8 @@ class Handler extends ExceptionHandler
* Convert an authentication exception into an unauthenticated response.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
*/
protected function unauthenticated($request, AuthenticationException $exception)
protected function unauthenticated($request, AuthenticationException $exception): JsonResponse|RedirectResponse
{
if ($request->expectsJson()) {
return new JsonResponse($this->convertExceptionToArray($exception), JsonResponse::HTTP_UNAUTHORIZED);
@ -265,7 +255,7 @@ class Handler extends ExceptionHandler
}
/**
* Extracts all of the previous exceptions that lead to the one passed into this
* Extracts all the previous exceptions that lead to the one passed into this
* function being thrown.
*
* @return \Throwable[]

View file

@ -1,11 +1,4 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Exceptions\Http\Base;

View file

@ -12,20 +12,15 @@ use Pterodactyl\Exceptions\DisplayException;
*/
class DaemonConnectionException extends DisplayException
{
/**
* @var int
*/
private $statusCode = Response::HTTP_GATEWAY_TIMEOUT;
private int $statusCode = Response::HTTP_GATEWAY_TIMEOUT;
/**
* Every request to the Wings instance will return a unique X-Request-Id header
* which allows for all errors to be efficiently tied to a specific request that
* triggered them, and gives users a more direct method of informing hosts when
* something goes wrong.
*
* @var string|null
*/
private $requestId;
private ?string $requestId;
/**
* Throw a displayable exception caused by a daemon connection error.
@ -34,7 +29,7 @@ class DaemonConnectionException extends DisplayException
{
/** @var \GuzzleHttp\Psr7\Response|null $response */
$response = method_exists($previous, 'getResponse') ? $previous->getResponse() : null;
$this->requestId = $response ? $response->getHeaderLine('X-Request-Id') : null;
$this->requestId = $response?->getHeaderLine('X-Request-Id');
if ($useStatusCode) {
$this->statusCode = is_null($response) ? $this->statusCode : $response->getStatusCode();
@ -72,8 +67,6 @@ class DaemonConnectionException extends DisplayException
/**
* Override the default reporting method for DisplayException by just logging immediately
* here and including the specific X-Request-Id header that was returned by the call.
*
* @return void
*/
public function report()
{
@ -84,18 +77,13 @@ class DaemonConnectionException extends DisplayException
/**
* Return the HTTP status code for this exception.
*
* @return int
*/
public function getStatusCode()
public function getStatusCode(): int
{
return $this->statusCode;
}
/**
* @return string|null
*/
public function getRequestId()
public function getRequestId(): ?string
{
return $this->requestId;
}

View file

@ -1,11 +1,4 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Exceptions\Http\Server;

View file

@ -2,6 +2,7 @@
namespace Pterodactyl\Exceptions\Model;
use Illuminate\Support\MessageBag;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Validation\Validator;
use Pterodactyl\Exceptions\PterodactylException;
@ -10,20 +11,10 @@ use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
class DataValidationException extends PterodactylException implements HttpExceptionInterface, MessageProvider
{
/**
* The validator instance.
*/
protected Validator $validator;
/**
* The underlying model instance that triggered this exception.
*/
protected Model $model;
/**
* DataValidationException constructor.
*/
public function __construct(Validator $validator, Model $model)
public function __construct(protected Validator $validator, protected Model $model)
{
$message = sprintf(
'Could not save %s[%s]: failed to validate data: %s',
@ -33,35 +24,25 @@ class DataValidationException extends PterodactylException implements HttpExcept
);
parent::__construct($message);
$this->validator = $validator;
$this->model = $model;
}
/**
* Return the validator message bag.
*
* @return \Illuminate\Support\MessageBag
*/
public function getMessageBag()
public function getMessageBag(): MessageBag
{
return $this->validator->errors();
}
/**
* Return the status code for this request.
*
* @return int
*/
public function getStatusCode()
public function getStatusCode(): int
{
return 500;
}
/**
* @return array
*/
public function getHeaders()
public function getHeaders(): array
{
return [];
}

View file

@ -9,20 +9,16 @@ class RecordNotFoundException extends RepositoryException implements HttpExcepti
{
/**
* Returns the status code.
*
* @return int
*/
public function getStatusCode()
public function getStatusCode(): int
{
return Response::HTTP_NOT_FOUND;
}
/**
* Returns response headers.
*
* @return array
*/
public function getHeaders()
public function getHeaders(): array
{
return [];
}

View file

@ -8,10 +8,8 @@ class InvalidPortMappingException extends DisplayException
{
/**
* InvalidPortMappingException constructor.
*
* @param mixed $port
*/
public function __construct($port)
public function __construct(mixed $port)
{
parent::__construct(trans('exceptions.allocations.invalid_mapping', ['port' => $port]));
}

View file

@ -1,11 +1,4 @@
<?php
/*
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Exceptions\Service\Egg;

View file

@ -1,11 +1,4 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Exceptions\Service\Egg;

View file

@ -1,11 +1,4 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Exceptions\Service\Egg;

View file

@ -1,11 +1,4 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Exceptions\Service\Egg\Variable;

View file

@ -7,10 +7,7 @@ use Pterodactyl\Exceptions\DisplayException;
class HasActiveServersException extends DisplayException
{
/**
* @return int
*/
public function getStatusCode()
public function getStatusCode(): int
{
return Response::HTTP_BAD_REQUEST;
}

View file

@ -1,11 +1,4 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Exceptions\Service\Helper;

View file

@ -1,11 +1,4 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Exceptions\Service;

View file

@ -7,10 +7,7 @@ use Pterodactyl\Exceptions\DisplayException;
class HasActiveNodesException extends DisplayException
{
/**
* @return int
*/
public function getStatusCode()
public function getStatusCode(): int
{
return Response::HTTP_BAD_REQUEST;
}

View file

@ -1,11 +1,4 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Exceptions\Service\Schedule\Task;

View file

@ -1,11 +1,4 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Exceptions\Service\Server;

View file

@ -1,11 +1,4 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Exceptions\Service\Subuser;

View file

@ -1,11 +1,4 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Exceptions\Service\Subuser;