2017-10-07 04:57:53 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Admin\Nests;
|
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
use JavaScript;
|
2017-10-07 04:57:53 +00:00
|
|
|
use Illuminate\View\View;
|
|
|
|
use Pterodactyl\Models\Egg;
|
2022-10-24 00:15:11 +00:00
|
|
|
use Pterodactyl\Models\Nest;
|
2017-10-07 21:16:51 +00:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
use Prologue\Alerts\AlertsMessageBag;
|
2022-10-14 16:59:20 +00:00
|
|
|
use Illuminate\View\Factory as ViewFactory;
|
2017-10-07 04:57:53 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
2017-10-07 21:16:51 +00:00
|
|
|
use Pterodactyl\Services\Eggs\EggUpdateService;
|
|
|
|
use Pterodactyl\Services\Eggs\EggCreationService;
|
|
|
|
use Pterodactyl\Services\Eggs\EggDeletionService;
|
|
|
|
use Pterodactyl\Http\Requests\Admin\Egg\EggFormRequest;
|
2017-10-07 04:57:53 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
|
2022-10-24 00:15:11 +00:00
|
|
|
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
2017-10-07 04:57:53 +00:00
|
|
|
|
|
|
|
class EggController extends Controller
|
|
|
|
{
|
2022-10-14 16:59:20 +00:00
|
|
|
/**
|
|
|
|
* EggController constructor.
|
|
|
|
*/
|
2017-10-07 21:16:51 +00:00
|
|
|
public function __construct(
|
2022-10-14 16:59:20 +00:00
|
|
|
protected AlertsMessageBag $alert,
|
|
|
|
protected EggCreationService $creationService,
|
|
|
|
protected EggDeletionService $deletionService,
|
|
|
|
protected EggRepositoryInterface $repository,
|
|
|
|
protected EggUpdateService $updateService,
|
|
|
|
protected ViewFactory $view
|
2017-10-07 21:16:51 +00:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a request to display the Egg creation page.
|
|
|
|
*/
|
|
|
|
public function create(): View
|
|
|
|
{
|
2022-10-24 00:15:11 +00:00
|
|
|
$nests = Nest::with('eggs.variables')->get();
|
2022-10-14 16:59:20 +00:00
|
|
|
JavaScript::put(['nests' => $nests->keyBy('id')]);
|
2017-10-07 21:16:51 +00:00
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
return $this->view->make('admin.eggs.new', ['nests' => $nests]);
|
2017-10-07 21:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle request to store a new Egg.
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Egg\NoParentConfigurationFoundException
|
|
|
|
*/
|
|
|
|
public function store(EggFormRequest $request): RedirectResponse
|
|
|
|
{
|
2022-09-25 19:24:54 +00:00
|
|
|
$data = $request->validated();
|
2022-05-07 21:45:22 +00:00
|
|
|
$data['docker_images'] = $this->normalizeDockerImages($data['docker_images'] ?? null);
|
2020-12-13 18:13:32 +00:00
|
|
|
|
|
|
|
$egg = $this->creationService->handle($data);
|
2017-10-07 21:16:51 +00:00
|
|
|
$this->alert->success(trans('admin/nests.eggs.notices.egg_created'))->flash();
|
|
|
|
|
|
|
|
return redirect()->route('admin.nests.egg.view', $egg->id);
|
2017-10-07 04:57:53 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 21:16:51 +00:00
|
|
|
/**
|
|
|
|
* Handle request to view a single Egg.
|
|
|
|
*/
|
2017-10-07 04:57:53 +00:00
|
|
|
public function view(Egg $egg): View
|
|
|
|
{
|
2022-10-14 16:59:20 +00:00
|
|
|
return $this->view->make('admin.eggs.view', [
|
2022-05-07 21:45:22 +00:00
|
|
|
'egg' => $egg,
|
|
|
|
'images' => array_map(
|
|
|
|
fn ($key, $value) => $key === $value ? $value : "$key|$value",
|
|
|
|
array_keys($egg->docker_images),
|
|
|
|
$egg->docker_images,
|
|
|
|
),
|
|
|
|
]);
|
2017-10-07 21:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle request to update an Egg.
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
2022-10-24 00:15:11 +00:00
|
|
|
* @throws RecordNotFoundException
|
2017-10-07 21:16:51 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Egg\NoParentConfigurationFoundException
|
|
|
|
*/
|
|
|
|
public function update(EggFormRequest $request, Egg $egg): RedirectResponse
|
|
|
|
{
|
2022-09-25 19:24:54 +00:00
|
|
|
$data = $request->validated();
|
2022-05-07 21:45:22 +00:00
|
|
|
$data['docker_images'] = $this->normalizeDockerImages($data['docker_images'] ?? null);
|
2020-12-13 18:13:32 +00:00
|
|
|
|
|
|
|
$this->updateService->handle($egg, $data);
|
2017-10-07 21:16:51 +00:00
|
|
|
$this->alert->success(trans('admin/nests.eggs.notices.updated'))->flash();
|
|
|
|
|
|
|
|
return redirect()->route('admin.nests.egg.view', $egg->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle request to destroy an egg.
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Egg\HasChildrenException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\HasActiveServersException
|
|
|
|
*/
|
|
|
|
public function destroy(Egg $egg): RedirectResponse
|
|
|
|
{
|
|
|
|
$this->deletionService->handle($egg->id);
|
|
|
|
$this->alert->success(trans('admin/nests.eggs.notices.deleted'))->flash();
|
|
|
|
|
|
|
|
return redirect()->route('admin.nests.view', $egg->nest_id);
|
2017-10-07 04:57:53 +00:00
|
|
|
}
|
2022-05-07 21:45:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Normalizes a string of docker image data into the expected egg format.
|
|
|
|
*/
|
|
|
|
protected function normalizeDockerImages(string $input = null): array
|
|
|
|
{
|
|
|
|
$data = array_map(fn ($value) => trim($value), explode("\n", $input ?? ''));
|
|
|
|
|
|
|
|
$images = [];
|
|
|
|
// Iterate over the image data provided and convert it into a name => image
|
|
|
|
// pairing that is used to improve the display on the front-end.
|
|
|
|
foreach ($data as $value) {
|
|
|
|
$parts = explode('|', $value, 2);
|
|
|
|
$images[$parts[0]] = empty($parts[1]) ? $parts[0] : $parts[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $images;
|
|
|
|
}
|
2017-10-07 04:57:53 +00:00
|
|
|
}
|