Skip to content

Commit e313dff

Browse files
committed
Massively simplify API binding logic
Changes the API internals to use normal Laravel binding which automatically supports nested-models and can determine their relationships. This removes a lot of confusingly complex internal logic and replaces it with standard Laravel code. This also removes a deprecated "getModel" method and fully replaces it with a "parameter" method that does stricter type-checking.
1 parent f1235c7 commit e313dff

File tree

53 files changed

+288
-602
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+288
-602
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ public function index(GetLocationsRequest $request): array
7575
/**
7676
* Return a single location.
7777
*/
78-
public function view(GetLocationRequest $request): array
78+
public function view(GetLocationRequest $request, Location $location): array
7979
{
80-
return $this->fractal->item($request->getModel(Location::class))
80+
return $this->fractal->item($location)
8181
->transformWith($this->getTransformer(LocationTransformer::class))
8282
->toArray();
8383
}
@@ -108,9 +108,9 @@ public function store(StoreLocationRequest $request): JsonResponse
108108
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
109109
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
110110
*/
111-
public function update(UpdateLocationRequest $request): array
111+
public function update(UpdateLocationRequest $request, Location $location): array
112112
{
113-
$location = $this->updateService->handle($request->getModel(Location::class), $request->validated());
113+
$location = $this->updateService->handle($location, $request->validated());
114114

115115
return $this->fractal->item($location)
116116
->transformWith($this->getTransformer(LocationTransformer::class))
@@ -122,9 +122,9 @@ public function update(UpdateLocationRequest $request): array
122122
*
123123
* @throws \Pterodactyl\Exceptions\Service\Location\HasActiveNodesException
124124
*/
125-
public function delete(DeleteLocationRequest $request): Response
125+
public function delete(DeleteLocationRequest $request, Location $location): Response
126126
{
127-
$this->deletionService->handle($request->getModel(Location::class));
127+
$this->deletionService->handle($location);
128128

129129
return response('', 204);
130130
}

app/Http/Controllers/Api/Application/Nests/EggController.php

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,29 @@
44

55
use Pterodactyl\Models\Egg;
66
use Pterodactyl\Models\Nest;
7-
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
87
use Pterodactyl\Transformers\Api\Application\EggTransformer;
98
use Pterodactyl\Http\Requests\Api\Application\Nests\Eggs\GetEggRequest;
109
use Pterodactyl\Http\Requests\Api\Application\Nests\Eggs\GetEggsRequest;
1110
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
1211

1312
class EggController extends ApplicationApiController
1413
{
15-
/**
16-
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface
17-
*/
18-
private $repository;
19-
20-
/**
21-
* EggController constructor.
22-
*/
23-
public function __construct(EggRepositoryInterface $repository)
24-
{
25-
parent::__construct();
26-
27-
$this->repository = $repository;
28-
}
29-
3014
/**
3115
* Return all eggs that exist for a given nest.
3216
*/
33-
public function index(GetEggsRequest $request): array
17+
public function index(GetEggsRequest $request, Nest $nest): array
3418
{
35-
$eggs = $this->repository->findWhere([
36-
['nest_id', '=', $request->getModel(Nest::class)->id],
37-
]);
38-
39-
return $this->fractal->collection($eggs)
19+
return $this->fractal->collection($nest->eggs)
4020
->transformWith($this->getTransformer(EggTransformer::class))
4121
->toArray();
4222
}
4323

4424
/**
4525
* Return a single egg that exists on the specified nest.
4626
*/
47-
public function view(GetEggRequest $request): array
27+
public function view(GetEggRequest $request, Nest $nest, Egg $egg): array
4828
{
49-
return $this->fractal->item($request->getModel(Egg::class))
29+
return $this->fractal->item($egg)
5030
->transformWith($this->getTransformer(EggTransformer::class))
5131
->toArray();
5232
}

app/Http/Controllers/Api/Application/Nests/NestController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ public function index(GetNestsRequest $request): array
4040
/**
4141
* Return information about a single Nest model.
4242
*/
43-
public function view(GetNestsRequest $request): array
43+
public function view(GetNestsRequest $request, Nest $nest): array
4444
{
45-
return $this->fractal->item($request->getModel(Nest::class))
45+
return $this->fractal->item($nest)
4646
->transformWith($this->getTransformer(NestTransformer::class))
4747
->toArray();
4848
}

app/Http/Controllers/Api/Application/Servers/DatabaseController.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,10 @@ public function store(StoreServerDatabaseRequest $request, Server $server): Json
105105

106106
/**
107107
* Handle a request to delete a specific server database from the Panel.
108-
*
109-
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
110108
*/
111-
public function delete(ServerDatabaseWriteRequest $request): Response
109+
public function delete(ServerDatabaseWriteRequest $request, Server $server, Database $database): Response
112110
{
113-
$this->databaseManagementService->delete($request->getModel(Database::class));
111+
$this->databaseManagementService->delete($database);
114112

115113
return response('', 204);
116114
}

app/Http/Controllers/Api/Application/Servers/ExternalServerController.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

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

5+
use Pterodactyl\Models\Server;
56
use Pterodactyl\Transformers\Api\Application\ServerTransformer;
67
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
78
use Pterodactyl\Http\Requests\Api\Application\Servers\GetExternalServerRequest;
@@ -11,9 +12,11 @@ class ExternalServerController extends ApplicationApiController
1112
/**
1213
* Retrieve a specific server from the database using its external ID.
1314
*/
14-
public function index(GetExternalServerRequest $request): array
15+
public function index(GetExternalServerRequest $request, string $external_id): array
1516
{
16-
return $this->fractal->item($request->getServerModel())
17+
$server = Server::query()->where('external_id', $external_id)->firstOrFail();
18+
19+
return $this->fractal->item($server)
1720
->transformWith($this->getTransformer(ServerTransformer::class))
1821
->toArray();
1922
}

app/Http/Controllers/Api/Application/Servers/ServerController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ public function store(StoreServerRequest $request): JsonResponse
8686
/**
8787
* Show a single server transformed for the application API.
8888
*/
89-
public function view(GetServerRequest $request): array
89+
public function view(GetServerRequest $request, Server $server): array
9090
{
91-
return $this->fractal->item($request->getModel(Server::class))
91+
return $this->fractal->item($server)
9292
->transformWith($this->getTransformer(ServerTransformer::class))
9393
->toArray();
9494
}

app/Http/Controllers/Api/Application/Servers/ServerDetailsController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ public function __construct(
4242
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
4343
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
4444
*/
45-
public function details(UpdateServerDetailsRequest $request): array
45+
public function details(UpdateServerDetailsRequest $request, Server $server): array
4646
{
47-
$server = $this->detailsModificationService->returnUpdatedModel()->handle(
48-
$request->getModel(Server::class),
47+
$updated = $this->detailsModificationService->returnUpdatedModel()->handle(
48+
$server,
4949
$request->validated()
5050
);
5151

52-
return $this->fractal->item($server)
52+
return $this->fractal->item($updated)
5353
->transformWith($this->getTransformer(ServerTransformer::class))
5454
->toArray();
5555
}

app/Http/Controllers/Api/Application/Servers/StartupController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ public function __construct(StartupModificationService $modificationService)
3434
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
3535
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
3636
*/
37-
public function index(UpdateServerStartupRequest $request): array
37+
public function index(UpdateServerStartupRequest $request, Server $server): array
3838
{
3939
$server = $this->modificationService
4040
->setUserLevel(User::USER_LEVEL_ADMIN)
41-
->handle($request->getModel(Server::class), $request->validated());
41+
->handle($server, $request->validated());
4242

4343
return $this->fractal->item($server)
4444
->transformWith($this->getTransformer(ServerTransformer::class))

app/Http/Controllers/Api/Application/Users/ExternalUserController.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

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

5+
use Pterodactyl\Models\User;
56
use Pterodactyl\Transformers\Api\Application\UserTransformer;
67
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
78
use Pterodactyl\Http\Requests\Api\Application\Users\GetExternalUserRequest;
@@ -11,9 +12,11 @@ class ExternalUserController extends ApplicationApiController
1112
/**
1213
* Retrieve a specific user from the database using their external ID.
1314
*/
14-
public function index(GetExternalUserRequest $request): array
15+
public function index(GetExternalUserRequest $request, string $external_id): array
1516
{
16-
return $this->fractal->item($request->getUserModel())
17+
$user = User::query()->where('external_id', $external_id)->firstOrFail();
18+
19+
return $this->fractal->item($user)
1720
->transformWith($this->getTransformer(UserTransformer::class))
1821
->toArray();
1922
}

app/Http/Controllers/Api/Client/Servers/ScheduleController.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ public function __construct(ScheduleRepository $repository, ProcessScheduleServi
5252
*/
5353
public function index(ViewScheduleRequest $request, Server $server)
5454
{
55-
$schedules = $server->schedule;
56-
$schedules->loadMissing('tasks');
55+
$schedules = $server->schedules->loadMissing('tasks');
5756

5857
return $this->fractal->collection($schedules)
5958
->transformWith($this->getTransformer(ScheduleTransformer::class))

0 commit comments

Comments
 (0)