Path: blob/1.0-develop/app/Transformers/Api/Application/NodeTransformer.php
10280 views
<?php12namespace Pterodactyl\Transformers\Api\Application;34use Pterodactyl\Models\Node;5use League\Fractal\Resource\Item;6use League\Fractal\Resource\Collection;7use League\Fractal\Resource\NullResource;8use Pterodactyl\Services\Acl\Api\AdminAcl;910class NodeTransformer extends BaseTransformer11{12/**13* List of resources that can be included.14*/15protected array $availableIncludes = ['allocations', 'location', 'servers'];1617/**18* Return the resource name for the JSONAPI output.19*/20public function getResourceName(): string21{22return Node::RESOURCE_NAME;23}2425/**26* Return a node transformed into a format that can be consumed by the27* external administrative API.28*/29public function transform(Node $node): array30{31$response = collect($node->toArray())->mapWithKeys(function ($value, $key) {32// I messed up early in 2016 when I named this column as poorly33// as I did. This is the tragic result of my mistakes.34$key = ($key === 'daemonSFTP') ? 'daemonSftp' : $key;3536return [snake_case($key) => $value];37})->toArray();3839$response[$node->getUpdatedAtColumn()] = $this->formatTimestamp($node->updated_at);40$response[$node->getCreatedAtColumn()] = $this->formatTimestamp($node->created_at);4142$resources = $node->servers()->select(['memory', 'disk'])->get();4344$response['allocated_resources'] = [45'memory' => $resources->sum('memory'),46'disk' => $resources->sum('disk'),47];4849return $response;50}5152/**53* Return the nodes associated with this location.54*55* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException56*/57public function includeAllocations(Node $node): Collection|NullResource58{59if (!$this->authorize(AdminAcl::RESOURCE_ALLOCATIONS)) {60return $this->null();61}6263$node->loadMissing('allocations');6465return $this->collection(66$node->getRelation('allocations'),67$this->makeTransformer(AllocationTransformer::class),68'allocation'69);70}7172/**73* Return the nodes associated with this location.74*75* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException76*/77public function includeLocation(Node $node): Item|NullResource78{79if (!$this->authorize(AdminAcl::RESOURCE_LOCATIONS)) {80return $this->null();81}8283$node->loadMissing('location');8485return $this->item(86$node->getRelation('location'),87$this->makeTransformer(LocationTransformer::class),88'location'89);90}9192/**93* Return the nodes associated with this location.94*95* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException96*/97public function includeServers(Node $node): Collection|NullResource98{99if (!$this->authorize(AdminAcl::RESOURCE_SERVERS)) {100return $this->null();101}102103$node->loadMissing('servers');104105return $this->collection(106$node->getRelation('servers'),107$this->makeTransformer(ServerTransformer::class),108'server'109);110}111}112113114