From bfb28f949da6d1aace63d996d8b98fc509b56fd6 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Thu, 9 Jul 2020 19:17:24 -0700 Subject: [PATCH] [Breaking] Return server allocations automatically as a relation object --- .../Api/Client/ClientApiController.php | 4 ++- .../Api/Client/AllocationTransformer.php | 6 ++-- .../Api/Client/ServerTransformer.php | 29 ++++++++++++++----- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/app/Http/Controllers/Api/Client/ClientApiController.php b/app/Http/Controllers/Api/Client/ClientApiController.php index b3d0d0ea3..56c3db1f7 100644 --- a/app/Http/Controllers/Api/Client/ClientApiController.php +++ b/app/Http/Controllers/Api/Client/ClientApiController.php @@ -28,10 +28,12 @@ abstract class ClientApiController extends ApplicationApiController /** * Returns the parsed includes for this request. + * + * @return string[] */ protected function parseIncludes() { - $includes = $this->request->query('include'); + $includes = $this->request->query('include') ?? []; if (! is_string($includes)) { return $includes; diff --git a/app/Transformers/Api/Client/AllocationTransformer.php b/app/Transformers/Api/Client/AllocationTransformer.php index 055afdae3..8f83aac7e 100644 --- a/app/Transformers/Api/Client/AllocationTransformer.php +++ b/app/Transformers/Api/Client/AllocationTransformer.php @@ -24,13 +24,11 @@ class AllocationTransformer extends BaseClientTransformer */ public function transform(Allocation $model) { - $model->loadMissing('server'); - return [ 'ip' => $model->ip, - 'alias' => $model->ip_alias, + 'ip_alias' => $model->ip_alias, 'port' => $model->port, - 'default' => $model->getRelation('server')->allocation_id === $model->id, + 'is_default' => $model->server->allocation_id === $model->id, ]; } } diff --git a/app/Transformers/Api/Client/ServerTransformer.php b/app/Transformers/Api/Client/ServerTransformer.php index 360ba20fa..148fd8990 100644 --- a/app/Transformers/Api/Client/ServerTransformer.php +++ b/app/Transformers/Api/Client/ServerTransformer.php @@ -9,6 +9,11 @@ use Pterodactyl\Models\Allocation; class ServerTransformer extends BaseClientTransformer { + /** + * @var string[] + */ + protected $defaultIncludes = ['allocations']; + /** * @var array */ @@ -42,14 +47,6 @@ class ServerTransformer extends BaseClientTransformer 'port' => $server->node->daemonSFTP, ], 'description' => $server->description, - 'allocations' => $server->allocations->map(function (Allocation $allocation) use ($server) { - return [ - 'ip' => $allocation->ip, - 'ip_alias' => $allocation->ip_alias, - 'port' => $allocation->port, - 'is_default' => $allocation->id === $server->allocation_id, - ]; - }), 'limits' => [ 'memory' => $server->memory, 'swap' => $server->swap, @@ -67,6 +64,22 @@ class ServerTransformer extends BaseClientTransformer ]; } + /** + * Returns the allocations associated with this server. + * + * @param \Pterodactyl\Models\Server $server + * @return \League\Fractal\Resource\Collection + * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException + */ + public function includeAllocations(Server $server) + { + return $this->collection( + $server->allocations, + $this->makeTransformer(AllocationTransformer::class), + Allocation::RESOURCE_NAME + ); + } + /** * Returns the egg associated with this server. *