Skip to content

Commit 8afced3

Browse files
committed
Add nests & eggs
Cleanup middleware handling and parameters on controllers...
1 parent de07b3c commit 8afced3

26 files changed

+737
-446
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace Pterodactyl\Extensions\League\Fractal\Serializers;
4+
5+
use League\Fractal\Serializer\ArraySerializer;
6+
7+
class PterodactylSerializer extends ArraySerializer
8+
{
9+
/**
10+
* Serialize an item.
11+
*
12+
* @param string $resourceKey
13+
* @param array $data
14+
* @return array
15+
*/
16+
public function item($resourceKey, array $data)
17+
{
18+
return [
19+
'object' => $resourceKey,
20+
'attributes' => $data,
21+
];
22+
}
23+
24+
/**
25+
* Serialize a collection.
26+
*
27+
* @param string $resourceKey
28+
* @param array $data
29+
* @return array
30+
*/
31+
public function collection($resourceKey, array $data)
32+
{
33+
$response = [];
34+
foreach ($data as $datum) {
35+
$response[] = $this->item($resourceKey, $datum);
36+
}
37+
38+
return [
39+
'object' => 'list',
40+
'data' => $response,
41+
];
42+
}
43+
44+
/**
45+
* Serialize a null resource.
46+
*
47+
* @return array
48+
*/
49+
public function null()
50+
{
51+
return [
52+
'object' => 'null_resource',
53+
'attributes' => null,
54+
];
55+
}
56+
57+
/**
58+
* Merge the included resources with the parent resource being serialized.
59+
*
60+
* @param array $transformedData
61+
* @param array $includedData
62+
* @return array
63+
*/
64+
public function mergeIncludes($transformedData, $includedData)
65+
{
66+
foreach ($includedData as $key => $datum) {
67+
$transformedData['relationships'][$key] = $datum;
68+
}
69+
70+
return $transformedData;
71+
}
72+
}

app/Extensions/Spatie/Fractalistic/Fractal.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
use League\Fractal\TransformerAbstract;
66
use Spatie\Fractal\Fractal as SpatieFractal;
7-
use League\Fractal\Serializer\JsonApiSerializer;
87
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
98
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
9+
use Pterodactyl\Extensions\League\Fractal\Serializers\PterodactylSerializer;
1010

1111
class Fractal extends SpatieFractal
1212
{
@@ -22,7 +22,7 @@ public function createData()
2222
{
2323
// Set the serializer by default.
2424
if (is_null($this->serializer)) {
25-
$this->serializer = new JsonApiSerializer;
25+
$this->serializer = new PterodactylSerializer;
2626
}
2727

2828
// Automatically set the paginator on the response object if the

app/Http/Controllers/Api/Application/Locations/LocationController.php

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,20 @@
22

33
namespace Pterodactyl\Http\Controllers\Api\Application\Locations;
44

5-
use Spatie\Fractal\Fractal;
65
use Illuminate\Http\Response;
76
use Pterodactyl\Models\Location;
87
use Illuminate\Http\JsonResponse;
9-
use Pterodactyl\Http\Controllers\Controller;
10-
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
118
use Pterodactyl\Services\Locations\LocationUpdateService;
129
use Pterodactyl\Services\Locations\LocationCreationService;
1310
use Pterodactyl\Services\Locations\LocationDeletionService;
1411
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
1512
use Pterodactyl\Transformers\Api\Application\LocationTransformer;
13+
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
1614
use Pterodactyl\Http\Requests\Api\Application\Locations\GetLocationsRequest;
1715
use Pterodactyl\Http\Requests\Api\Application\Locations\DeleteLocationRequest;
1816
use Pterodactyl\Http\Requests\Api\Application\Locations\UpdateLocationRequest;
1917

20-
class LocationController extends Controller
18+
class LocationController extends ApplicationApiController
2119
{
2220
/**
2321
* @var \Pterodactyl\Services\Locations\LocationCreationService
@@ -29,11 +27,6 @@ class LocationController extends Controller
2927
*/
3028
private $deletionService;
3129

32-
/**
33-
* @var \Spatie\Fractal\Fractal
34-
*/
35-
private $fractal;
36-
3730
/**
3831
* @var \Pterodactyl\Contracts\Repository\LocationRepositoryInterface
3932
*/
@@ -47,22 +40,21 @@ class LocationController extends Controller
4740
/**
4841
* LocationController constructor.
4942
*
50-
* @param \Spatie\Fractal\Fractal $fractal
5143
* @param \Pterodactyl\Services\Locations\LocationCreationService $creationService
5244
* @param \Pterodactyl\Services\Locations\LocationDeletionService $deletionService
5345
* @param \Pterodactyl\Contracts\Repository\LocationRepositoryInterface $repository
5446
* @param \Pterodactyl\Services\Locations\LocationUpdateService $updateService
5547
*/
5648
public function __construct(
57-
Fractal $fractal,
5849
LocationCreationService $creationService,
5950
LocationDeletionService $deletionService,
6051
LocationRepositoryInterface $repository,
6152
LocationUpdateService $updateService
6253
) {
54+
parent::__construct();
55+
6356
$this->creationService = $creationService;
6457
$this->deletionService = $deletionService;
65-
$this->fractal = $fractal;
6658
$this->repository = $repository;
6759
$this->updateService = $updateService;
6860
}
@@ -78,24 +70,20 @@ public function index(GetLocationsRequest $request): array
7870
$locations = $this->repository->paginated(100);
7971

8072
return $this->fractal->collection($locations)
81-
->transformWith((new LocationTransformer)->setKey($request->key()))
82-
->withResourceName('location')
83-
->paginateWith(new IlluminatePaginatorAdapter($locations))
73+
->transformWith($this->getTransformer(LocationTransformer::class))
8474
->toArray();
8575
}
8676

8777
/**
8878
* Return a single location.
8979
*
9080
* @param \Pterodactyl\Http\Controllers\Api\Application\Locations\GetLocationRequest $request
91-
* @param \Pterodactyl\Models\Location $location
9281
* @return array
9382
*/
94-
public function view(GetLocationRequest $request, Location $location): array
83+
public function view(GetLocationRequest $request): array
9584
{
96-
return $this->fractal->item($location)
97-
->transformWith((new LocationTransformer)->setKey($request->key()))
98-
->withResourceName('location')
85+
return $this->fractal->item($request->getModel(Location::class))
86+
->transformWith($this->getTransformer(LocationTransformer::class))
9987
->toArray();
10088
}
10189

@@ -113,43 +101,44 @@ public function store(StoreLocationRequest $request): JsonResponse
113101
$location = $this->creationService->handle($request->validated());
114102

115103
return $this->fractal->item($location)
116-
->transformWith((new LocationTransformer)->setKey($request->key()))
117-
->withResourceName('location')
104+
->transformWith($this->getTransformer(LocationTransformer::class))
105+
->addMeta([
106+
'resource' => route('api.application.locations.view', [
107+
'location' => $location->id,
108+
]),
109+
])
118110
->respond(201);
119111
}
120112

121113
/**
122114
* Update a location on the Panel and return the updated record to the user.
123115
*
124116
* @param \Pterodactyl\Http\Requests\Api\Application\Locations\UpdateLocationRequest $request
125-
* @param \Pterodactyl\Models\Location $location
126117
* @return array
127118
*
128119
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
129120
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
130121
*/
131-
public function update(UpdateLocationRequest $request, Location $location): array
122+
public function update(UpdateLocationRequest $request): array
132123
{
133-
$location = $this->updateService->handle($location, $request->validated());
124+
$location = $this->updateService->handle($request->getModel(Location::class), $request->validated());
134125

135126
return $this->fractal->item($location)
136-
->transformWith((new LocationTransformer)->setKey($request->key()))
137-
->withResourceName('location')
127+
->transformWith($this->getTransformer(LocationTransformer::class))
138128
->toArray();
139129
}
140130

141131
/**
142132
* Delete a location from the Panel.
143133
*
144134
* @param \Pterodactyl\Http\Requests\Api\Application\Locations\DeleteLocationRequest $request
145-
* @param \Pterodactyl\Models\Location $location
146135
* @return \Illuminate\Http\Response
147136
*
148137
* @throws \Pterodactyl\Exceptions\Service\Location\HasActiveNodesException
149138
*/
150-
public function delete(DeleteLocationRequest $request, Location $location): Response
139+
public function delete(DeleteLocationRequest $request): Response
151140
{
152-
$this->deletionService->handle($location);
141+
$this->deletionService->handle($request->getModel(Location::class));
153142

154143
return response('', 204);
155144
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Controllers\Api\Application\Nests;
4+
5+
use Pterodactyl\Models\Egg;
6+
use Pterodactyl\Models\Nest;
7+
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
8+
use Pterodactyl\Transformers\Api\Application\EggTransformer;
9+
use Pterodactyl\Http\Requests\Api\Application\Nests\Eggs\GetEggRequest;
10+
use Pterodactyl\Http\Requests\Api\Application\Nests\Eggs\GetEggsRequest;
11+
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
12+
13+
class EggController extends ApplicationApiController
14+
{
15+
/**
16+
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface
17+
*/
18+
private $repository;
19+
20+
/**
21+
* EggController constructor.
22+
*
23+
* @param \Pterodactyl\Contracts\Repository\EggRepositoryInterface $repository
24+
*/
25+
public function __construct(EggRepositoryInterface $repository)
26+
{
27+
parent::__construct();
28+
29+
$this->repository = $repository;
30+
}
31+
32+
/**
33+
* Return all eggs that exist for a given nest.
34+
*
35+
* @param \Pterodactyl\Http\Requests\Api\Application\Nests\Eggs\GetEggsRequest $request
36+
* @return array
37+
*/
38+
public function index(GetEggsRequest $request): array
39+
{
40+
$eggs = $this->repository->findWhere([
41+
['nest_id', '=', $request->getModel(Nest::class)->id],
42+
]);
43+
44+
return $this->fractal->collection($eggs)
45+
->transformWith($this->getTransformer(EggTransformer::class))
46+
->toArray();
47+
}
48+
49+
/**
50+
* Return a single egg that exists on the specified nest.
51+
*
52+
* @param \Pterodactyl\Http\Requests\Api\Application\Nests\Eggs\GetEggRequest $request
53+
* @return array
54+
*/
55+
public function view(GetEggRequest $request): array
56+
{
57+
return $this->fractal->item($request->getModel(Egg::class))
58+
->transformWith($this->getTransformer(EggTransformer::class))
59+
->toArray();
60+
}
61+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Controllers\Api\Application\Nests;
4+
5+
use Pterodactyl\Models\Nest;
6+
use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
7+
use Pterodactyl\Transformers\Api\Application\NestTransformer;
8+
use Pterodactyl\Http\Requests\Api\Application\Nests\GetNestsRequest;
9+
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
10+
11+
class NestController extends ApplicationApiController
12+
{
13+
/**
14+
* @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface
15+
*/
16+
private $repository;
17+
18+
/**
19+
* NestController constructor.
20+
*
21+
* @param \Pterodactyl\Contracts\Repository\NestRepositoryInterface $repository
22+
*/
23+
public function __construct(NestRepositoryInterface $repository)
24+
{
25+
parent::__construct();
26+
27+
$this->repository = $repository;
28+
}
29+
30+
/**
31+
* Return all Nests that exist on the Panel.
32+
*
33+
* @param \Pterodactyl\Http\Requests\Api\Application\Nests\GetNestsRequest $request
34+
* @return array
35+
*/
36+
public function index(GetNestsRequest $request): array
37+
{
38+
$nests = $this->repository->paginated(50);
39+
40+
return $this->fractal->collection($nests)
41+
->transformWith($this->getTransformer(NestTransformer::class))
42+
->toArray();
43+
}
44+
45+
/**
46+
* Return information about a single Nest model.
47+
*
48+
* @param \Pterodactyl\Http\Requests\Api\Application\Nests\GetNestsRequest $request
49+
* @return array
50+
*/
51+
public function view(GetNestsRequest $request): array
52+
{
53+
return $this->fractal->item($request->getModel(Nest::class))
54+
->transformWith($this->getTransformer(NestTransformer::class))
55+
->toArray();
56+
}
57+
}

0 commit comments

Comments
 (0)