Simplify transformer logic

This commit is contained in:
DaneEveritt 2022-05-22 16:23:22 -04:00
parent f7fc67344e
commit 33bafe9277
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
6 changed files with 32 additions and 44 deletions

View file

@ -58,17 +58,11 @@ class DisplayException extends PterodactylException
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
* @return \Illuminate\Http\RedirectResponse
*/
public function render($request)
{
if ($request->expectsJson()) {
return response()->json(Handler::convertToArray($this, [
'detail' => $this->getMessage(),
]), method_exists($this, 'getStatusCode') ? $this->getStatusCode() : Response::HTTP_BAD_REQUEST);
}
Container::getInstance()->make(AlertsMessageBag::class)->danger($this->getMessage())->flash();
app(AlertsMessageBag::class)->danger($this->getMessage())->flash();
return redirect()->back()->withInput();
}

View file

@ -197,7 +197,7 @@ class Handler extends ExceptionHandler
/**
* Return the exception as a JSONAPI representation for use on API requests.
*/
public static function convertToArray(Throwable $exception, array $override = []): array
protected function convertExceptionToArray(Throwable $exception, array $override = []): array
{
$match = self::$exceptionResponseCodes[get_class($exception)] ?? null;
@ -226,7 +226,13 @@ class Handler extends ExceptionHandler
'file' => str_replace(Application::getInstance()->basePath(), '', $exception->getFile()),
],
'meta' => [
'trace' => explode("\n", $exception->getTraceAsString()),
'trace' => Collection::make($exception->getTrace())
->map(fn ($trace) => Arr::except($trace, ['args']))
->all(),
'previous' => Collection::make($this->extractPrevious($exception))
->map(fn ($exception) => $exception->getTrace())
->map(fn ($trace) => Arr::except($trace, ['args']))
->all(),
],
]);
}
@ -252,20 +258,30 @@ class Handler extends ExceptionHandler
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
return new JsonResponse(self::convertToArray($exception), JsonResponse::HTTP_UNAUTHORIZED);
return new JsonResponse($this->convertExceptionToArray($exception), JsonResponse::HTTP_UNAUTHORIZED);
}
return $this->container->make('redirect')->guest('/auth/login');
return redirect()->guest('/auth/login');
}
/**
* Converts an exception into an array to render in the response. Overrides
* Laravel's built-in converter to output as a JSONAPI spec compliant object.
* Extracts all of the previous exceptions that lead to the one passed into this
* function being thrown.
*
* @return array
* @param \Throwable $e
* @return \Throwable[]
*/
protected function convertExceptionToArray(Throwable $exception)
protected function extractPrevious(Throwable $e): array
{
return self::convertToArray($exception);
$previous = [];
while ($value = $e->getPrevious()) {
if (!$value instanceof Throwable) {
break;
}
$previous[] = $value;
$e = $value;
}
return $previous;
}
}

View file

@ -3,7 +3,6 @@
namespace Pterodactyl\Http\Controllers\Api\Client;
use Webmozart\Assert\Assert;
use Pterodactyl\Transformers\Daemon\BaseDaemonTransformer;
use Pterodactyl\Transformers\Api\Client\BaseClientTransformer;
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
@ -55,13 +54,8 @@ abstract class ClientApiController extends ApplicationApiController
*/
public function getTransformer(string $abstract)
{
Assert::methodExists($abstract, 'fromRequest');
Assert::subclassOf($abstract, BaseClientTransformer::class);
/** @var T $transformer */
$transformer = $abstract::fromRequest($this->request);
Assert::isInstanceOfAny($transformer, [BaseClientTransformer::class, BaseDaemonTransformer::class]);
return $transformer;
return $abstract::fromRequest($this->request);
}
}

View file

@ -10,7 +10,7 @@ use Illuminate\Http\JsonResponse;
use Pterodactyl\Services\Nodes\NodeJWTService;
use Illuminate\Contracts\Routing\ResponseFactory;
use Pterodactyl\Repositories\Wings\DaemonFileRepository;
use Pterodactyl\Transformers\Daemon\FileObjectTransformer;
use Pterodactyl\Transformers\Api\Client\FileObjectTransformer;
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\CopyFileRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Files\PullFileRequest;

View file

@ -1,19 +1,12 @@
<?php
namespace Pterodactyl\Transformers\Daemon;
namespace Pterodactyl\Transformers\Api\Client;
use Carbon\Carbon;
use Illuminate\Support\Arr;
class FileObjectTransformer extends BaseDaemonTransformer
class FileObjectTransformer extends BaseClientTransformer
{
/**
* An array of files we allow editing in the Panel.
*
* @var array
*/
private $editable = [];
/**
* Transform a file object response from the daemon into a standardized response.
*

View file

@ -1,9 +0,0 @@
<?php
namespace Pterodactyl\Transformers\Daemon;
use League\Fractal\TransformerAbstract;
abstract class BaseDaemonTransformer extends TransformerAbstract
{
}