Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Transformers/Api/Application/EggTransformer.php
10279 views
1
<?php
2
3
namespace Pterodactyl\Transformers\Api\Application;
4
5
use Illuminate\Support\Arr;
6
use Pterodactyl\Models\Egg;
7
use Pterodactyl\Models\Nest;
8
use Pterodactyl\Models\Server;
9
use League\Fractal\Resource\Item;
10
use Pterodactyl\Models\EggVariable;
11
use League\Fractal\Resource\Collection;
12
use League\Fractal\Resource\NullResource;
13
use Pterodactyl\Services\Acl\Api\AdminAcl;
14
15
class EggTransformer extends BaseTransformer
16
{
17
/**
18
* Relationships that can be loaded onto this transformation.
19
*/
20
protected array $availableIncludes = [
21
'nest',
22
'servers',
23
'config',
24
'script',
25
'variables',
26
];
27
28
/**
29
* Return the resource name for the JSONAPI output.
30
*/
31
public function getResourceName(): string
32
{
33
return Egg::RESOURCE_NAME;
34
}
35
36
/**
37
* Transform an Egg model into a representation that can be consumed by
38
* the application api.
39
*
40
* @throws \JsonException
41
*/
42
public function transform(Egg $model): array
43
{
44
$files = json_decode($model->config_files, true, 512, JSON_THROW_ON_ERROR);
45
if (empty($files)) {
46
$files = new \stdClass();
47
}
48
49
return [
50
'id' => $model->id,
51
'uuid' => $model->uuid,
52
'name' => $model->name,
53
'nest' => $model->nest_id,
54
'author' => $model->author,
55
'description' => $model->description,
56
// "docker_image" is deprecated, but left here to avoid breaking too many things at once
57
// in external software. We'll remove it down the road once things have gotten the chance
58
// to upgrade to using "docker_images".
59
'docker_image' => count($model->docker_images) > 0 ? Arr::first($model->docker_images) : '',
60
'docker_images' => $model->docker_images,
61
'config' => [
62
'files' => $files,
63
'startup' => json_decode($model->config_startup, true),
64
'stop' => $model->config_stop,
65
'logs' => json_decode($model->config_logs, true),
66
'file_denylist' => $model->file_denylist,
67
'extends' => $model->config_from,
68
],
69
'startup' => $model->startup,
70
'script' => [
71
'privileged' => $model->script_is_privileged,
72
'install' => $model->script_install,
73
'entry' => $model->script_entry,
74
'container' => $model->script_container,
75
'extends' => $model->copy_script_from,
76
],
77
$model->getCreatedAtColumn() => $this->formatTimestamp($model->created_at),
78
$model->getUpdatedAtColumn() => $this->formatTimestamp($model->updated_at),
79
];
80
}
81
82
/**
83
* Include the Nest relationship for the given Egg in the transformation.
84
*
85
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
86
*/
87
public function includeNest(Egg $model): Item|NullResource
88
{
89
if (!$this->authorize(AdminAcl::RESOURCE_NESTS)) {
90
return $this->null();
91
}
92
93
$model->loadMissing('nest');
94
95
return $this->item($model->getRelation('nest'), $this->makeTransformer(NestTransformer::class), Nest::RESOURCE_NAME);
96
}
97
98
/**
99
* Include the Servers relationship for the given Egg in the transformation.
100
*
101
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
102
*/
103
public function includeServers(Egg $model): Collection|NullResource
104
{
105
if (!$this->authorize(AdminAcl::RESOURCE_SERVERS)) {
106
return $this->null();
107
}
108
109
$model->loadMissing('servers');
110
111
return $this->collection($model->getRelation('servers'), $this->makeTransformer(ServerTransformer::class), Server::RESOURCE_NAME);
112
}
113
114
/**
115
* Include more detailed information about the configuration if this Egg is
116
* extending another.
117
*/
118
public function includeConfig(Egg $model): Item|NullResource
119
{
120
if (is_null($model->config_from)) {
121
return $this->null();
122
}
123
124
$model->loadMissing('configFrom');
125
126
return $this->item($model, function (Egg $model) {
127
return [
128
'files' => json_decode($model->inherit_config_files),
129
'startup' => json_decode($model->inherit_config_startup),
130
'stop' => $model->inherit_config_stop,
131
'logs' => json_decode($model->inherit_config_logs),
132
];
133
});
134
}
135
136
/**
137
* Include more detailed information about the script configuration if the
138
* Egg is extending another.
139
*/
140
public function includeScript(Egg $model): Item|NullResource
141
{
142
if (is_null($model->copy_script_from)) {
143
return $this->null();
144
}
145
146
$model->loadMissing('scriptFrom');
147
148
return $this->item($model, function (Egg $model) {
149
return [
150
'privileged' => $model->script_is_privileged,
151
'install' => $model->copy_script_install,
152
'entry' => $model->copy_script_entry,
153
'container' => $model->copy_script_container,
154
];
155
});
156
}
157
158
/**
159
* Include the variables that are defined for this Egg.
160
*
161
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
162
*/
163
public function includeVariables(Egg $model): Collection|NullResource
164
{
165
if (!$this->authorize(AdminAcl::RESOURCE_EGGS)) {
166
return $this->null();
167
}
168
169
$model->loadMissing('variables');
170
171
return $this->collection(
172
$model->getRelation('variables'),
173
$this->makeTransformer(EggVariableTransformer::class),
174
EggVariable::RESOURCE_NAME
175
);
176
}
177
}
178
179