diff --git a/app/Http/Controllers/Api/Client/ClientApiController.php b/app/Http/Controllers/Api/Client/ClientApiController.php index b3d0d0ea..56c3db1f 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 055afdae..8f83aac7 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 360ba20f..148fd899 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. *