Path: blob/1.0-develop/app/Transformers/Api/Application/ServerTransformer.php
10280 views
<?php12namespace Pterodactyl\Transformers\Api\Application;34use Pterodactyl\Models\Server;5use League\Fractal\Resource\Item;6use League\Fractal\Resource\Collection;7use League\Fractal\Resource\NullResource;8use Pterodactyl\Services\Acl\Api\AdminAcl;9use Pterodactyl\Services\Servers\EnvironmentService;1011class ServerTransformer extends BaseTransformer12{13private EnvironmentService $environmentService;1415/**16* List of resources that can be included.17*/18protected array $availableIncludes = [19'allocations',20'user',21'subusers',22'nest',23'egg',24'variables',25'location',26'node',27'databases',28'transfer',29];3031/**32* Perform dependency injection.33*/34public function handle(EnvironmentService $environmentService)35{36$this->environmentService = $environmentService;37}3839/**40* Return the resource name for the JSONAPI output.41*/42public function getResourceName(): string43{44return Server::RESOURCE_NAME;45}4647/**48* Return a generic transformed server array.49*/50public function transform(Server $server): array51{52return [53'id' => $server->getKey(),54'external_id' => $server->external_id,55'uuid' => $server->uuid,56'identifier' => $server->uuidShort,57'name' => $server->name,58'description' => $server->description,59'status' => $server->status,60// This field is deprecated, please use "status".61'suspended' => $server->isSuspended(),62'limits' => [63'memory' => $server->memory,64'swap' => $server->swap,65'disk' => $server->disk,66'io' => $server->io,67'cpu' => $server->cpu,68'threads' => $server->threads,69'oom_disabled' => $server->oom_disabled,70],71'feature_limits' => [72'databases' => $server->database_limit,73'allocations' => $server->allocation_limit,74'backups' => $server->backup_limit,75],76'user' => $server->owner_id,77'node' => $server->node_id,78'allocation' => $server->allocation_id,79'nest' => $server->nest_id,80'egg' => $server->egg_id,81'container' => [82'startup_command' => $server->startup,83'image' => $server->image,84// This field is deprecated, please use "status".85'installed' => $server->isInstalled() ? 1 : 0,86'environment' => $this->environmentService->handle($server),87],88$server->getUpdatedAtColumn() => $this->formatTimestamp($server->updated_at),89$server->getCreatedAtColumn() => $this->formatTimestamp($server->created_at),90];91}9293/**94* Return a generic array of allocations for this server.95*96* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException97*/98public function includeAllocations(Server $server): Collection|NullResource99{100if (!$this->authorize(AdminAcl::RESOURCE_ALLOCATIONS)) {101return $this->null();102}103104$server->loadMissing('allocations');105106return $this->collection($server->getRelation('allocations'), $this->makeTransformer(AllocationTransformer::class), 'allocation');107}108109/**110* Return a generic array of data about subusers for this server.111*112* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException113*/114public function includeSubusers(Server $server): Collection|NullResource115{116if (!$this->authorize(AdminAcl::RESOURCE_USERS)) {117return $this->null();118}119120$server->loadMissing('subusers');121122return $this->collection($server->getRelation('subusers'), $this->makeTransformer(SubuserTransformer::class), 'subuser');123}124125/**126* Return a generic array of data about subusers for this server.127*128* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException129*/130public function includeUser(Server $server): Item|NullResource131{132if (!$this->authorize(AdminAcl::RESOURCE_USERS)) {133return $this->null();134}135136$server->loadMissing('user');137138return $this->item($server->getRelation('user'), $this->makeTransformer(UserTransformer::class), 'user');139}140141/**142* Return a generic array with nest information for this server.143*144* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException145*/146public function includeNest(Server $server): Item|NullResource147{148if (!$this->authorize(AdminAcl::RESOURCE_NESTS)) {149return $this->null();150}151152$server->loadMissing('nest');153154return $this->item($server->getRelation('nest'), $this->makeTransformer(NestTransformer::class), 'nest');155}156157/**158* Return a generic array with egg information for this server.159*160* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException161*/162public function includeEgg(Server $server): Item|NullResource163{164if (!$this->authorize(AdminAcl::RESOURCE_EGGS)) {165return $this->null();166}167168$server->loadMissing('egg');169170return $this->item($server->getRelation('egg'), $this->makeTransformer(EggTransformer::class), 'egg');171}172173/**174* Return a generic array of data about subusers for this server.175*176* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException177*/178public function includeVariables(Server $server): Collection|NullResource179{180if (!$this->authorize(AdminAcl::RESOURCE_SERVERS)) {181return $this->null();182}183184$server->loadMissing('variables');185186return $this->collection($server->getRelation('variables'), $this->makeTransformer(ServerVariableTransformer::class), 'server_variable');187}188189/**190* Return a generic array with location information for this server.191*192* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException193*/194public function includeLocation(Server $server): Item|NullResource195{196if (!$this->authorize(AdminAcl::RESOURCE_LOCATIONS)) {197return $this->null();198}199200$server->loadMissing('location');201202return $this->item($server->getRelation('location'), $this->makeTransformer(LocationTransformer::class), 'location');203}204205/**206* Return a generic array with node information for this server.207*208* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException209*/210public function includeNode(Server $server): Item|NullResource211{212if (!$this->authorize(AdminAcl::RESOURCE_NODES)) {213return $this->null();214}215216$server->loadMissing('node');217218return $this->item($server->getRelation('node'), $this->makeTransformer(NodeTransformer::class), 'node');219}220221/**222* Return a generic array with database information for this server.223*224* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException225*/226public function includeDatabases(Server $server): Collection|NullResource227{228if (!$this->authorize(AdminAcl::RESOURCE_SERVER_DATABASES)) {229return $this->null();230}231232$server->loadMissing('databases');233234return $this->collection($server->getRelation('databases'), $this->makeTransformer(ServerDatabaseTransformer::class), 'databases');235}236}237238239