Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Transformers/Api/Application/NodeTransformer.php
10280 views
1
<?php
2
3
namespace Pterodactyl\Transformers\Api\Application;
4
5
use Pterodactyl\Models\Node;
6
use League\Fractal\Resource\Item;
7
use League\Fractal\Resource\Collection;
8
use League\Fractal\Resource\NullResource;
9
use Pterodactyl\Services\Acl\Api\AdminAcl;
10
11
class NodeTransformer extends BaseTransformer
12
{
13
/**
14
* List of resources that can be included.
15
*/
16
protected array $availableIncludes = ['allocations', 'location', 'servers'];
17
18
/**
19
* Return the resource name for the JSONAPI output.
20
*/
21
public function getResourceName(): string
22
{
23
return Node::RESOURCE_NAME;
24
}
25
26
/**
27
* Return a node transformed into a format that can be consumed by the
28
* external administrative API.
29
*/
30
public function transform(Node $node): array
31
{
32
$response = collect($node->toArray())->mapWithKeys(function ($value, $key) {
33
// I messed up early in 2016 when I named this column as poorly
34
// as I did. This is the tragic result of my mistakes.
35
$key = ($key === 'daemonSFTP') ? 'daemonSftp' : $key;
36
37
return [snake_case($key) => $value];
38
})->toArray();
39
40
$response[$node->getUpdatedAtColumn()] = $this->formatTimestamp($node->updated_at);
41
$response[$node->getCreatedAtColumn()] = $this->formatTimestamp($node->created_at);
42
43
$resources = $node->servers()->select(['memory', 'disk'])->get();
44
45
$response['allocated_resources'] = [
46
'memory' => $resources->sum('memory'),
47
'disk' => $resources->sum('disk'),
48
];
49
50
return $response;
51
}
52
53
/**
54
* Return the nodes associated with this location.
55
*
56
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
57
*/
58
public function includeAllocations(Node $node): Collection|NullResource
59
{
60
if (!$this->authorize(AdminAcl::RESOURCE_ALLOCATIONS)) {
61
return $this->null();
62
}
63
64
$node->loadMissing('allocations');
65
66
return $this->collection(
67
$node->getRelation('allocations'),
68
$this->makeTransformer(AllocationTransformer::class),
69
'allocation'
70
);
71
}
72
73
/**
74
* Return the nodes associated with this location.
75
*
76
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
77
*/
78
public function includeLocation(Node $node): Item|NullResource
79
{
80
if (!$this->authorize(AdminAcl::RESOURCE_LOCATIONS)) {
81
return $this->null();
82
}
83
84
$node->loadMissing('location');
85
86
return $this->item(
87
$node->getRelation('location'),
88
$this->makeTransformer(LocationTransformer::class),
89
'location'
90
);
91
}
92
93
/**
94
* Return the nodes associated with this location.
95
*
96
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
97
*/
98
public function includeServers(Node $node): Collection|NullResource
99
{
100
if (!$this->authorize(AdminAcl::RESOURCE_SERVERS)) {
101
return $this->null();
102
}
103
104
$node->loadMissing('servers');
105
106
return $this->collection(
107
$node->getRelation('servers'),
108
$this->makeTransformer(ServerTransformer::class),
109
'server'
110
);
111
}
112
}
113
114