mirror of
https://github.com/pterodactyl/panel.git
synced 2026-12-05 22:05:47 -05:00
Co-authored-by: Anthony <anthony@infraly.co> Co-authored-by: Sky Mulley <daniel.mulley@outlook.com>
124 lines
5.3 KiB
PHP
124 lines
5.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Pterodactyl\Transformers\Api\Admin;
|
|
|
|
use Illuminate\Support\Arr;
|
|
use League\Fractal\Resource\Collection;
|
|
use League\Fractal\Resource\NullResource;
|
|
use Pterodactyl\Extensions\Scribe\Attributes\ResponseField;
|
|
use Pterodactyl\Models\Egg;
|
|
use Pterodactyl\Models\EggVariable;
|
|
use Pterodactyl\Models\Server;
|
|
use Pterodactyl\Models\Tag;
|
|
use Pterodactyl\Support\JsonEmptyObject;
|
|
use Pterodactyl\Support\JsonValueGuard;
|
|
|
|
#[ResponseField('description', nullable: true)]
|
|
#[ResponseField('features', nullable: true)]
|
|
#[ResponseField('docker_images', schema: ['type' => 'object', 'additionalProperties' => ['type' => 'string'], 'example' => ['Java 23' => 'ghcr.io/pterodactyl/yolks:java_23']])]
|
|
#[ResponseField('config.files', schema: ['type' => 'object', 'additionalProperties' => ['type' => 'object', 'properties' => ['parser' => ['type' => 'string'], 'find' => ['type' => 'object', 'additionalProperties' => ['oneOf' => [['type' => 'string'], ['type' => 'number'], ['type' => 'boolean'], ['type' => 'object', 'additionalProperties' => ['type' => 'string']]]]]]], 'example' => ['server.properties' => ['parser' => 'properties', 'find' => ['server-port' => '{{server.build.default.port}}']]]])]
|
|
#[ResponseField('config.logs', schema: ['type' => 'object', 'additionalProperties' => ['oneOf' => [['type' => 'string'], ['type' => 'number'], ['type' => 'boolean']]], 'example' => ['custom' => true, 'location' => 'logs/latest.log']])]
|
|
#[ResponseField('config.startup', schema: ['type' => 'object', 'additionalProperties' => ['oneOf' => [['type' => 'string'], ['type' => 'array', 'items' => ['type' => 'string']]]], 'example' => ['done' => ['Done']]])]
|
|
#[ResponseField('config.file_denylist', schema: ['type' => 'array', 'nullable' => true, 'items' => ['type' => 'string'], 'example' => ['secret.txt']])]
|
|
#[ResponseField('force_outgoing_ip', 'boolean', nullable: true)]
|
|
#[ResponseField('script.privileged', 'boolean', nullable: true)]
|
|
class EggTransformer extends BaseAdminTransformer
|
|
{
|
|
protected array $includeRelations = [
|
|
'variables' => ['relation' => 'variables', 'transformer' => EggVariableTransformer::class],
|
|
'tags' => ['relation' => 'tags', 'transformer' => TagTransformer::class],
|
|
'servers' => ['relation' => 'servers', 'transformer' => ServerTransformer::class],
|
|
];
|
|
|
|
/**
|
|
* @var list<string>
|
|
*/
|
|
protected array $availableIncludes = ['variables', 'tags', 'servers'];
|
|
|
|
public function getResourceName(): string
|
|
{
|
|
return Egg::RESOURCE_NAME;
|
|
}
|
|
|
|
/**
|
|
* @return ApiPayload
|
|
*/
|
|
public function transform(Egg $model): array
|
|
{
|
|
$files = json_decode($model->config_files ?? 'null', true, 512, JSON_THROW_ON_ERROR);
|
|
if (empty($files)) {
|
|
$files = new JsonEmptyObject;
|
|
}
|
|
|
|
$payload = [
|
|
'id' => $model->id,
|
|
'uuid' => $model->uuid,
|
|
'author' => $model->author,
|
|
'name' => $model->name,
|
|
'description' => $model->description,
|
|
'features' => $model->features,
|
|
// "docker_image" is deprecated in favor of "docker_images"; kept for external compatibility.
|
|
'docker_image' => count($model->docker_images) > 0 ? Arr::first($model->docker_images) : '',
|
|
'docker_images' => $model->docker_images,
|
|
'force_outgoing_ip' => $model->force_outgoing_ip,
|
|
'config' => [
|
|
'files' => $files,
|
|
'startup' => json_decode($model->config_startup ?? 'null', true),
|
|
'stop' => $model->config_stop,
|
|
'logs' => json_decode($model->config_logs ?? 'null', true),
|
|
'file_denylist' => $model->file_denylist,
|
|
'extends' => $model->config_from,
|
|
],
|
|
'startup' => $model->startup,
|
|
'script' => [
|
|
'privileged' => $model->script_is_privileged,
|
|
'install' => $model->script_install,
|
|
'entry' => $model->script_entry,
|
|
'container' => $model->script_container,
|
|
'extends' => $model->copy_script_from,
|
|
],
|
|
'relationships' => [],
|
|
$model->getCreatedAtColumn() => $this->formatTimestamp($model->created_at),
|
|
$model->getUpdatedAtColumn() => $this->formatTimestamp($model->updated_at),
|
|
];
|
|
JsonValueGuard::assertPayload($payload);
|
|
|
|
return $payload;
|
|
}
|
|
|
|
public function includeVariables(Egg $model): Collection|NullResource
|
|
{
|
|
$model->loadMissing('variables');
|
|
|
|
return $this->collection(
|
|
$model->getRelation('variables'),
|
|
$this->makeTransformer(EggVariableTransformer::class),
|
|
EggVariable::RESOURCE_NAME
|
|
);
|
|
}
|
|
|
|
public function includeTags(Egg $model): Collection|NullResource
|
|
{
|
|
$model->loadMissing('tags');
|
|
|
|
return $this->collection(
|
|
$model->getRelation('tags'),
|
|
$this->makeTransformer(TagTransformer::class),
|
|
Tag::RESOURCE_NAME
|
|
);
|
|
}
|
|
|
|
public function includeServers(Egg $model): Collection|NullResource
|
|
{
|
|
$model->loadMissing('servers');
|
|
|
|
return $this->collection(
|
|
$model->getRelation('servers'),
|
|
$this->makeTransformer(ServerTransformer::class),
|
|
Server::RESOURCE_NAME
|
|
);
|
|
}
|
|
}
|