misc_pterodactyl-panel/database/Seeders/EggSeeder.php

84 lines
2.3 KiB
PHP
Raw 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
{
/**
* @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(
2022-12-15 00:05:46 +00:00
private EggImporterService $importerService,
private EggUpdateImporterService $updateImporterService
2017-11-04 04:07:18 +00:00
) {
}
/**
* Run the egg seeder.
2022-12-15 00:05:46 +00:00
*
* @throws \JsonException
2017-11-04 04:07:18 +00:00
*/
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.
2022-12-15 00:05:46 +00:00
*
* @throws \JsonException
2017-11-04 04:07:18 +00:00
*/
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 {
2022-12-15 00:05:46 +00:00
$this->importerService->handleFile($nest->id, $file);
$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('');
}
}