Path: blob/1.0-develop/database/Seeders/EggSeeder.php
7458 views
<?php12namespace Database\Seeders;34use Pterodactyl\Models\Egg;5use Pterodactyl\Models\Nest;6use Illuminate\Database\Seeder;7use Illuminate\Http\UploadedFile;8use Pterodactyl\Services\Eggs\Sharing\EggImporterService;9use Pterodactyl\Services\Eggs\Sharing\EggUpdateImporterService;1011class EggSeeder extends Seeder12{13protected EggImporterService $importerService;1415protected EggUpdateImporterService $updateImporterService;1617/**18* @var string[]19*/20public static array $import = [21'Minecraft',22'Source Engine',23'Voice Servers',24'Rust',25];2627/**28* EggSeeder constructor.29*/30public function __construct(31EggImporterService $importerService,32EggUpdateImporterService $updateImporterService,33) {34$this->importerService = $importerService;35$this->updateImporterService = $updateImporterService;36}3738/**39* Run the egg seeder.40*/41public function run()42{43foreach (static::$import as $nest) {44/* @noinspection PhpParamsInspection */45$this->parseEggFiles(46Nest::query()->where('author', '[email protected]')->where('name', $nest)->firstOrFail()47);48}49}5051/**52* Loop through the list of egg files and import them.53*/54protected function parseEggFiles(Nest $nest)55{56$files = new \DirectoryIterator(database_path('Seeders/eggs/' . kebab_case($nest->name)));5758$this->command->alert('Updating Eggs for Nest: ' . $nest->name);59/** @var \DirectoryIterator $file */60foreach ($files as $file) {61if (!$file->isFile() || !$file->isReadable()) {62continue;63}6465$decoded = json_decode(file_get_contents($file->getRealPath()), true, 512, JSON_THROW_ON_ERROR);66$file = new UploadedFile($file->getPathname(), $file->getFilename(), 'application/json');6768$egg = $nest->eggs()69->where('author', $decoded['author'])70->where('name', $decoded['name'])71->first();7273if ($egg instanceof Egg) {74$this->updateImporterService->handle($egg, $file);75$this->command->info('Updated ' . $decoded['name']);76} else {77$this->importerService->handle($file, $nest->id);78$this->command->comment('Created ' . $decoded['name']);79}80}8182$this->command->line('');83}84}858687