coolify/app/Models/EnvironmentVariable.php

86 lines
3.2 KiB
PHP
Raw Normal View History

2023-05-04 22:29:14 +02:00
<?php
namespace App\Models;
2023-06-05 12:07:55 +02:00
use App\Models\EnvironmentVariable as ModelsEnvironmentVariable;
2023-05-04 22:29:14 +02:00
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
2023-05-04 22:29:14 +02:00
class EnvironmentVariable extends Model
{
2023-08-07 22:14:21 +02:00
protected $guarded = [];
2023-05-04 22:29:14 +02:00
protected $casts = [
2023-10-24 15:41:21 +02:00
'key' => 'string',
2023-05-04 22:29:14 +02:00
'value' => 'encrypted',
'is_build_time' => 'boolean',
];
2023-08-07 22:14:21 +02:00
protected static function booted()
{
static::created(function ($environment_variable) {
if ($environment_variable->application_id && !$environment_variable->is_preview) {
2023-09-11 22:29:47 +02:00
$found = ModelsEnvironmentVariable::where('key', $environment_variable->key)->where('application_id', $environment_variable->application_id)->where('is_preview', true)->first();
2023-10-24 15:41:21 +02:00
$application = Application::find($environment_variable->application_id);
if ($application->build_pack === 'dockerfile') {
return;
}
2023-09-08 16:16:59 +02:00
if (!$found) {
ModelsEnvironmentVariable::create([
'key' => $environment_variable->key,
'value' => $environment_variable->value,
'is_build_time' => $environment_variable->is_build_time,
'application_id' => $environment_variable->application_id,
'is_preview' => true,
]);
}
}
2023-08-07 22:14:21 +02:00
});
}
2023-10-24 15:41:21 +02:00
public function service()
{
2023-09-20 15:42:41 +02:00
return $this->belongsTo(Service::class);
}
protected function value(): Attribute
{
return Attribute::make(
2023-09-20 15:42:41 +02:00
get: fn (?string $value = null) => $this->get_environment_variables($value),
set: fn (?string $value = null) => $this->set_environment_variables($value),
);
}
2023-09-21 21:30:13 +02:00
private function get_environment_variables(?string $environment_variable = null): string|null
2023-05-04 22:29:14 +02:00
{
2023-08-22 17:44:49 +02:00
// $team_id = currentTeam()->id;
2023-09-21 21:30:13 +02:00
if (!$environment_variable) {
return null;
}
2023-09-11 22:29:47 +02:00
$environment_variable = trim(decrypt($environment_variable));
if (Str::startsWith($environment_variable, '{{') && Str::endsWith($environment_variable, '}}') && Str::contains($environment_variable, 'global.')) {
$variable = Str::after($environment_variable, 'global.');
$variable = Str::before($variable, '}}');
$variable = Str::of($variable)->trim()->value;
2023-10-24 15:41:21 +02:00
// $environment_variable = GlobalEnvironmentVariable::where('name', $environment_variable)->where('team_id', $team_id)->first()?->value;
ray('global env variable');
return $environment_variable;
2023-05-04 22:29:14 +02:00
}
2023-09-11 22:29:47 +02:00
return $environment_variable;
2023-05-04 22:29:14 +02:00
}
2023-09-20 15:42:41 +02:00
private function set_environment_variables(?string $environment_variable = null): string|null
2023-05-04 22:29:14 +02:00
{
2023-09-20 15:42:41 +02:00
if (is_null($environment_variable) && $environment_variable == '') {
return null;
}
$environment_variable = trim($environment_variable);
2023-09-11 22:29:47 +02:00
return encrypt($environment_variable);
2023-05-04 22:29:14 +02:00
}
protected function key(): Attribute
{
return Attribute::make(
2023-08-11 20:48:52 +02:00
set: fn (string $value) => Str::of($value)->trim(),
);
}
2023-05-04 22:29:14 +02:00
}