From b39596bb5ff8e251bc034cfce069187ad632c3e6 Mon Sep 17 00:00:00 2001 From: robert dennis <31261583+robertdrakedennis@users.noreply.github.com> Date: Mon, 12 Oct 2026 00:25:54 -0400 Subject: [PATCH] egg helpers --- app/Actions/Eggs/Sharing/ImportEgg.php | 7 +-- .../Eggs/Sharing/UpdateEggFromImport.php | 9 ++-- .../Servers/ServerDetailsController.php | 2 +- .../Wings/ServerConfigurationCollection.php | 2 +- app/Services/Eggs/EggConfigurationService.php | 7 +-- app/Services/Eggs/EggImportData.php | 41 +++++++++++++++ app/Services/Eggs/EggParserService.php | 52 ------------------- tests/Unit/EggConfigurationServiceTest.php | 8 +-- ...rServiceTest.php => EggImportDataTest.php} | 18 +++---- 9 files changed, 61 insertions(+), 85 deletions(-) delete mode 100644 app/Services/Eggs/EggParserService.php rename tests/Unit/Services/Eggs/{EggParserServiceTest.php => EggImportDataTest.php} (84%) diff --git a/app/Actions/Eggs/Sharing/ImportEgg.php b/app/Actions/Eggs/Sharing/ImportEgg.php index 89f2d2617..342aba23e 100644 --- a/app/Actions/Eggs/Sharing/ImportEgg.php +++ b/app/Actions/Eggs/Sharing/ImportEgg.php @@ -10,16 +10,13 @@ use Pterodactyl\Contracts\Eggs\ImportsEggs; use Pterodactyl\Models\Egg; use Pterodactyl\Models\EggVariable; use Pterodactyl\Services\Eggs\EggImportData; -use Pterodactyl\Services\Eggs\EggParserService; use Ramsey\Uuid\Uuid; final readonly class ImportEgg implements ImportsEggs { - public function __construct(private EggParserService $parser) {} - public function import(UploadedFile $file): Egg { - return $this->importParsed($this->parser->handle($file)); + return $this->importParsed(EggImportData::fromFile($file)); } public function importParsed(EggImportData $parsed): Egg @@ -29,8 +26,8 @@ final readonly class ImportEgg implements ImportsEggs 'uuid' => Uuid::uuid4()->toString(), 'author' => $parsed->author, 'copy_script_from' => null, + ...$parsed->attributes(), ]); - $egg = $this->parser->fillFromParsed($egg, $parsed); $egg->save(); foreach ($parsed->variables as $variable) { EggVariable::query()->forceCreate($variable->attributesForEgg($egg->id)); diff --git a/app/Actions/Eggs/Sharing/UpdateEggFromImport.php b/app/Actions/Eggs/Sharing/UpdateEggFromImport.php index 0494f0038..25767f229 100644 --- a/app/Actions/Eggs/Sharing/UpdateEggFromImport.php +++ b/app/Actions/Eggs/Sharing/UpdateEggFromImport.php @@ -9,20 +9,17 @@ use Illuminate\Support\Facades\DB; use Pterodactyl\Contracts\Eggs\UpdatesEggsFromImports; use Pterodactyl\Models\Egg; use Pterodactyl\Models\EggVariable; +use Pterodactyl\Services\Eggs\EggImportData; use Pterodactyl\Services\Eggs\EggImportVariable; -use Pterodactyl\Services\Eggs\EggParserService; final readonly class UpdateEggFromImport implements UpdatesEggsFromImports { - public function __construct(private EggParserService $parser) {} - public function update(Egg $egg, UploadedFile $file): Egg { - $parsed = $this->parser->handle($file); + $parsed = EggImportData::fromFile($file); return DB::transaction(function () use ($egg, $parsed): Egg { - $egg = $this->parser->fillFromParsed($egg, $parsed); - $egg->save(); + $egg->forceFill($parsed->attributes())->save(); foreach ($parsed->variables as $variable) { EggVariable::unguarded(function () use ($egg, $variable): void { diff --git a/app/Http/Controllers/Api/Remote/Servers/ServerDetailsController.php b/app/Http/Controllers/Api/Remote/Servers/ServerDetailsController.php index 27bd5cb0d..264c504ba 100644 --- a/app/Http/Controllers/Api/Remote/Servers/ServerDetailsController.php +++ b/app/Http/Controllers/Api/Remote/Servers/ServerDetailsController.php @@ -40,7 +40,7 @@ class ServerDetailsController extends Controller return new JsonResponse([ 'settings' => $configuration->forServer($server), - 'process_configuration' => $eggConfiguration->handle($server), + 'process_configuration' => $eggConfiguration->forServer($server), ]); } diff --git a/app/Http/Resources/Wings/ServerConfigurationCollection.php b/app/Http/Resources/Wings/ServerConfigurationCollection.php index 0e37dddd2..520e8b86e 100644 --- a/app/Http/Resources/Wings/ServerConfigurationCollection.php +++ b/app/Http/Resources/Wings/ServerConfigurationCollection.php @@ -34,7 +34,7 @@ class ServerConfigurationCollection extends ResourceCollection foreach ($this->collection ?? [] as $server) { throw_unless($server instanceof Server, UnexpectedValueException::class, 'Server configuration resources require Server models.'); - $processConfiguration = $this->egg->handle($server); + $processConfiguration = $this->egg->forServer($server); $result[] = [ 'uuid' => $server->uuid, diff --git a/app/Services/Eggs/EggConfigurationService.php b/app/Services/Eggs/EggConfigurationService.php index 91f9f58cd..cef3c7c2b 100644 --- a/app/Services/Eggs/EggConfigurationService.php +++ b/app/Services/Eggs/EggConfigurationService.php @@ -15,13 +15,10 @@ use UnexpectedValueException; class EggConfigurationService { - /** - * EggConfigurationService constructor. - */ public function __construct(private readonly ServerConfigurationStructureService $configurationStructureService) {} /** - * Return an Egg file to be used by the Daemon. + * The process configuration Wings reads for the server's egg. * * @return array{ * startup: array{done: mixed, user_interaction: array{}, strip_ansi: mixed}, @@ -29,7 +26,7 @@ class EggConfigurationService * configs: EggConfigurationOutputs, * } */ - public function handle(Server $server): array + public function forServer(Server $server): array { $egg = $server->egg ?? throw new UnexpectedValueException('The server does not have an egg assigned.'); $configs = $this->replacePlaceholders($server, $this->configurationFiles($egg->inherit_config_files)); diff --git a/app/Services/Eggs/EggImportData.php b/app/Services/Eggs/EggImportData.php index 5984bdf78..3fb95b5ab 100644 --- a/app/Services/Eggs/EggImportData.php +++ b/app/Services/Eggs/EggImportData.php @@ -4,8 +4,10 @@ declare(strict_types=1); namespace Pterodactyl\Services\Eggs; +use Illuminate\Http\UploadedFile; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Validator; +use JsonException; use Pterodactyl\Actions\Eggs\CreateEgg; use Pterodactyl\Actions\Eggs\Variables\CreateEggVariable; use Pterodactyl\Exceptions\Service\InvalidFileUploadException; @@ -38,11 +40,50 @@ final readonly class EggImportData public array $variables, ) {} + /** + * @throws InvalidFileUploadException + * @throws JsonException + */ + public static function fromFile(UploadedFile $file): self + { + throw_if($file->getError() !== UPLOAD_ERR_OK || ! $file->isFile(), InvalidFileUploadException::class, 'The selected file is not valid and cannot be imported.'); + + $contents = $file->openFile()->fread($file->getSize()); + throw_if($contents === false, InvalidFileUploadException::class, 'The selected file could not be read.'); + + return self::fromJson($contents); + } + public static function fromJson(string $contents): self { return self::fromValue(json_decode($contents, true, flags: JSON_THROW_ON_ERROR)); } + /** + * The egg attributes an import sets, both on a new egg and on one it updates. + * + * @return array + */ + public function attributes(): array + { + return [ + 'name' => $this->name, + 'description' => $this->description, + 'features' => $this->features, + 'docker_images' => $this->dockerImages, + 'file_denylist' => $this->fileDenylist, + 'update_url' => $this->updateUrl, + 'config_files' => $this->configFiles, + 'config_startup' => $this->configStartup, + 'config_logs' => $this->configLogs, + 'config_stop' => $this->configStop, + 'startup' => $this->startup, + 'script_install' => $this->scriptInstall, + 'script_entry' => $this->scriptEntry, + 'script_container' => $this->scriptContainer, + ]; + } + private static function fromValue(mixed $value): self { $root = self::object($value, 'egg'); diff --git a/app/Services/Eggs/EggParserService.php b/app/Services/Eggs/EggParserService.php deleted file mode 100644 index 98b09b5dc..000000000 --- a/app/Services/Eggs/EggParserService.php +++ /dev/null @@ -1,52 +0,0 @@ -getError() !== UPLOAD_ERR_OK || ! $file->isFile(), InvalidFileUploadException::class, 'The selected file is not valid and cannot be imported.'); - - $contents = $file->openFile()->fread($file->getSize()); - throw_if($contents === false, InvalidFileUploadException::class, 'The selected file could not be read.'); - - return EggImportData::fromJson($contents); - } - - /** - * Fills the provided model with the parsed JSON data. - */ - public function fillFromParsed(Egg $model, EggImportData $parsed): Egg - { - return $model->forceFill([ - 'name' => $parsed->name, - 'description' => $parsed->description, - 'features' => $parsed->features, - 'docker_images' => $parsed->dockerImages, - 'file_denylist' => $parsed->fileDenylist, - 'update_url' => $parsed->updateUrl, - 'config_files' => $parsed->configFiles, - 'config_startup' => $parsed->configStartup, - 'config_logs' => $parsed->configLogs, - 'config_stop' => $parsed->configStop, - 'startup' => $parsed->startup, - 'script_install' => $parsed->scriptInstall, - 'script_entry' => $parsed->scriptEntry, - 'script_container' => $parsed->scriptContainer, - ]); - } -} diff --git a/tests/Unit/EggConfigurationServiceTest.php b/tests/Unit/EggConfigurationServiceTest.php index e91ba131d..2d926f022 100644 --- a/tests/Unit/EggConfigurationServiceTest.php +++ b/tests/Unit/EggConfigurationServiceTest.php @@ -18,25 +18,25 @@ uses(TestCase::class); test('maps legacy placeholders into the wings configuration', function () { $server = server('{"scalar":"invalid","missing-find":{"parser":"json"},"server.properties":{"parser":"properties","find":{"server-port":"{{server.build.default.port}}","enabled":true,"conditional":{"old":"{{env.PORT}}","new":"{{server.build.default.port}}"},"docker-interface":"{{config.docker.interface}}","mixed-placeholders":"{{config.docker.interface}} {{server.build.default.port}} {{env.PORT}}","missing-server":"{{server.missing}}","missing-env":"{{env.MISSING}}","empty-object":"{{env.EMPTY_OBJECT}}"}}}'); $service = new EggConfigurationService(configurationStructure($server)); - expect($service->handle($server))->toBe(['startup' => ['done' => ['Server started'], 'user_interaction' => [], 'strip_ansi' => false], 'stop' => ['type' => 'command', 'value' => 'stop'], 'configs' => [['parser' => 'properties', 'file' => 'server.properties', 'replace' => [['match' => 'server-port', 'replace_with' => '25565'], ['match' => 'enabled', 'replace_with' => true], ['match' => 'conditional', 'if_value' => 'old', 'replace_with' => '3000'], ['match' => 'conditional', 'if_value' => 'new', 'replace_with' => '25565'], ['match' => 'docker-interface', 'replace_with' => '{{config.docker.network.interface}}'], ['match' => 'mixed-placeholders', 'replace_with' => '{{config.docker.network.interface}} 25565 3000'], ['match' => 'missing-server', 'replace_with' => ''], ['match' => 'missing-env', 'replace_with' => ''], ['match' => 'empty-object', 'replace_with' => 'Array']]]]]); + expect($service->forServer($server))->toBe(['startup' => ['done' => ['Server started'], 'user_interaction' => [], 'strip_ansi' => false], 'stop' => ['type' => 'command', 'value' => 'stop'], 'configs' => [['parser' => 'properties', 'file' => 'server.properties', 'replace' => [['match' => 'server-port', 'replace_with' => '25565'], ['match' => 'enabled', 'replace_with' => true], ['match' => 'conditional', 'if_value' => 'old', 'replace_with' => '3000'], ['match' => 'conditional', 'if_value' => 'new', 'replace_with' => '25565'], ['match' => 'docker-interface', 'replace_with' => '{{config.docker.network.interface}}'], ['match' => 'mixed-placeholders', 'replace_with' => '{{config.docker.network.interface}} 25565 3000'], ['match' => 'missing-server', 'replace_with' => ''], ['match' => 'missing-env', 'replace_with' => ''], ['match' => 'empty-object', 'replace_with' => 'Array']]]]]); }); test('defaults missing startup flags and stop commands', function () { $server = server('{}', '{"done":[]}', null); $service = new EggConfigurationService(configurationStructure($server)); - expect($service->handle($server))->toBe(['startup' => ['done' => [], 'user_interaction' => [], 'strip_ansi' => false], 'stop' => ['type' => 'command', 'value' => ''], 'configs' => []]); + expect($service->forServer($server))->toBe(['startup' => ['done' => [], 'user_interaction' => [], 'strip_ansi' => false], 'stop' => ['type' => 'command', 'value' => ''], 'configs' => []]); }); test('preserves an enabled strip ansi flag', function () { $server = server('{}', '{"done":[],"strip_ansi":true}'); $service = new EggConfigurationService(configurationStructure($server)); - expect($service->handle($server)['startup']['strip_ansi'])->toBeTrue(); + expect($service->forServer($server)['startup']['strip_ansi'])->toBeTrue(); }); test('reads config that is empty or not json as empty instead of failing', function (string $broken) { $exceptions = Exceptions::fake(); $server = server($broken, $broken); $service = new EggConfigurationService(configurationStructure($server)); - expect($service->handle($server))->toBe(['startup' => ['done' => null, 'user_interaction' => [], 'strip_ansi' => false], 'stop' => ['type' => 'command', 'value' => 'stop'], 'configs' => []]); + expect($service->forServer($server))->toBe(['startup' => ['done' => null, 'user_interaction' => [], 'strip_ansi' => false], 'stop' => ['type' => 'command', 'value' => 'stop'], 'configs' => []]); $broken === '' ? $exceptions->assertNothingReported() : $exceptions->assertReported(JsonException::class); })->with(['empty' => '', 'not json' => '{"done":']); function server(string $configurationFiles, ?string $configurationStartup = '{"done":"Server started","strip_ansi":false}', ?string $configurationStop = 'stop'): Server diff --git a/tests/Unit/Services/Eggs/EggParserServiceTest.php b/tests/Unit/Services/Eggs/EggImportDataTest.php similarity index 84% rename from tests/Unit/Services/Eggs/EggParserServiceTest.php rename to tests/Unit/Services/Eggs/EggImportDataTest.php index 8d530a9ee..62b481b2d 100644 --- a/tests/Unit/Services/Eggs/EggParserServiceTest.php +++ b/tests/Unit/Services/Eggs/EggImportDataTest.php @@ -2,16 +2,16 @@ declare(strict_types=1); -namespace Pterodactyl\Tests\Pest\Unit\Services\Eggs\EggParserServiceTest; +namespace Pterodactyl\Tests\Pest\Unit\Services\Eggs\EggImportDataTest; use Illuminate\Http\UploadedFile; use Pterodactyl\Exceptions\Service\InvalidFileUploadException; -use Pterodactyl\Services\Eggs\EggParserService; +use Pterodactyl\Services\Eggs\EggImportData; use Pterodactyl\Tests\TestCase; uses(TestCase::class); test('parses a v2 egg into typed import data', function () { - $parsed = service()->handle(fixture('minecraft/egg-paper.json')); + $parsed = EggImportData::fromFile(fixture('minecraft/egg-paper.json')); expect($parsed->name)->toBe('Paper'); expect($parsed->dockerImages['Java 21'])->toBe('ghcr.io/pterodactyl/yolks:java_21'); expect($parsed->features)->toBe(['eula', 'java_version', 'pid_limit']); @@ -19,7 +19,7 @@ test('parses a v2 egg into typed import data', function () { expect($parsed->variables[0]->userViewable)->toBeTrue(); }); test('normalizes a v1 image list', function () { - $parsed = service()->handle(fixture('source-engine/egg-garrys-mod.json')); + $parsed = EggImportData::fromFile(fixture('source-engine/egg-garrys-mod.json')); expect($parsed->dockerImages)->toBe(['ghcr.io/pterodactyl/games:source' => 'ghcr.io/pterodactyl/games:source']); expect($parsed->variables[0]->environmentVariable)->toBe('SRCDS_MAP'); }); @@ -27,10 +27,10 @@ test('rejects unrecognized egg payloads', function () { $file = UploadedFile::fake()->createWithContent('invalid.json', '{"meta":{"version":"unknown"}}'); $this->expectException(InvalidFileUploadException::class); $this->expectExceptionMessage('The egg field "meta.version" is missing or invalid.'); - service()->handle($file); + EggImportData::fromFile($file); }); test('every bundled egg passes import validation', function (string $path) { - expect(service()->handle(fixture($path))->name)->not->toBeEmpty(); + expect(EggImportData::fromFile(fixture($path))->name)->not->toBeEmpty(); })->with(fn (): array => array_map( // The dataset is built before the application boots, so base_path() is not available yet. fn (string $file): string => basename(dirname($file)).'/'.basename($file), @@ -41,7 +41,7 @@ test('rejects egg contents an admin could not save', function (callable $corrupt $corrupt($egg); $file = UploadedFile::fake()->createWithContent('egg.json', (string) json_encode($egg)); - expect(fn () => service()->handle($file))->toThrow(InvalidFileUploadException::class, "The egg field \"{$field}\" is invalid"); + expect(fn () => EggImportData::fromFile($file))->toThrow(InvalidFileUploadException::class, "The egg field \"{$field}\" is invalid"); })->with([ 'author that is not an email' => [function (array &$egg): void { $egg['author'] = 'not-an-email'; @@ -65,10 +65,6 @@ test('rejects egg contents an admin could not save', function (callable $corrupt $egg['variables'][1]['env_variable'] = $egg['variables'][0]['env_variable']; }, 'variables.0.env_variable'], ]); -function service(): EggParserService -{ - return new EggParserService(); -} function fixture(string $path): UploadedFile { $absolutePath = base_path('database/Seeders/eggs/'.$path);