Skip to content

Commit 51204b8

Browse files
committed
Add all of the potential transformers that might be needed for now.
1 parent 65630bd commit 51204b8

12 files changed

+642
-9
lines changed

app/Http/Controllers/API/Admin/ServerController.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ public function view(Request $request, $id)
6464
$fractal = Fractal::create()->item($server);
6565

6666
if ($request->input('include')) {
67-
$fractal->parseIncludes(collect(explode(',', $request->input('include')))->intersect([
68-
'allocations', 'subusers', 'user',
69-
'pack', 'service', 'option',
70-
])->toArray());
67+
$fractal->parseIncludes(explode(',', $request->input('include')));
7168
}
7269

7370
return $fractal->transformWith(new ServerTransformer)

app/Http/Controllers/API/User/ServerController.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ public function index(Request $request, $uuid)
4747
$fractal = Fractal::create()->item($server);
4848

4949
if ($request->input('include')) {
50-
$fractal->parseIncludes(collect(explode(',', $request->input('include')))->intersect([
51-
'allocations', 'subusers', 'stats',
52-
])->toArray());
50+
$fractal->parseIncludes(explode(',', $request->input('include')));
5351
}
5452

5553
return $fractal->transformWith(new ServerTransformer)

app/Models/Server.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,4 +313,14 @@ public function databases()
313313
{
314314
return $this->hasMany(Database::class);
315315
}
316+
317+
/**
318+
* Gets the location of the server.
319+
*
320+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
321+
*/
322+
public function location()
323+
{
324+
return $this->node->location();
325+
}
316326
}

app/Transformers/Admin/AllocationTransformer.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,55 @@
3030
class AllocationTransformer extends TransformerAbstract
3131
{
3232
/**
33-
* Return a generic transformed server array.
33+
* The filter to be applied to this transformer.
34+
*
35+
* @var bool|string
36+
*/
37+
protected $filter;
38+
39+
/**
40+
* Transformer constructor.
41+
*
42+
* @param bool|string $filter
43+
* @return void
44+
*/
45+
public function __construct($filter = false)
46+
{
47+
$this->filter = $filter;
48+
}
49+
50+
/**
51+
* Return a generic transformed allocation array.
3452
*
3553
* @return array
3654
*/
3755
public function transform(Allocation $allocation)
3856
{
57+
return $this->transformWithFilter($allocation);
58+
}
59+
60+
/**
61+
* Determine which transformer filter to apply.
62+
*
63+
* @return array
64+
*/
65+
protected function transformWithFilter(Allocation $allocation)
66+
{
67+
if ($this->filter === 'server') {
68+
return $this->transformForServer($allocation);
69+
}
70+
3971
return $allocation->toArray();
4072
}
73+
74+
/**
75+
* Transform the allocation to only return information not duplicated
76+
* in the server response (discard node_id and server_id).
77+
*
78+
* @return array
79+
*/
80+
protected function transformForServer(Allocation $allocation)
81+
{
82+
return collect($allocation)->only('id', 'ip', 'ip_alias', 'port')->toArray();
83+
}
4184
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Pterodactyl\Transformers\Admin;
26+
27+
use Pterodactyl\Models\Location;
28+
use League\Fractal\TransformerAbstract;
29+
30+
class LocationTransformer extends TransformerAbstract
31+
{
32+
/**
33+
* List of resources that can be included.
34+
*
35+
* @var array
36+
*/
37+
protected $availableIncludes = [
38+
'nodes',
39+
'servers',
40+
];
41+
42+
/**
43+
* Return a generic transformed pack array.
44+
*
45+
* @return array
46+
*/
47+
public function transform(Location $location)
48+
{
49+
return $location->toArray();
50+
}
51+
52+
/**
53+
* Return the nodes associated with this location.
54+
*
55+
* @return \Leauge\Fractal\Resource\Collection
56+
*/
57+
public function includeServers(Location $location)
58+
{
59+
return $this->collection($location->servers, new ServerTransformer, 'server');
60+
}
61+
62+
/**
63+
* Return the nodes associated with this location.
64+
*
65+
* @return \Leauge\Fractal\Resource\Collection
66+
*/
67+
public function includeNodes(Location $location)
68+
{
69+
return $this->collection($location->nodes, new NodeTransformer, 'node');
70+
}
71+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Pterodactyl\Transformers\Admin;
26+
27+
use Pterodactyl\Models\Node;
28+
use League\Fractal\TransformerAbstract;
29+
30+
class NodeTransformer extends TransformerAbstract
31+
{
32+
/**
33+
* List of resources that can be included.
34+
*
35+
* @var array
36+
*/
37+
protected $availableIncludes = [
38+
'allocations',
39+
'location',
40+
'servers',
41+
];
42+
43+
/**
44+
* Return a generic transformed pack array.
45+
*
46+
* @return array
47+
*/
48+
public function transform(Node $node)
49+
{
50+
return $node->toArray();
51+
}
52+
53+
/**
54+
* Return the nodes associated with this location.
55+
*
56+
* @return \Leauge\Fractal\Resource\Collection
57+
*/
58+
public function includeAllocations(Node $node)
59+
{
60+
return $this->collection($node->allocations, new AllocationTransformer, 'allocation');
61+
}
62+
63+
/**
64+
* Return the nodes associated with this location.
65+
*
66+
* @return \Leauge\Fractal\Resource\Item
67+
*/
68+
public function includeLocation(Node $node)
69+
{
70+
return $this->item($node->location, new LocationTransformer, 'location');
71+
}
72+
73+
/**
74+
* Return the nodes associated with this location.
75+
*
76+
* @return \Leauge\Fractal\Resource\Collection
77+
*/
78+
public function includeServers(Node $node)
79+
{
80+
return $this->collection($node->servers, new ServerTransformer, 'server');
81+
}
82+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Pterodactyl\Transformers\Admin;
26+
27+
use Pterodactyl\Models\ServiceOption;
28+
use League\Fractal\TransformerAbstract;
29+
30+
class OptionTransformer extends TransformerAbstract
31+
{
32+
/**
33+
* List of resources that can be included.
34+
*
35+
* @var array
36+
*/
37+
protected $availableIncludes = [
38+
'service',
39+
'packs',
40+
'servers',
41+
'variables',
42+
];
43+
44+
/**
45+
* Return a generic transformed service option array.
46+
*
47+
* @return array
48+
*/
49+
public function transform(ServiceOption $option)
50+
{
51+
return $option->toArray();
52+
}
53+
54+
/**
55+
* Return the parent service for this service option.
56+
*
57+
* @return \Leauge\Fractal\Resource\Collection
58+
*/
59+
public function includeService(ServiceOption $option)
60+
{
61+
return $this->item($option->service, new ServiceTransformer, 'service');
62+
}
63+
64+
/**
65+
* Return the packs associated with this service option.
66+
*
67+
* @return \Leauge\Fractal\Resource\Collection
68+
*/
69+
public function includePacks(ServiceOption $option)
70+
{
71+
return $this->collection($option->packs, new PackTransformer, 'pack');
72+
}
73+
74+
/**
75+
* Return the servers associated with this service option.
76+
*
77+
* @return \Leauge\Fractal\Resource\Collection
78+
*/
79+
public function includeServers(ServiceOption $option)
80+
{
81+
return $this->collection($option->servers, new ServerTransformer, 'server');
82+
}
83+
84+
/**
85+
* Return the variables for this service option.
86+
*
87+
* @return \Leauge\Fractal\Resource\Collection
88+
*/
89+
public function includeVariables(ServiceOption $option)
90+
{
91+
return $this->collection($option->variables, new ServiceVariableTransformer, 'variable');
92+
}
93+
}

0 commit comments

Comments
 (0)