|
| 1 | +<?php |
| 2 | + |
| 3 | +use Pterodactyl\Models\Nest; |
| 4 | +use Illuminate\Database\Seeder; |
| 5 | +use Illuminate\Http\UploadedFile; |
| 6 | +use Illuminate\Support\Collection; |
| 7 | +use Illuminate\Filesystem\Filesystem; |
| 8 | +use Pterodactyl\Services\Eggs\Sharing\EggImporterService; |
| 9 | +use Pterodactyl\Contracts\Repository\EggRepositoryInterface; |
| 10 | +use Pterodactyl\Contracts\Repository\NestRepositoryInterface; |
| 11 | +use Pterodactyl\Exceptions\Repository\RecordNotFoundException; |
| 12 | +use Pterodactyl\Services\Eggs\Sharing\EggUpdateImporterService; |
| 13 | + |
| 14 | +class EggSeeder extends Seeder |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var \Illuminate\Filesystem\Filesystem |
| 18 | + */ |
| 19 | + private $filesystem; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var \Pterodactyl\Services\Eggs\Sharing\EggImporterService |
| 23 | + */ |
| 24 | + private $importerService; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface |
| 28 | + */ |
| 29 | + private $nestRepository; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface |
| 33 | + */ |
| 34 | + private $repository; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var \Pterodactyl\Services\Eggs\Sharing\EggUpdateImporterService |
| 38 | + */ |
| 39 | + private $updateImporterService; |
| 40 | + |
| 41 | + /** |
| 42 | + * EggSeeder constructor. |
| 43 | + * |
| 44 | + * @param \Pterodactyl\Services\Eggs\Sharing\EggImporterService $importerService |
| 45 | + * @param \Pterodactyl\Contracts\Repository\EggRepositoryInterface $repository |
| 46 | + * @param \Pterodactyl\Services\Eggs\Sharing\EggUpdateImporterService $updateImporterService |
| 47 | + * @param \Illuminate\Filesystem\Filesystem $filesystem |
| 48 | + * @param \Pterodactyl\Contracts\Repository\NestRepositoryInterface $nestRepository |
| 49 | + */ |
| 50 | + public function __construct( |
| 51 | + EggImporterService $importerService, |
| 52 | + EggRepositoryInterface $repository, |
| 53 | + EggUpdateImporterService $updateImporterService, |
| 54 | + Filesystem $filesystem, |
| 55 | + NestRepositoryInterface $nestRepository |
| 56 | + ) { |
| 57 | + $this->filesystem = $filesystem; |
| 58 | + $this->importerService = $importerService; |
| 59 | + $this->repository = $repository; |
| 60 | + $this->updateImporterService = $updateImporterService; |
| 61 | + $this->nestRepository = $nestRepository; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Run the egg seeder. |
| 66 | + */ |
| 67 | + public function run() |
| 68 | + { |
| 69 | + $this->getEggsToImport()->each(function ($nest) { |
| 70 | + $this->parseEggFiles($this->findMatchingNest($nest)); |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Return a list of eggs to import. |
| 76 | + * |
| 77 | + * @return \Illuminate\Support\Collection |
| 78 | + */ |
| 79 | + protected function getEggsToImport(): Collection |
| 80 | + { |
| 81 | + return collect([ |
| 82 | + 'Minecraft', |
| 83 | + 'Source Engine', |
| 84 | + 'Voice Servers', |
| 85 | + ]); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Find the nest that these eggs should be attached to. |
| 90 | + * |
| 91 | + * @param string $nestName |
| 92 | + * @return \Pterodactyl\Models\Nest |
| 93 | + * |
| 94 | + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException |
| 95 | + */ |
| 96 | + private function findMatchingNest(string $nestName): Nest |
| 97 | + { |
| 98 | + return $this->nestRepository->findFirstWhere([ |
| 99 | + ['author', '=', 'support@pterodactyl.io'], |
| 100 | + ['name', '=', $nestName], |
| 101 | + ]); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Loop through the list of egg files and import them. |
| 106 | + * |
| 107 | + * @param \Pterodactyl\Models\Nest $nest |
| 108 | + */ |
| 109 | + private function parseEggFiles(Nest $nest) |
| 110 | + { |
| 111 | + $files = $this->filesystem->allFiles(database_path('seeds/eggs/' . kebab_case($nest->name))); |
| 112 | + |
| 113 | + $this->command->alert('Updating Eggs for Nest: ' . $nest->name); |
| 114 | + collect($files)->each(function ($file) use ($nest) { |
| 115 | + /* @var \Symfony\Component\Finder\SplFileInfo $file */ |
| 116 | + $decoded = json_decode($file->getContents()); |
| 117 | + if (json_last_error() !== JSON_ERROR_NONE) { |
| 118 | + return $this->command->error('JSON decode exception for ' . $file->getFilename() . ': ' . json_last_error_msg()); |
| 119 | + } |
| 120 | + |
| 121 | + $file = new UploadedFile($file->getPathname(), $file->getFilename(), 'application/json', $file->getSize()); |
| 122 | + |
| 123 | + try { |
| 124 | + $egg = $this->repository->withColumns('id')->findFirstWhere([ |
| 125 | + ['author', '=', $decoded->author], |
| 126 | + ['name', '=', $decoded->name], |
| 127 | + ['nest_id', '=', $nest->id], |
| 128 | + ]); |
| 129 | + |
| 130 | + $this->updateImporterService->handle($egg->id, $file); |
| 131 | + |
| 132 | + return $this->command->info('Updated ' . $decoded->name); |
| 133 | + } catch (RecordNotFoundException $exception) { |
| 134 | + $this->importerService->handle($file, $nest->id); |
| 135 | + |
| 136 | + return $this->command->comment('Created ' . $decoded->name); |
| 137 | + } |
| 138 | + }); |
| 139 | + |
| 140 | + $this->command->line(''); |
| 141 | + } |
| 142 | +} |
0 commit comments