Path: blob/1.0-develop/app/Services/Eggs/Sharing/EggUpdateImporterService.php
10311 views
<?php12namespace Pterodactyl\Services\Eggs\Sharing;34use Pterodactyl\Models\Egg;5use Illuminate\Http\UploadedFile;6use Illuminate\Support\Collection;7use Pterodactyl\Models\EggVariable;8use Illuminate\Database\ConnectionInterface;9use Pterodactyl\Services\Eggs\EggParserService;1011class EggUpdateImporterService12{13/**14* EggUpdateImporterService constructor.15*/16public function __construct(protected ConnectionInterface $connection, protected EggParserService $parser)17{18}1920/**21* Update an existing Egg using an uploaded JSON file.22*23* @throws \Pterodactyl\Exceptions\Service\InvalidFileUploadException|\Throwable24*/25public function handle(Egg $egg, UploadedFile $file): Egg26{27$parsed = $this->parser->handle($file);2829return $this->connection->transaction(function () use ($egg, $parsed) {30$egg = $this->parser->fillFromParsed($egg, $parsed);31$egg->save();3233// Update existing variables or create new ones.34foreach ($parsed['variables'] ?? [] as $variable) {35EggVariable::unguarded(function () use ($egg, $variable) {36$egg->variables()->updateOrCreate([37'env_variable' => $variable['env_variable'],38], Collection::make($variable)->except('egg_id', 'env_variable')->toArray());39});40}4142$imported = array_map(fn ($value) => $value['env_variable'], $parsed['variables'] ?? []);4344$egg->variables()->whereNotIn('env_variable', $imported)->delete();4546return $egg->refresh();47});48}49}505152