Update egg import/update logic to all use the same pathwaus

This commit is contained in:
DaneEveritt 2022-05-15 14:40:19 -04:00
parent 6554164252
commit cca0010a00
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
12 changed files with 271 additions and 334 deletions

View file

@ -2,59 +2,38 @@
namespace Database\Seeders;
use Pterodactyl\Models\Egg;
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;
protected EggImporterService $importerService;
protected EggUpdateImporterService $updateImporterService;
/**
* @var \Pterodactyl\Services\Eggs\Sharing\EggImporterService
* @var string[]
*/
private $importerService;
/**
* @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface
*/
private $nestRepository;
/**
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface
*/
private $repository;
/**
* @var \Pterodactyl\Services\Eggs\Sharing\EggUpdateImporterService
*/
private $updateImporterService;
public static array $import = [
'Minecraft',
'Source Engine',
'Voice Servers',
'Rust',
];
/**
* EggSeeder constructor.
*/
public function __construct(
EggImporterService $importerService,
EggRepositoryInterface $repository,
EggUpdateImporterService $updateImporterService,
Filesystem $filesystem,
NestRepositoryInterface $nestRepository
EggUpdateImporterService $updateImporterService
) {
$this->filesystem = $filesystem;
$this->importerService = $importerService;
$this->repository = $repository;
$this->updateImporterService = $updateImporterService;
$this->nestRepository = $nestRepository;
}
/**
@ -62,72 +41,44 @@ class EggSeeder extends 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',
'Rust',
]);
}
/**
* 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],
]);
foreach (static::$import as $nest) {
/* @noinspection PhpParamsInspection */
$this->parseEggFiles(
Nest::query()->where('author', 'support@pterodactyl.io')->where('name', $nest)->firstOrFail()
);
}
}
/**
* Loop through the list of egg files and import them.
*/
private function parseEggFiles(Nest $nest)
protected function parseEggFiles(Nest $nest)
{
$files = $this->filesystem->allFiles(database_path('Seeders/eggs/' . kebab_case($nest->name)));
$files = new \DirectoryIterator(database_path('Seeders/eggs/' . kebab_case($nest->name)));
$this->command->alert('Updating Eggs for Nest: ' . $nest->name);
Collection::make($files)->each(function ($file) use ($nest) {
/* @var \Symfony\Component\Finder\SplFileInfo $file */
$decoded = json_decode($file->getContents());
if (json_last_error() !== JSON_ERROR_NONE) {
$this->command->error('JSON decode exception for ' . $file->getFilename() . ': ' . json_last_error_msg());
return;
/** @var \DirectoryIterator $file */
foreach ($files as $file) {
if (!$file->isFile() || !$file->isReadable()) {
continue;
}
$decoded = json_decode(file_get_contents($file->getRealPath()), true, 512, JSON_THROW_ON_ERROR);
$file = new UploadedFile($file->getPathname(), $file->getFilename(), 'application/json');
try {
$egg = $this->repository->setColumns('id')->findFirstWhere([
['author', '=', $decoded->author],
['name', '=', $decoded->name],
['nest_id', '=', $nest->id],
]);
$egg = $nest->eggs()
->where('author', $decoded['author'])
->where('name', $decoded['name'])
->first();
if ($egg instanceof Egg) {
$this->updateImporterService->handle($egg, $file);
$this->command->info('Updated ' . $decoded->name);
} catch (RecordNotFoundException $exception) {
$this->command->info('Updated ' . $decoded['name']);
} else {
$this->importerService->handle($file, $nest->id);
$this->command->comment('Created ' . $decoded->name);
$this->command->comment('Created ' . $decoded['name']);
}
});
}
$this->command->line('');
}