Skip to content

Commit

Permalink
Merge branch 'pelican-dev:main' into issue/545
Browse files Browse the repository at this point in the history
  • Loading branch information
RMartinOscar committed Aug 22, 2024
2 parents a6bf9cb + 20b06b7 commit 6d1e327
Show file tree
Hide file tree
Showing 29 changed files with 465 additions and 116 deletions.
1 change: 1 addition & 0 deletions app/Filament/Pages/Installer/PanelInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public function submit()
Artisan::call('migrate', [
'--force' => true,
'--seed' => true,
'--database' => $variables['DB_CONNECTION'],
]);

if (!$this->hasCompletedMigrations()) {
Expand Down
2 changes: 1 addition & 1 deletion app/Filament/Pages/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ private function mailSettings(): array
TextInput::make('MAILGUN_SECRET')
->label('Secret')
->required()
->default(env('MAIL_USERNAME', config('services.mailgun.secret'))),
->default(env('MAILGUN_SECRET', config('services.mailgun.secret'))),
TextInput::make('MAILGUN_ENDPOINT')
->label('Endpoint')
->required()
Expand Down
31 changes: 27 additions & 4 deletions app/Filament/Resources/EggResource/Pages/CreateEgg.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function form(Form $form): Form
->mutateRelationshipDataBeforeCreateUsing(function (array $data): array {
$data['default_value'] ??= '';
$data['description'] ??= '';
$data['rules'] ??= '';
$data['rules'] ??= [];
$data['user_viewable'] ??= '';
$data['user_editable'] ??= '';

Expand All @@ -143,7 +143,7 @@ public function form(Form $form): Form
->mutateRelationshipDataBeforeSaveUsing(function (array $data): array {
$data['default_value'] ??= '';
$data['description'] ??= '';
$data['rules'] ??= '';
$data['rules'] ??= [];
$data['user_viewable'] ??= '';
$data['user_editable'] ??= '';

Expand Down Expand Up @@ -173,7 +173,30 @@ public function form(Form $form): Form
Checkbox::make('user_viewable')->label('Viewable'),
Checkbox::make('user_editable')->label('Editable'),
]),
Textarea::make('rules')->columnSpanFull(),
TagsInput::make('rules')
->columnSpanFull()
->placeholder('Add Rule')
->reorderable()
->suggestions([
'required',
'nullable',
'string',
'integer',
'numeric',
'boolean',
'alpha',
'alpha_dash',
'alpha_num',
'url',
'email',
'regex:',
'min:',
'max:',
'between:',
'between:1024,65535',
'in:',
'in:true,false',
]),
]),
]),
Tab::make('Install Script')
Expand All @@ -187,7 +210,7 @@ public function form(Form $form): Form
TextInput::make('script_container')
->required()
->maxLength(255)
->default('alpine:3.4'),
->default('ghcr.io/pelican-eggs/installers:debian'),

Select::make('script_entry')
->selectablePlaceholder(false)
Expand Down
29 changes: 26 additions & 3 deletions app/Filament/Resources/EggResource/Pages/EditEgg.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function form(Form $form): Form
->mutateRelationshipDataBeforeCreateUsing(function (array $data): array {
$data['default_value'] ??= '';
$data['description'] ??= '';
$data['rules'] ??= '';
$data['rules'] ??= [];
$data['user_viewable'] ??= '';
$data['user_editable'] ??= '';

Expand All @@ -153,7 +153,7 @@ public function form(Form $form): Form
->mutateRelationshipDataBeforeSaveUsing(function (array $data): array {
$data['default_value'] ??= '';
$data['description'] ??= '';
$data['rules'] ??= '';
$data['rules'] ??= [];
$data['user_viewable'] ??= '';
$data['user_editable'] ??= '';

Expand Down Expand Up @@ -183,7 +183,30 @@ public function form(Form $form): Form
Checkbox::make('user_viewable')->label('Viewable'),
Checkbox::make('user_editable')->label('Editable'),
]),
TextInput::make('rules')->columnSpanFull(),
TagsInput::make('rules')
->columnSpanFull()
->placeholder('Add Rule')
->reorderable()
->suggestions([
'required',
'nullable',
'string',
'integer',
'numeric',
'boolean',
'alpha',
'alpha_dash',
'alpha_num',
'url',
'email',
'regex:',
'min:',
'max:',
'between:',
'between:1024,65535',
'in:',
'in:true,false',
]),
]),
]),
Tab::make('Install Script')
Expand Down
9 changes: 4 additions & 5 deletions app/Filament/Resources/ServerResource/Pages/CreateServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,7 @@ public function form(Form $form): Form

$text = Forms\Components\TextInput::make('variable_value')
->hidden($this->shouldHideComponent(...))
->maxLength(255)
->required(fn (Forms\Get $get) => in_array('required', explode('|', $get('rules'))))
->required(fn (Forms\Get $get) => in_array('required', $get('rules')))
->rules(
fn (Forms\Get $get): Closure => function (string $attribute, $value, Closure $fail) use ($get) {
$validator = Validator::make(['validatorkey' => $value], [
Expand All @@ -471,7 +470,7 @@ public function form(Form $form): Form
->live(onBlur: true)
->hintIcon('tabler-code')
->label(fn (Forms\Get $get) => $get('name'))
->hintIconTooltip(fn (Forms\Get $get) => $get('rules'))
->hintIconTooltip(fn (Forms\Get $get) => implode('|', $get('rules')))
->prefix(fn (Forms\Get $get) => '{{' . $get('env_variable') . '}}')
->helperText(fn (Forms\Get $get) => empty($get('description')) ? '' : $get('description'))
->afterStateUpdated(function (Forms\Set $set, Forms\Get $get, $state) {
Expand Down Expand Up @@ -808,7 +807,7 @@ protected function handleRecordCreation(array $data): Model

private function shouldHideComponent(Forms\Get $get, Forms\Components\Component $component): bool
{
$containsRuleIn = str($get('rules'))->explode('|')->reduce(
$containsRuleIn = collect($get('rules'))->reduce(
fn ($result, $value) => $result === true && !str($value)->startsWith('in:'), true
);

Expand All @@ -825,7 +824,7 @@ private function shouldHideComponent(Forms\Get $get, Forms\Components\Component

private function getSelectOptionsFromRules(Forms\Get $get): array
{
$inRule = str($get('rules'))->explode('|')->reduce(
$inRule = collect($get('rules'))->reduce(
fn ($result, $value) => str($value)->startsWith('in:') ? $value : $result, ''
);

Expand Down
20 changes: 8 additions & 12 deletions app/Filament/Resources/ServerResource/Pages/EditServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ public function form(Form $form): Form

$text = Forms\Components\TextInput::make('variable_value')
->hidden($this->shouldHideComponent(...))
->required(fn (ServerVariable $serverVariable) => in_array('required', explode('|', $serverVariable->variable->rules)))
->required(fn (ServerVariable $serverVariable) => $serverVariable->variable->getRequiredAttribute())
->rules([
fn (ServerVariable $serverVariable): Closure => function (string $attribute, $value, Closure $fail) use ($serverVariable) {
$validator = Validator::make(['validatorkey' => $value], [
Expand All @@ -516,7 +516,7 @@ public function form(Form $form): Form
->live(onBlur: true)
->hintIcon('tabler-code')
->label(fn (ServerVariable $serverVariable) => $serverVariable->variable->name)
->hintIconTooltip(fn (ServerVariable $serverVariable) => $serverVariable->variable->rules)
->hintIconTooltip(fn (ServerVariable $serverVariable) => implode('|', $serverVariable->variable->rules))
->prefix(fn (ServerVariable $serverVariable) => '{{' . $serverVariable->variable->env_variable . '}}')
->helperText(fn (ServerVariable $serverVariable) => empty($serverVariable->variable->description) ? '' : $serverVariable->variable->description);
}
Expand Down Expand Up @@ -757,28 +757,24 @@ public function getRelationManagers(): array
];
}

private function shouldHideComponent(Forms\Get $get, Forms\Components\Component $component): bool
private function shouldHideComponent(ServerVariable $serverVariable, Forms\Components\Component $component): bool
{
$containsRuleIn = str($get('rules'))->explode('|')->reduce(
fn ($result, $value) => $result === true && !str($value)->startsWith('in:'), true
);
$containsRuleIn = array_first($serverVariable->variable->rules, fn ($value) => str($value)->startsWith('in:'), false);

if ($component instanceof Forms\Components\Select) {
return $containsRuleIn;
return !$containsRuleIn;
}

if ($component instanceof Forms\Components\TextInput) {
return !$containsRuleIn;
return $containsRuleIn;
}

throw new \Exception('Component type not supported: ' . $component::class);
}

private function getSelectOptionsFromRules(Forms\Get $get): array
private function getSelectOptionsFromRules(ServerVariable $serverVariable): array
{
$inRule = str($get('rules'))->explode('|')->reduce(
fn ($result, $value) => str($value)->startsWith('in:') ? $value : $result, ''
);
$inRule = array_first($serverVariable->variable->rules, fn ($value) => str($value)->startsWith('in:'));

return str($inRule)
->after('in:')
Expand Down
9 changes: 6 additions & 3 deletions app/Models/EggVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @property string $default_value
* @property bool $user_viewable
* @property bool $user_editable
* @property string $rules
* @property array $rules
* @property \Carbon\CarbonImmutable $created_at
* @property \Carbon\CarbonImmutable $updated_at
* @property bool $required
Expand Down Expand Up @@ -58,12 +58,14 @@ class EggVariable extends Model
'default_value' => 'string',
'user_viewable' => 'boolean',
'user_editable' => 'boolean',
'rules' => 'string',
'rules' => 'array',
'rules.*' => 'string',
];

protected $attributes = [
'user_editable' => 0,
'user_viewable' => 0,
'rules' => '[]',
];

protected function casts(): array
Expand All @@ -72,14 +74,15 @@ protected function casts(): array
'egg_id' => 'integer',
'user_viewable' => 'bool',
'user_editable' => 'bool',
'rules' => 'array',
'created_at' => 'immutable_datetime',
'updated_at' => 'immutable_datetime',
];
}

public function getRequiredAttribute(): bool
{
return in_array('required', explode('|', $this->rules));
return in_array('required', $this->rules);
}

public function egg(): HasOne
Expand Down
2 changes: 2 additions & 0 deletions app/Services/Eggs/Sharing/EggImporterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public function fromFile(UploadedFile $file, Egg $egg = null): Egg
// Update existing variables or create new ones.
foreach ($parsed['variables'] ?? [] as $variable) {
EggVariable::unguarded(function () use ($egg, $variable) {
$variable['rules'] = is_array($variable['rules']) ? $variable['rules'] : explode('|', $variable['rules']);

$egg->variables()->updateOrCreate([
'env_variable' => $variable['env_variable'],
], Collection::make($variable)->except(['egg_id', 'env_variable'])->toArray());
Expand Down
4 changes: 2 additions & 2 deletions app/Services/Eggs/Variables/VariableCreationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function handle(int $egg, array $data): EggVariable
throw new ReservedVariableNameException(sprintf('Cannot use the protected name %s for this environment variable.', array_get($data, 'env_variable')));
}

if (!empty($data['rules'] ?? '')) {
if (!empty($data['rules'] ?? [])) {
$this->validateRules($data['rules']);
}

Expand All @@ -55,7 +55,7 @@ public function handle(int $egg, array $data): EggVariable
'default_value' => $data['default_value'] ?? '',
'user_viewable' => in_array('user_viewable', $options),
'user_editable' => in_array('user_editable', $options),
'rules' => $data['rules'] ?? '',
'rules' => $data['rules'] ?? [],
]);

return $eggVariable;
Expand Down
11 changes: 3 additions & 8 deletions app/Services/Eggs/Variables/VariableUpdateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Services\Eggs\Variables;

use Illuminate\Support\Str;
use App\Models\EggVariable;
use App\Exceptions\DisplayException;
use App\Traits\Services\ValidatesValidationRules;
Expand Down Expand Up @@ -54,12 +53,8 @@ public function handle(EggVariable $variable, array $data): mixed
}
}

if (!empty($data['rules'] ?? '')) {
$this->validateRules(
(is_string($data['rules']) && Str::contains($data['rules'], ';;'))
? explode(';;', $data['rules'])
: $data['rules']
);
if (!empty($data['rules'] ?? [])) {
$this->validateRules($data['rules']);
}

$options = array_get($data, 'options') ?? [];
Expand All @@ -71,7 +66,7 @@ public function handle(EggVariable $variable, array $data): mixed
'default_value' => $data['default_value'] ?? '',
'user_viewable' => in_array('user_viewable', $options),
'user_editable' => in_array('user_editable', $options),
'rules' => $data['rules'] ?? '',
'rules' => $data['rules'] ?? [],
]);
}
}
2 changes: 1 addition & 1 deletion app/Transformers/Api/Client/EggVariableTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function transform(EggVariable $variable): array
'default_value' => $variable->default_value,
'server_value' => $variable->server_value,
'is_editable' => $variable->user_editable,
'rules' => $variable->rules,
'rules' => implode('|', $variable->rules),
];
}
}
13 changes: 10 additions & 3 deletions database/Seeders/eggs/minecraft/egg-bungeecord.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"version": "PTDL_v2",
"update_url": "https:\/\/github.com\/pelican-dev\/panel\/raw\/main\/database\/Seeders\/eggs\/minecraft\/egg-bungeecord.json"
},
"exported_at": "2024-07-03T14:33:51+00:00",
"exported_at": "2024-07-25T12:03:12+00:00",
"name": "Bungeecord",
"author": "panel@example.com",
"uuid": "9e6b409e-4028-4947-aea8-50a2c404c271",
Expand Down Expand Up @@ -44,7 +44,11 @@
"default_value": "latest",
"user_viewable": true,
"user_editable": true,
"rules": "required|alpha_num|between:1,6",
"rules": [
"required",
"alpha_num",
"between:1,6"
],
"sort": null,
"field_type": "text"
},
Expand All @@ -55,7 +59,10 @@
"default_value": "bungeecord.jar",
"user_viewable": true,
"user_editable": true,
"rules": "required|regex:\/^([\\w\\d._-]+)(\\.jar)$\/",
"rules": [
"required",
"regex:\/^([\\w\\d._-]+)(\\.jar)$\/"
],
"sort": null,
"field_type": "text"
}
Expand Down
Loading

0 comments on commit 6d1e327

Please sign in to comment.