Skip to content

Commit 2c77d5c

Browse files
committed
Begin implementation of services for services/service options
1 parent 7277f72 commit 2c77d5c

21 files changed

+567
-408
lines changed

app/Contracts/Repository/DatabaseHostRepositoryInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@
2626

2727
interface DatabaseHostRepositoryInterface extends RepositoryInterface
2828
{
29+
/**
30+
* Return database hosts with a count of databases and the node information for which it is attached.
31+
*
32+
* @return \Illuminate\Support\Collection
33+
*/
34+
public function getWithViewDetails();
35+
2936
/**
3037
* Delete a database host from the DB if there are no databases using it.
3138
*

app/Contracts/Repository/LocationRepositoryInterface.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,14 @@ public function deleteIfNoNodes($id);
4444
*
4545
* @return mixed
4646
*/
47-
public function allWithDetails();
47+
public function getAllWithDetails();
48+
49+
/**
50+
* Return all of the available locations with the nodes as a relationship.
51+
*
52+
* @return \Illuminate\Support\Collection
53+
*/
54+
public function getAllWithNodes();
4855

4956
/**
5057
* Return all of the nodes and their respective count of servers for a location.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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\Contracts\Repository;
26+
27+
interface ServiceOptionRepositoryInterface extends RepositoryInterface
28+
{
29+
//
30+
}

app/Http/Controllers/Admin/DatabaseController.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424

2525
namespace Pterodactyl\Http\Controllers\Admin;
2626

27-
use Pterodactyl\Models\Location;
2827
use Pterodactyl\Models\DatabaseHost;
2928
use Prologue\Alerts\AlertsMessageBag;
3029
use Pterodactyl\Http\Controllers\Controller;
3130
use Pterodactyl\Services\Database\DatabaseHostService;
3231
use Pterodactyl\Http\Requests\Admin\DatabaseHostFormRequest;
32+
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
33+
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
3334

3435
class DatabaseController extends Controller
3536
{
@@ -39,14 +40,14 @@ class DatabaseController extends Controller
3940
protected $alert;
4041

4142
/**
42-
* @var \Pterodactyl\Models\DatabaseHost
43+
* @var \Pterodactyl\Contracts\Repository\LocationRepositoryInterface
4344
*/
44-
protected $hostModel;
45+
protected $locationRepository;
4546

4647
/**
47-
* @var \Pterodactyl\Models\Location
48+
* @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface
4849
*/
49-
protected $locationModel;
50+
protected $repository;
5051

5152
/**
5253
* @var \Pterodactyl\Services\Database\DatabaseHostService
@@ -56,21 +57,21 @@ class DatabaseController extends Controller
5657
/**
5758
* DatabaseController constructor.
5859
*
59-
* @param \Prologue\Alerts\AlertsMessageBag $alert
60-
* @param \Pterodactyl\Models\DatabaseHost $hostModel
61-
* @param \Pterodactyl\Models\Location $locationModel
62-
* @param \Pterodactyl\Services\Database\DatabaseHostService $service
60+
* @param \Prologue\Alerts\AlertsMessageBag $alert
61+
* @param \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface $repository
62+
* @param \Pterodactyl\Services\Database\DatabaseHostService $service
63+
* @param \Pterodactyl\Contracts\Repository\LocationRepositoryInterface $locationRepository
6364
*/
6465
public function __construct(
6566
AlertsMessageBag $alert,
66-
DatabaseHost $hostModel,
67-
Location $locationModel,
68-
DatabaseHostService $service
67+
DatabaseHostRepositoryInterface $repository,
68+
DatabaseHostService $service,
69+
LocationRepositoryInterface $locationRepository
6970
) {
7071
$this->alert = $alert;
71-
$this->hostModel = $hostModel;
72-
$this->locationModel = $locationModel;
72+
$this->repository = $repository;
7373
$this->service = $service;
74+
$this->locationRepository = $locationRepository;
7475
}
7576

7677
/**
@@ -81,8 +82,8 @@ public function __construct(
8182
public function index()
8283
{
8384
return view('admin.databases.index', [
84-
'locations' => $this->locationModel->with('nodes')->get(),
85-
'hosts' => $this->hostModel->withCount('databases')->with('node')->get(),
85+
'locations' => $this->locationRepository->getAllWithNodes(),
86+
'hosts' => $this->repository->getWithViewDetails(),
8687
]);
8788
}
8889

@@ -97,7 +98,7 @@ public function view(DatabaseHost $host)
9798
$host->load('databases.server');
9899

99100
return view('admin.databases.view', [
100-
'locations' => $this->locationModel->with('nodes')->get(),
101+
'locations' => $this->locationRepository->getAllWithNodes(),
101102
'host' => $host,
102103
]);
103104
}

app/Http/Controllers/Admin/LocationController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function __construct(
7474
public function index()
7575
{
7676
return view('admin.locations.index', [
77-
'locations' => $this->repository->allWithDetails(),
77+
'locations' => $this->repository->getAllWithDetails(),
7878
]);
7979
}
8080

app/Http/Controllers/Admin/OptionController.php

Lines changed: 73 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626

2727
use Log;
2828
use Alert;
29+
use Prologue\Alerts\AlertsMessageBag;
30+
use Pterodactyl\Contracts\Repository\ServiceRepositoryInterface;
31+
use Pterodactyl\Http\Requests\Admin\OptionVariableFormRequest;
32+
use Pterodactyl\Http\Requests\Admin\ServiceOptionFormRequest;
33+
use Pterodactyl\Services\Services\Options\CreationService;
34+
use Pterodactyl\Services\Services\Variables\VariableCreationService;
2935
use Route;
3036
use Javascript;
3137
use Illuminate\Http\Request;
@@ -42,100 +48,95 @@
4248
class OptionController extends Controller
4349
{
4450
/**
45-
* Store the repository instance.
46-
*
47-
* @var \Pterodactyl\Repositories\OptionRepository
51+
* @var \Prologue\Alerts\AlertsMessageBag
4852
*/
49-
protected $repository;
53+
protected $alert;
54+
55+
/**
56+
* @var \Pterodactyl\Services\Services\Options\CreationService
57+
*/
58+
protected $creationService;
59+
60+
/**
61+
* @var \Pterodactyl\Contracts\Repository\ServiceRepositoryInterface
62+
*/
63+
protected $serviceRepository;
64+
65+
/**
66+
* @var \Pterodactyl\Services\Services\Variables\VariableCreationService
67+
*/
68+
protected $variableCreationService;
5069

5170
/**
5271
* OptionController constructor.
72+
*
73+
* @param \Prologue\Alerts\AlertsMessageBag $alert
74+
* @param \Pterodactyl\Contracts\Repository\ServiceRepositoryInterface $serviceRepository
75+
* @param \Pterodactyl\Services\Services\Options\CreationService $creationService
76+
* @param \Pterodactyl\Services\Services\Variables\VariableCreationService $variableCreationService
5377
*/
54-
public function __construct()
55-
{
56-
$this->repository = new OptionRepository(Route::current()->parameter('option'));
78+
public function __construct(
79+
AlertsMessageBag $alert,
80+
ServiceRepositoryInterface $serviceRepository,
81+
CreationService $creationService,
82+
VariableCreationService $variableCreationService
83+
) {
84+
$this->alert = $alert;
85+
$this->creationService = $creationService;
86+
$this->serviceRepository = $serviceRepository;
87+
$this->variableCreationService = $variableCreationService;
5788
}
5889

5990
/**
6091
* Handles request to view page for adding new option.
6192
*
62-
* @param \Illuminate\Http\Request $request
6393
* @return \Illuminate\View\View
6494
*/
65-
public function create(Request $request)
95+
public function create()
6696
{
67-
$services = Service::with('options')->get();
97+
$services = $this->serviceRepository->getWithOptions();
6898
Javascript::put(['services' => $services->keyBy('id')]);
6999

70100
return view('admin.services.options.new', ['services' => $services]);
71101
}
72102

73103
/**
74-
* Handles POST request to create a new option.
104+
* Handle adding a new service option.
105+
*
106+
* @param \Pterodactyl\Http\Requests\Admin\ServiceOptionFormRequest $request
107+
* @return \Illuminate\Http\RedirectResponse
75108
*
76-
* @param \Illuminate\Http\Request $request
77-
* @return \Illuminate\Response\RedirectResponse
109+
* @throws \Pterodactyl\Exceptions\DisplayException
110+
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
78111
*/
79-
public function store(Request $request)
112+
public function store(ServiceOptionFormRequest $request)
80113
{
81-
$repo = new OptionRepository;
82-
83-
try {
84-
$option = $repo->create($request->intersect([
85-
'service_id', 'name', 'description', 'tag',
86-
'docker_image', 'startup', 'config_from', 'config_startup',
87-
'config_logs', 'config_files', 'config_stop',
88-
]));
89-
Alert::success('Successfully created new service option.')->flash();
114+
$option = $this->creationService->handle($request->normalize());
115+
$this->alert->success(trans('admin/services.options.notices.option_created'))->flash();
90116

91-
return redirect()->route('admin.services.option.view', $option->id);
92-
} catch (DisplayValidationException $ex) {
93-
return redirect()->route('admin.services.option.new')->withErrors(json_decode($ex->getMessage()))->withInput();
94-
} catch (DisplayException $ex) {
95-
Alert::danger($ex->getMessage())->flash();
96-
} catch (\Exception $ex) {
97-
Log::error($ex);
98-
Alert::danger('An unhandled exception occurred while attempting to create this service. This error has been logged.')->flash();
99-
}
100-
101-
return redirect()->route('admin.services.option.new')->withInput();
117+
return redirect()->route('admin.services.option.view', $option->id);
102118
}
103119

104120
/**
105121
* Handles POST request to create a new option variable.
106122
*
107-
* @param \Illuminate\Http\Request $request
108-
* @param int $id
123+
* @param \Pterodactyl\Http\Requests\Admin\OptionVariableFormRequest $request
124+
* @param \Pterodactyl\Models\ServiceOption $option
109125
* @return \Illuminate\Http\RedirectResponse
110126
*/
111-
public function createVariable(Request $request, $id)
127+
public function createVariable(OptionVariableFormRequest $request, ServiceOption $option)
112128
{
113-
$repo = new VariableRepository;
114-
115-
try {
116-
$variable = $repo->create($id, $request->intersect([
117-
'name', 'description', 'env_variable',
118-
'default_value', 'options', 'rules',
119-
]));
120-
121-
Alert::success('New variable successfully assigned to this service option.')->flash();
122-
} catch (DisplayValidationException $ex) {
123-
return redirect()->route('admin.services.option.variables', $id)->withErrors(json_decode($ex->getMessage()));
124-
} catch (DisplayException $ex) {
125-
Alert::danger($ex->getMessage())->flash();
126-
} catch (\Exception $ex) {
127-
Log::error($ex);
128-
Alert::danger('An unhandled exception was encountered while attempting to process that request. This error has been logged.')->flash();
129-
}
129+
$this->variableCreationService->handle($option->id, $request->normalize());
130+
$this->alert->success(trans('admin/services.variables.notices.variable_created'))->flash();
130131

131-
return redirect()->route('admin.services.option.variables', $id);
132+
return redirect()->route('admin.services.option.variables', $option->id);
132133
}
133134

134135
/**
135136
* Display option overview page.
136137
*
137-
* @param \Illuminate\Http\Request $request
138-
* @param int $id
138+
* @param \Illuminate\Http\Request $request
139+
* @param int $id
139140
* @return \Illuminate\View\View
140141
*/
141142
public function viewConfiguration(Request $request, $id)
@@ -146,13 +147,14 @@ public function viewConfiguration(Request $request, $id)
146147
/**
147148
* Display variable overview page for a service option.
148149
*
149-
* @param \Illuminate\Http\Request $request
150-
* @param int $id
150+
* @param \Illuminate\Http\Request $request
151+
* @param int $id
151152
* @return \Illuminate\View\View
152153
*/
153154
public function viewVariables(Request $request, $id)
154155
{
155-
return view('admin.services.options.variables', ['option' => ServiceOption::with('variables')->findOrFail($id)]);
156+
return view('admin.services.options.variables', ['option' => ServiceOption::with('variables')
157+
->findOrFail($id), ]);
156158
}
157159

158160
/**
@@ -179,8 +181,8 @@ public function viewScripts(Request $request, $id)
179181
/**
180182
* Handles POST when editing a configration for a service option.
181183
*
182-
* @param \Illuminate\Http\Request $request
183-
* @param int $id
184+
* @param \Illuminate\Http\Request $request
185+
* @param int $id
184186
* @return \Illuminate\Http\RedirectResponse
185187
*/
186188
public function editConfiguration(Request $request, $id)
@@ -207,7 +209,8 @@ public function editConfiguration(Request $request, $id)
207209
Alert::danger($ex->getMessage())->flash();
208210
} catch (\Exception $ex) {
209211
Log::error($ex);
210-
Alert::danger('An unhandled exception occurred while attempting to perform that action. This error has been logged.')->flash();
212+
Alert::danger('An unhandled exception occurred while attempting to perform that action. This error has been logged.')
213+
->flash();
211214
}
212215

213216
return redirect()->route('admin.services.option.view', $id);
@@ -216,9 +219,9 @@ public function editConfiguration(Request $request, $id)
216219
/**
217220
* Handles POST when editing a configration for a service option.
218221
*
219-
* @param \Pterodactyl\Http\Requests\Admin\Service\StoreOptionVariable $request
220-
* @param int $option
221-
* @param int $variable
222+
* @param \Pterodactyl\Http\Requests\Admin\Service\StoreOptionVariable $request
223+
* @param int $option
224+
* @param int $variable
222225
* @return \Illuminate\Http\RedirectResponse
223226
*/
224227
public function editVariable(StoreOptionVariable $request, $option, $variable)
@@ -237,7 +240,8 @@ public function editVariable(StoreOptionVariable $request, $option, $variable)
237240
Alert::danger($ex->getMessage())->flash();
238241
} catch (\Exception $ex) {
239242
Log::error($ex);
240-
Alert::danger('An unhandled exception was encountered while attempting to process that request. This error has been logged.')->flash();
243+
Alert::danger('An unhandled exception was encountered while attempting to process that request. This error has been logged.')
244+
->flash();
241245
}
242246

243247
return redirect()->route('admin.services.option.variables', $option);
@@ -259,7 +263,8 @@ public function updateScripts(EditOptionScript $request)
259263
Alert::danger($ex->getMessage())->flash();
260264
} catch (\Exception $ex) {
261265
Log::error($ex);
262-
Alert::danger('An unhandled exception was encountered while attempting to process that request. This error has been logged.')->flash();
266+
Alert::danger('An unhandled exception was encountered while attempting to process that request. This error has been logged.')
267+
->flash();
263268
}
264269

265270
return redirect()->route('admin.services.option.scripts', $id);

0 commit comments

Comments
 (0)