2023-05-04 20:29:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2023-06-05 10:07:55 +00:00
|
|
|
use App\Models\EnvironmentVariable as ModelsEnvironmentVariable;
|
2023-05-04 20:29:14 +00:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2023-05-05 08:51:58 +00:00
|
|
|
use Illuminate\Support\Str;
|
2023-05-04 20:29:14 +00:00
|
|
|
|
|
|
|
class EnvironmentVariable extends Model
|
|
|
|
{
|
2023-06-05 10:07:55 +00:00
|
|
|
protected static function booted()
|
|
|
|
{
|
|
|
|
static::created(function ($environment_variable) {
|
|
|
|
if (!$environment_variable->is_preview) {
|
|
|
|
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,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
protected $fillable = ['key', 'value', 'is_build_time', 'application_id', 'is_preview'];
|
2023-05-04 20:29:14 +00:00
|
|
|
protected $casts = [
|
2023-05-05 08:51:58 +00:00
|
|
|
"key" => 'string',
|
2023-05-04 20:29:14 +00:00
|
|
|
'value' => 'encrypted',
|
|
|
|
'is_build_time' => 'boolean',
|
|
|
|
];
|
|
|
|
private function get_environment_variables(string $environment_variable): string|null
|
|
|
|
{
|
2023-05-05 07:02:50 +00:00
|
|
|
// $team_id = session('currentTeam')->id;
|
2023-05-04 20:29:14 +00:00
|
|
|
if (str_contains(trim($environment_variable), '{{') && str_contains(trim($environment_variable), '}}')) {
|
|
|
|
$environment_variable = preg_replace('/\s+/', '', $environment_variable);
|
|
|
|
$environment_variable = str_replace('{{', '', $environment_variable);
|
|
|
|
$environment_variable = str_replace('}}', '', $environment_variable);
|
|
|
|
if (str_starts_with($environment_variable, 'global.')) {
|
|
|
|
$environment_variable = str_replace('global.', '', $environment_variable);
|
|
|
|
// $environment_variable = GlobalEnvironmentVariable::where('name', $environment_variable)->where('team_id', $team_id)->first()?->value;
|
|
|
|
return $environment_variable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return decrypt($environment_variable);
|
|
|
|
}
|
|
|
|
private function set_environment_variables(string $environment_variable): string|null
|
|
|
|
{
|
2023-05-05 08:51:58 +00:00
|
|
|
$environment_variable = trim($environment_variable);
|
2023-05-04 20:29:14 +00:00
|
|
|
if (!str_contains(trim($environment_variable), '{{') && !str_contains(trim($environment_variable), '}}')) {
|
|
|
|
return encrypt($environment_variable);
|
|
|
|
}
|
|
|
|
return $environment_variable;
|
|
|
|
}
|
|
|
|
protected function value(): Attribute
|
|
|
|
{
|
|
|
|
return Attribute::make(
|
|
|
|
get: fn (string $value) => $this->get_environment_variables($value),
|
|
|
|
set: fn (string $value) => $this->set_environment_variables($value),
|
|
|
|
);
|
|
|
|
}
|
2023-05-05 08:51:58 +00:00
|
|
|
protected function key(): Attribute
|
|
|
|
{
|
|
|
|
return Attribute::make(
|
|
|
|
set: fn (string $value) => Str::of($value)->trim(),
|
|
|
|
);
|
|
|
|
}
|
2023-05-04 20:29:14 +00:00
|
|
|
}
|