Path: blob/1.0-develop/app/Transformers/Api/Application/EggTransformer.php
10279 views
<?php12namespace Pterodactyl\Transformers\Api\Application;34use Illuminate\Support\Arr;5use Pterodactyl\Models\Egg;6use Pterodactyl\Models\Nest;7use Pterodactyl\Models\Server;8use League\Fractal\Resource\Item;9use Pterodactyl\Models\EggVariable;10use League\Fractal\Resource\Collection;11use League\Fractal\Resource\NullResource;12use Pterodactyl\Services\Acl\Api\AdminAcl;1314class EggTransformer extends BaseTransformer15{16/**17* Relationships that can be loaded onto this transformation.18*/19protected array $availableIncludes = [20'nest',21'servers',22'config',23'script',24'variables',25];2627/**28* Return the resource name for the JSONAPI output.29*/30public function getResourceName(): string31{32return Egg::RESOURCE_NAME;33}3435/**36* Transform an Egg model into a representation that can be consumed by37* the application api.38*39* @throws \JsonException40*/41public function transform(Egg $model): array42{43$files = json_decode($model->config_files, true, 512, JSON_THROW_ON_ERROR);44if (empty($files)) {45$files = new \stdClass();46}4748return [49'id' => $model->id,50'uuid' => $model->uuid,51'name' => $model->name,52'nest' => $model->nest_id,53'author' => $model->author,54'description' => $model->description,55// "docker_image" is deprecated, but left here to avoid breaking too many things at once56// in external software. We'll remove it down the road once things have gotten the chance57// to upgrade to using "docker_images".58'docker_image' => count($model->docker_images) > 0 ? Arr::first($model->docker_images) : '',59'docker_images' => $model->docker_images,60'config' => [61'files' => $files,62'startup' => json_decode($model->config_startup, true),63'stop' => $model->config_stop,64'logs' => json_decode($model->config_logs, true),65'file_denylist' => $model->file_denylist,66'extends' => $model->config_from,67],68'startup' => $model->startup,69'script' => [70'privileged' => $model->script_is_privileged,71'install' => $model->script_install,72'entry' => $model->script_entry,73'container' => $model->script_container,74'extends' => $model->copy_script_from,75],76$model->getCreatedAtColumn() => $this->formatTimestamp($model->created_at),77$model->getUpdatedAtColumn() => $this->formatTimestamp($model->updated_at),78];79}8081/**82* Include the Nest relationship for the given Egg in the transformation.83*84* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException85*/86public function includeNest(Egg $model): Item|NullResource87{88if (!$this->authorize(AdminAcl::RESOURCE_NESTS)) {89return $this->null();90}9192$model->loadMissing('nest');9394return $this->item($model->getRelation('nest'), $this->makeTransformer(NestTransformer::class), Nest::RESOURCE_NAME);95}9697/**98* Include the Servers relationship for the given Egg in the transformation.99*100* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException101*/102public function includeServers(Egg $model): Collection|NullResource103{104if (!$this->authorize(AdminAcl::RESOURCE_SERVERS)) {105return $this->null();106}107108$model->loadMissing('servers');109110return $this->collection($model->getRelation('servers'), $this->makeTransformer(ServerTransformer::class), Server::RESOURCE_NAME);111}112113/**114* Include more detailed information about the configuration if this Egg is115* extending another.116*/117public function includeConfig(Egg $model): Item|NullResource118{119if (is_null($model->config_from)) {120return $this->null();121}122123$model->loadMissing('configFrom');124125return $this->item($model, function (Egg $model) {126return [127'files' => json_decode($model->inherit_config_files),128'startup' => json_decode($model->inherit_config_startup),129'stop' => $model->inherit_config_stop,130'logs' => json_decode($model->inherit_config_logs),131];132});133}134135/**136* Include more detailed information about the script configuration if the137* Egg is extending another.138*/139public function includeScript(Egg $model): Item|NullResource140{141if (is_null($model->copy_script_from)) {142return $this->null();143}144145$model->loadMissing('scriptFrom');146147return $this->item($model, function (Egg $model) {148return [149'privileged' => $model->script_is_privileged,150'install' => $model->copy_script_install,151'entry' => $model->copy_script_entry,152'container' => $model->copy_script_container,153];154});155}156157/**158* Include the variables that are defined for this Egg.159*160* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException161*/162public function includeVariables(Egg $model): Collection|NullResource163{164if (!$this->authorize(AdminAcl::RESOURCE_EGGS)) {165return $this->null();166}167168$model->loadMissing('variables');169170return $this->collection(171$model->getRelation('variables'),172$this->makeTransformer(EggVariableTransformer::class),173EggVariable::RESOURCE_NAME174);175}176}177178179