Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Transformers/Api/Application/ServerTransformer.php
10280 views
1
<?php
2
3
namespace Pterodactyl\Transformers\Api\Application;
4
5
use Pterodactyl\Models\Server;
6
use League\Fractal\Resource\Item;
7
use League\Fractal\Resource\Collection;
8
use League\Fractal\Resource\NullResource;
9
use Pterodactyl\Services\Acl\Api\AdminAcl;
10
use Pterodactyl\Services\Servers\EnvironmentService;
11
12
class ServerTransformer extends BaseTransformer
13
{
14
private EnvironmentService $environmentService;
15
16
/**
17
* List of resources that can be included.
18
*/
19
protected array $availableIncludes = [
20
'allocations',
21
'user',
22
'subusers',
23
'nest',
24
'egg',
25
'variables',
26
'location',
27
'node',
28
'databases',
29
'transfer',
30
];
31
32
/**
33
* Perform dependency injection.
34
*/
35
public function handle(EnvironmentService $environmentService)
36
{
37
$this->environmentService = $environmentService;
38
}
39
40
/**
41
* Return the resource name for the JSONAPI output.
42
*/
43
public function getResourceName(): string
44
{
45
return Server::RESOURCE_NAME;
46
}
47
48
/**
49
* Return a generic transformed server array.
50
*/
51
public function transform(Server $server): array
52
{
53
return [
54
'id' => $server->getKey(),
55
'external_id' => $server->external_id,
56
'uuid' => $server->uuid,
57
'identifier' => $server->uuidShort,
58
'name' => $server->name,
59
'description' => $server->description,
60
'status' => $server->status,
61
// This field is deprecated, please use "status".
62
'suspended' => $server->isSuspended(),
63
'limits' => [
64
'memory' => $server->memory,
65
'swap' => $server->swap,
66
'disk' => $server->disk,
67
'io' => $server->io,
68
'cpu' => $server->cpu,
69
'threads' => $server->threads,
70
'oom_disabled' => $server->oom_disabled,
71
],
72
'feature_limits' => [
73
'databases' => $server->database_limit,
74
'allocations' => $server->allocation_limit,
75
'backups' => $server->backup_limit,
76
],
77
'user' => $server->owner_id,
78
'node' => $server->node_id,
79
'allocation' => $server->allocation_id,
80
'nest' => $server->nest_id,
81
'egg' => $server->egg_id,
82
'container' => [
83
'startup_command' => $server->startup,
84
'image' => $server->image,
85
// This field is deprecated, please use "status".
86
'installed' => $server->isInstalled() ? 1 : 0,
87
'environment' => $this->environmentService->handle($server),
88
],
89
$server->getUpdatedAtColumn() => $this->formatTimestamp($server->updated_at),
90
$server->getCreatedAtColumn() => $this->formatTimestamp($server->created_at),
91
];
92
}
93
94
/**
95
* Return a generic array of allocations for this server.
96
*
97
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
98
*/
99
public function includeAllocations(Server $server): Collection|NullResource
100
{
101
if (!$this->authorize(AdminAcl::RESOURCE_ALLOCATIONS)) {
102
return $this->null();
103
}
104
105
$server->loadMissing('allocations');
106
107
return $this->collection($server->getRelation('allocations'), $this->makeTransformer(AllocationTransformer::class), 'allocation');
108
}
109
110
/**
111
* Return a generic array of data about subusers for this server.
112
*
113
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
114
*/
115
public function includeSubusers(Server $server): Collection|NullResource
116
{
117
if (!$this->authorize(AdminAcl::RESOURCE_USERS)) {
118
return $this->null();
119
}
120
121
$server->loadMissing('subusers');
122
123
return $this->collection($server->getRelation('subusers'), $this->makeTransformer(SubuserTransformer::class), 'subuser');
124
}
125
126
/**
127
* Return a generic array of data about subusers for this server.
128
*
129
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
130
*/
131
public function includeUser(Server $server): Item|NullResource
132
{
133
if (!$this->authorize(AdminAcl::RESOURCE_USERS)) {
134
return $this->null();
135
}
136
137
$server->loadMissing('user');
138
139
return $this->item($server->getRelation('user'), $this->makeTransformer(UserTransformer::class), 'user');
140
}
141
142
/**
143
* Return a generic array with nest information for this server.
144
*
145
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
146
*/
147
public function includeNest(Server $server): Item|NullResource
148
{
149
if (!$this->authorize(AdminAcl::RESOURCE_NESTS)) {
150
return $this->null();
151
}
152
153
$server->loadMissing('nest');
154
155
return $this->item($server->getRelation('nest'), $this->makeTransformer(NestTransformer::class), 'nest');
156
}
157
158
/**
159
* Return a generic array with egg information for this server.
160
*
161
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
162
*/
163
public function includeEgg(Server $server): Item|NullResource
164
{
165
if (!$this->authorize(AdminAcl::RESOURCE_EGGS)) {
166
return $this->null();
167
}
168
169
$server->loadMissing('egg');
170
171
return $this->item($server->getRelation('egg'), $this->makeTransformer(EggTransformer::class), 'egg');
172
}
173
174
/**
175
* Return a generic array of data about subusers for this server.
176
*
177
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
178
*/
179
public function includeVariables(Server $server): Collection|NullResource
180
{
181
if (!$this->authorize(AdminAcl::RESOURCE_SERVERS)) {
182
return $this->null();
183
}
184
185
$server->loadMissing('variables');
186
187
return $this->collection($server->getRelation('variables'), $this->makeTransformer(ServerVariableTransformer::class), 'server_variable');
188
}
189
190
/**
191
* Return a generic array with location information for this server.
192
*
193
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
194
*/
195
public function includeLocation(Server $server): Item|NullResource
196
{
197
if (!$this->authorize(AdminAcl::RESOURCE_LOCATIONS)) {
198
return $this->null();
199
}
200
201
$server->loadMissing('location');
202
203
return $this->item($server->getRelation('location'), $this->makeTransformer(LocationTransformer::class), 'location');
204
}
205
206
/**
207
* Return a generic array with node information for this server.
208
*
209
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
210
*/
211
public function includeNode(Server $server): Item|NullResource
212
{
213
if (!$this->authorize(AdminAcl::RESOURCE_NODES)) {
214
return $this->null();
215
}
216
217
$server->loadMissing('node');
218
219
return $this->item($server->getRelation('node'), $this->makeTransformer(NodeTransformer::class), 'node');
220
}
221
222
/**
223
* Return a generic array with database information for this server.
224
*
225
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
226
*/
227
public function includeDatabases(Server $server): Collection|NullResource
228
{
229
if (!$this->authorize(AdminAcl::RESOURCE_SERVER_DATABASES)) {
230
return $this->null();
231
}
232
233
$server->loadMissing('databases');
234
235
return $this->collection($server->getRelation('databases'), $this->makeTransformer(ServerDatabaseTransformer::class), 'databases');
236
}
237
}
238
239