Files

59 lines
2.2 KiB
PHP
Raw Permalink Normal View History

2026-10-05 18:10:18 -04:00
<?php
declare(strict_types=1);
namespace Pterodactyl\Actions\Eggs\Variables;
2026-10-11 23:29:38 -04:00
use Illuminate\Support\Arr;
2026-10-11 08:19:22 -04:00
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Contracts\Eggs\CreatesEggVariables;
use Pterodactyl\Models\Egg;
use Pterodactyl\Models\EggVariable;
2026-10-11 08:19:22 -04:00
use Pterodactyl\Rules\UnreservedEnvironmentVariable;
use Pterodactyl\Rules\ValidRuleSet;
2026-10-05 18:10:18 -04:00
final class CreateEggVariable implements CreatesEggVariables
{
/**
2026-10-11 23:50:53 -04:00
* Pass the egg to hold the environment variable name unique among its variables, and the
* variable being updated so its own name is not taken for a duplicate.
2026-10-05 18:10:18 -04:00
*
2026-10-11 08:19:22 -04:00
* @return NormalizedValidationRules
2026-10-05 18:10:18 -04:00
*/
2026-10-11 08:19:22 -04:00
public static function rules(?Egg $egg = null, ?EggVariable $ignore = null): array
2026-10-05 18:10:18 -04:00
{
2026-10-11 08:19:22 -04:00
$environmentVariable = ['required', 'regex:/^[\w]{1,191}$/', new UnreservedEnvironmentVariable];
if ($egg instanceof Egg) {
$environmentVariable[] = Rule::unique(EggVariable::class, 'env_variable')->where('egg_id', $egg->id)->ignore($ignore);
}
return [
'name' => ['required', 'string', 'min:1', 'max:191'],
'description' => ['sometimes', 'nullable', 'string'],
'env_variable' => $environmentVariable,
'rules' => ['bail', 'required', 'string', new ValidRuleSet],
'default_value' => ['present', 'string'],
'options' => ['sometimes', 'required', 'array', 'list'],
'options.*' => ['string'],
];
}
2026-10-11 23:29:38 -04:00
public function create(Egg $egg, array $input): EggVariable
2026-10-11 08:19:22 -04:00
{
2026-10-11 23:29:38 -04:00
$validated = Validator::validate($input, self::rules($egg));
$options = Arr::array($validated, 'options', []);
2026-10-11 08:19:22 -04:00
2026-10-11 23:29:38 -04:00
// A variable given no description gets a blank one, and the options set whether users
// can see and edit it; any other option is ignored.
$variable = $egg->variables()->create([
...Arr::except($validated, 'options'),
'description' => $validated['description'] ?? '',
2026-10-11 08:19:22 -04:00
'user_viewable' => in_array('user_viewable', $options, true),
'user_editable' => in_array('user_editable', $options, true),
2026-10-11 23:29:38 -04:00
]);
2026-10-05 18:10:18 -04:00
return $variable->refresh();
}
}