misc_pterodactyl-panel/database/Seeders/EggSeeder.php

135 lines
3.9 KiB
PHP
Raw Normal View History

2017-11-04 04:07:18 +00:00
<?php
namespace Database\Seeders;
2017-11-04 04:07:18 +00:00
use Pterodactyl\Models\Nest;
use Illuminate\Database\Seeder;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Collection;
use Illuminate\Filesystem\Filesystem;
use Pterodactyl\Services\Eggs\Sharing\EggImporterService;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
use Pterodactyl\Services\Eggs\Sharing\EggUpdateImporterService;
class EggSeeder extends Seeder
{
/**
* @var \Illuminate\Filesystem\Filesystem
*/
private $filesystem;
/**
* @var \Pterodactyl\Services\Eggs\Sharing\EggImporterService
*/
private $importerService;
/**
* @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface
*/
private $nestRepository;
/**
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface
*/
private $repository;
/**
* @var \Pterodactyl\Services\Eggs\Sharing\EggUpdateImporterService
*/
private $updateImporterService;
/**
* EggSeeder constructor.
*/
public function __construct(
EggImporterService $importerService,
EggRepositoryInterface $repository,
EggUpdateImporterService $updateImporterService,
Filesystem $filesystem,
NestRepositoryInterface $nestRepository
) {
$this->filesystem = $filesystem;
$this->importerService = $importerService;
$this->repository = $repository;
$this->updateImporterService = $updateImporterService;
$this->nestRepository = $nestRepository;
}
/**
* Run the egg seeder.
*/
public function run()
{
$this->getEggsToImport()->each(function ($nest) {
$this->parseEggFiles($this->findMatchingNest($nest));
});
}
/**
* Return a list of eggs to import.
*/
protected function getEggsToImport(): Collection
{
return collect([
'Minecraft',
'Source Engine',
'Voice Servers',
2017-11-06 02:24:58 +00:00
'Rust',
2017-11-04 04:07:18 +00:00
]);
}
/**
* Find the nest that these eggs should be attached to.
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
private function findMatchingNest(string $nestName): Nest
{
return $this->nestRepository->findFirstWhere([
['author', '=', 'support@pterodactyl.io'],
['name', '=', $nestName],
]);
}
/**
* Loop through the list of egg files and import them.
*/
private function parseEggFiles(Nest $nest)
{
$files = $this->filesystem->allFiles(database_path('Seeders/eggs/' . kebab_case($nest->name)));
2017-11-04 04:07:18 +00:00
$this->command->alert('Updating Eggs for Nest: ' . $nest->name);
2020-06-26 04:16:59 +00:00
Collection::make($files)->each(function ($file) use ($nest) {
2017-11-04 04:07:18 +00:00
/* @var \Symfony\Component\Finder\SplFileInfo $file */
$decoded = json_decode($file->getContents());
if (json_last_error() !== JSON_ERROR_NONE) {
2020-06-26 04:16:59 +00:00
$this->command->error('JSON decode exception for ' . $file->getFilename() . ': ' . json_last_error_msg());
2020-06-28 22:43:44 +00:00
2020-06-26 04:16:59 +00:00
return;
2017-11-04 04:07:18 +00:00
}
2020-06-26 04:16:59 +00:00
$file = new UploadedFile($file->getPathname(), $file->getFilename(), 'application/json');
2017-11-04 04:07:18 +00:00
try {
$egg = $this->repository->setColumns('id')->findFirstWhere([
2017-11-04 04:07:18 +00:00
['author', '=', $decoded->author],
['name', '=', $decoded->name],
['nest_id', '=', $nest->id],
]);
2020-10-06 04:29:35 +00:00
$this->updateImporterService->handle($egg, $file);
2017-11-04 04:07:18 +00:00
2020-06-26 04:16:59 +00:00
$this->command->info('Updated ' . $decoded->name);
2017-11-04 04:07:18 +00:00
} catch (RecordNotFoundException $exception) {
$this->importerService->handleFile($nest->id, $file);
2017-11-04 04:07:18 +00:00
2020-06-26 04:16:59 +00:00
$this->command->comment('Created ' . $decoded->name);
2017-11-04 04:07:18 +00:00
}
});
$this->command->line('');
}
}