Skip to content

Commit bfb28f9

Browse files
committed
[Breaking] Return server allocations automatically as a relation object
1 parent 5c18fd1 commit bfb28f9

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

app/Http/Controllers/Api/Client/ClientApiController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ protected function getIncludesForTransformer(BaseClientTransformer $transformer,
2828

2929
/**
3030
* Returns the parsed includes for this request.
31+
*
32+
* @return string[]
3133
*/
3234
protected function parseIncludes()
3335
{
34-
$includes = $this->request->query('include');
36+
$includes = $this->request->query('include') ?? [];
3537

3638
if (! is_string($includes)) {
3739
return $includes;

app/Transformers/Api/Client/AllocationTransformer.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@ public function getResourceName(): string
2424
*/
2525
public function transform(Allocation $model)
2626
{
27-
$model->loadMissing('server');
28-
2927
return [
3028
'ip' => $model->ip,
31-
'alias' => $model->ip_alias,
29+
'ip_alias' => $model->ip_alias,
3230
'port' => $model->port,
33-
'default' => $model->getRelation('server')->allocation_id === $model->id,
31+
'is_default' => $model->server->allocation_id === $model->id,
3432
];
3533
}
3634
}

app/Transformers/Api/Client/ServerTransformer.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99

1010
class ServerTransformer extends BaseClientTransformer
1111
{
12+
/**
13+
* @var string[]
14+
*/
15+
protected $defaultIncludes = ['allocations'];
16+
1217
/**
1318
* @var array
1419
*/
@@ -42,14 +47,6 @@ public function transform(Server $server): array
4247
'port' => $server->node->daemonSFTP,
4348
],
4449
'description' => $server->description,
45-
'allocations' => $server->allocations->map(function (Allocation $allocation) use ($server) {
46-
return [
47-
'ip' => $allocation->ip,
48-
'ip_alias' => $allocation->ip_alias,
49-
'port' => $allocation->port,
50-
'is_default' => $allocation->id === $server->allocation_id,
51-
];
52-
}),
5350
'limits' => [
5451
'memory' => $server->memory,
5552
'swap' => $server->swap,
@@ -67,6 +64,22 @@ public function transform(Server $server): array
6764
];
6865
}
6966

67+
/**
68+
* Returns the allocations associated with this server.
69+
*
70+
* @param \Pterodactyl\Models\Server $server
71+
* @return \League\Fractal\Resource\Collection
72+
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
73+
*/
74+
public function includeAllocations(Server $server)
75+
{
76+
return $this->collection(
77+
$server->allocations,
78+
$this->makeTransformer(AllocationTransformer::class),
79+
Allocation::RESOURCE_NAME
80+
);
81+
}
82+
7083
/**
7184
* Returns the egg associated with this server.
7285
*

0 commit comments

Comments
 (0)