misc_pterodactyl-panel/database/Seeders/EggSeeder.php

86 lines
2.5 KiB
PHP
Raw Permalink Normal View History

2017-11-04 04:07:18 +00:00
<?php
namespace Database\Seeders;
use Pterodactyl\Models\Egg;
2017-11-04 04:07:18 +00:00
use Pterodactyl\Models\Nest;
use Illuminate\Database\Seeder;
use Illuminate\Http\UploadedFile;
use Pterodactyl\Services\Eggs\Sharing\EggImporterService;
use Pterodactyl\Services\Eggs\Sharing\EggUpdateImporterService;
class EggSeeder extends Seeder
{
protected EggImporterService $importerService;
2017-11-04 04:07:18 +00:00
protected EggUpdateImporterService $updateImporterService;
2017-11-04 04:07:18 +00:00
/**
* @var string[]
2017-11-04 04:07:18 +00:00
*/
public static array $import = [
'Minecraft',
'Source Engine',
'Voice Servers',
'Rust',
];
2017-11-04 04:07:18 +00:00
/**
* EggSeeder constructor.
*/
public function __construct(
EggImporterService $importerService,
EggUpdateImporterService $updateImporterService
2017-11-04 04:07:18 +00:00
) {
$this->importerService = $importerService;
$this->updateImporterService = $updateImporterService;
}
/**
* Run the egg seeder.
*/
public function run()
{
foreach (static::$import as $nest) {
/* @noinspection PhpParamsInspection */
$this->parseEggFiles(
Nest::query()->where('author', 'support@pterodactyl.io')->where('name', $nest)->firstOrFail()
);
}
2017-11-04 04:07:18 +00:00
}
/**
* Loop through the list of egg files and import them.
*/
protected function parseEggFiles(Nest $nest)
2017-11-04 04:07:18 +00:00
{
$files = new \DirectoryIterator(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);
/** @var \DirectoryIterator $file */
foreach ($files as $file) {
if (!$file->isFile() || !$file->isReadable()) {
continue;
2017-11-04 04:07:18 +00:00
}
$decoded = json_decode(file_get_contents($file->getRealPath()), true, 512, JSON_THROW_ON_ERROR);
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
$egg = $nest->eggs()
->where('author', $decoded['author'])
->where('name', $decoded['name'])
->first();
2017-11-04 04:07:18 +00:00
if ($egg instanceof Egg) {
2020-10-06 04:29:35 +00:00
$this->updateImporterService->handle($egg, $file);
$this->command->info('Updated ' . $decoded['name']);
} else {
2017-11-04 04:07:18 +00:00
$this->importerService->handle($file, $nest->id);
$this->command->comment('Created ' . $decoded['name']);
2017-11-04 04:07:18 +00:00
}
}
2017-11-04 04:07:18 +00:00
$this->command->line('');
}
}