2018-01-27 18:38:56 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Transformers\Api\Application;
|
|
|
|
|
2022-05-14 20:03:50 +00:00
|
|
|
use Pterodactyl\Models\Egg;
|
2022-10-14 16:59:20 +00:00
|
|
|
use League\Fractal\Resource\Item;
|
|
|
|
use League\Fractal\Resource\Collection;
|
|
|
|
use League\Fractal\Resource\NullResource;
|
2018-01-27 18:38:56 +00:00
|
|
|
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
2022-12-15 00:05:46 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Transformer;
|
2018-01-27 18:38:56 +00:00
|
|
|
|
2022-12-15 00:05:46 +00:00
|
|
|
class EggTransformer extends Transformer
|
2018-01-27 18:38:56 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Relationships that can be loaded onto this transformation.
|
|
|
|
*/
|
2022-05-04 23:11:42 +00:00
|
|
|
protected array $availableIncludes = [
|
2021-01-27 05:08:53 +00:00
|
|
|
'config',
|
2022-12-15 00:05:46 +00:00
|
|
|
'nest',
|
2021-01-27 05:08:53 +00:00
|
|
|
'script',
|
2022-12-15 00:05:46 +00:00
|
|
|
'servers',
|
2021-01-27 05:08:53 +00:00
|
|
|
'variables',
|
2018-01-27 18:38:56 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the resource name for the JSONAPI output.
|
|
|
|
*/
|
|
|
|
public function getResourceName(): string
|
|
|
|
{
|
|
|
|
return Egg::RESOURCE_NAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transform an Egg model into a representation that can be consumed by
|
|
|
|
* the application api.
|
|
|
|
*
|
2022-10-14 16:59:20 +00:00
|
|
|
* @throws \JsonException
|
2018-01-27 18:38:56 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function transform(Egg $model): array
|
2018-01-27 18:38:56 +00:00
|
|
|
{
|
2022-09-17 18:36:41 +00:00
|
|
|
$files = json_decode($model->config_files, true, 512, JSON_THROW_ON_ERROR);
|
|
|
|
if (empty($files)) {
|
|
|
|
$files = new \stdClass();
|
|
|
|
}
|
|
|
|
|
2018-01-27 18:38:56 +00:00
|
|
|
return [
|
|
|
|
'id' => $model->id,
|
|
|
|
'uuid' => $model->uuid,
|
2019-12-28 19:12:01 +00:00
|
|
|
'name' => $model->name,
|
2022-12-15 00:05:46 +00:00
|
|
|
'nest_id' => $model->nest_id,
|
2018-01-27 18:38:56 +00:00
|
|
|
'author' => $model->author,
|
|
|
|
'description' => $model->description,
|
2020-12-13 17:53:17 +00:00
|
|
|
'docker_images' => $model->docker_images,
|
2018-01-27 18:38:56 +00:00
|
|
|
'config' => [
|
2022-09-17 18:36:41 +00:00
|
|
|
'files' => $files,
|
2018-03-25 22:41:36 +00:00
|
|
|
'startup' => json_decode($model->config_startup, true),
|
2018-01-27 18:38:56 +00:00
|
|
|
'stop' => $model->config_stop,
|
2021-01-27 05:08:53 +00:00
|
|
|
'file_denylist' => $model->file_denylist,
|
2018-01-27 18:38:56 +00:00
|
|
|
'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,
|
|
|
|
],
|
2022-12-15 00:05:46 +00:00
|
|
|
'created_at' => self::formatTimestamp($model->created_at),
|
|
|
|
'updated_at' => self::formatTimestamp($model->updated_at),
|
2018-01-27 18:38:56 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Include more detailed information about the configuration if this Egg is
|
|
|
|
* extending another.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function includeConfig(Egg $model): Item|NullResource
|
2018-01-27 18:38:56 +00:00
|
|
|
{
|
|
|
|
if (is_null($model->config_from)) {
|
|
|
|
return $this->null();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->item($model, function (Egg $model) {
|
|
|
|
return [
|
|
|
|
'files' => json_decode($model->inherit_config_files),
|
|
|
|
'startup' => json_decode($model->inherit_config_startup),
|
|
|
|
'stop' => $model->inherit_config_stop,
|
|
|
|
];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-15 00:05:46 +00:00
|
|
|
/**
|
|
|
|
* Include the Nest relationship for the given Egg in the transformation.
|
|
|
|
*/
|
|
|
|
public function includeNest(Egg $model): Item|NullResource
|
|
|
|
{
|
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_NESTS)) {
|
|
|
|
return $this->null();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->item($model->nest, new NestTransformer());
|
|
|
|
}
|
|
|
|
|
2018-01-27 18:38:56 +00:00
|
|
|
/**
|
|
|
|
* Include more detailed information about the script configuration if the
|
|
|
|
* Egg is extending another.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function includeScript(Egg $model): Item|NullResource
|
2018-01-27 18:38:56 +00:00
|
|
|
{
|
|
|
|
if (is_null($model->copy_script_from)) {
|
|
|
|
return $this->null();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->item($model, function (Egg $model) {
|
|
|
|
return [
|
|
|
|
'privileged' => $model->script_is_privileged,
|
|
|
|
'install' => $model->copy_script_install,
|
|
|
|
'entry' => $model->copy_script_entry,
|
|
|
|
'container' => $model->copy_script_container,
|
|
|
|
];
|
|
|
|
});
|
|
|
|
}
|
2018-02-24 17:17:21 +00:00
|
|
|
|
2022-12-15 00:05:46 +00:00
|
|
|
/**
|
|
|
|
* Include the Servers relationship for the given Egg in the transformation.
|
|
|
|
*/
|
|
|
|
public function includeServers(Egg $model): Collection|NullResource
|
|
|
|
{
|
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_SERVERS)) {
|
|
|
|
return $this->null();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->collection($model->servers, new ServerTransformer());
|
|
|
|
}
|
|
|
|
|
2018-02-24 17:17:21 +00:00
|
|
|
/**
|
|
|
|
* Include the variables that are defined for this Egg.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function includeVariables(Egg $model): Collection|NullResource
|
2018-02-24 17:17:21 +00:00
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_EGGS)) {
|
2018-02-24 17:17:21 +00:00
|
|
|
return $this->null();
|
|
|
|
}
|
|
|
|
|
|
|
|
$model->loadMissing('variables');
|
|
|
|
|
2022-12-15 00:05:46 +00:00
|
|
|
return $this->collection($model->variables, new EggVariableTransformer());
|
2018-02-24 17:17:21 +00:00
|
|
|
}
|
2018-01-27 18:38:56 +00:00
|
|
|
}
|