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-08-07 20:14:21 +00:00
|
|
|
protected $guarded = [];
|
2023-05-04 20:29:14 +00:00
|
|
|
protected $casts = [
|
2023-10-24 13:41:21 +00:00
|
|
|
'key' => 'string',
|
2023-05-04 20:29:14 +00:00
|
|
|
'value' => 'encrypted',
|
|
|
|
'is_build_time' => 'boolean',
|
|
|
|
];
|
2024-01-23 16:13:23 +00:00
|
|
|
protected $appends = ['real_value', 'is_shared'];
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-08-07 20:14:21 +00:00
|
|
|
protected static function booted()
|
|
|
|
{
|
|
|
|
static::created(function ($environment_variable) {
|
2023-08-08 09:51:36 +00:00
|
|
|
if ($environment_variable->application_id && !$environment_variable->is_preview) {
|
2023-09-11 20:29:47 +00:00
|
|
|
$found = ModelsEnvironmentVariable::where('key', $environment_variable->key)->where('application_id', $environment_variable->application_id)->where('is_preview', true)->first();
|
2023-10-24 13:41:21 +00:00
|
|
|
$application = Application::find($environment_variable->application_id);
|
|
|
|
if ($application->build_pack === 'dockerfile') {
|
|
|
|
return;
|
|
|
|
}
|
2023-09-08 14:16:59 +00: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-08 09:51:36 +00:00
|
|
|
}
|
2023-08-07 20:14:21 +00:00
|
|
|
});
|
|
|
|
}
|
2023-10-24 13:41:21 +00:00
|
|
|
public function service()
|
|
|
|
{
|
2023-09-20 13:42:41 +00:00
|
|
|
return $this->belongsTo(Service::class);
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
protected function value(): Attribute
|
|
|
|
{
|
|
|
|
return Attribute::make(
|
2023-09-20 13:42:41 +00:00
|
|
|
get: fn (?string $value = null) => $this->get_environment_variables($value),
|
|
|
|
set: fn (?string $value = null) => $this->set_environment_variables($value),
|
2023-08-08 09:51:36 +00:00
|
|
|
);
|
|
|
|
}
|
2024-01-24 14:54:55 +00:00
|
|
|
public function realValue(): Attribute
|
2024-01-23 16:13:23 +00:00
|
|
|
{
|
2024-01-24 14:54:55 +00:00
|
|
|
$resource = null;
|
|
|
|
if ($this->application_id) {
|
|
|
|
$resource = Application::find($this->application_id);
|
|
|
|
} else if ($this->service_id) {
|
|
|
|
$resource = Service::find($this->service_id);
|
|
|
|
} else if ($this->database_id) {
|
|
|
|
$resource = StandalonePostgresql::find($this->database_id);
|
|
|
|
if (!$resource) {
|
|
|
|
$resource = StandaloneMysql::find($this->database_id);
|
|
|
|
if (!$resource) {
|
|
|
|
$resource = StandaloneRedis::find($this->database_id);
|
|
|
|
if (!$resource) {
|
|
|
|
$resource = StandaloneMongodb::find($this->database_id);
|
|
|
|
if (!$resource) {
|
|
|
|
$resource = StandaloneMariadb::find($this->database_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-23 16:13:23 +00:00
|
|
|
return Attribute::make(
|
2024-01-24 14:54:55 +00:00
|
|
|
get: function () use ($resource) {
|
|
|
|
return $this->get_real_environment_variables($this->value, $resource);
|
|
|
|
}
|
2024-01-23 16:13:23 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
protected function isShared(): Attribute
|
|
|
|
{
|
|
|
|
return Attribute::make(
|
|
|
|
get: function () {
|
|
|
|
$type = str($this->value)->after("{{")->before(".")->value;
|
|
|
|
if (str($this->value)->startsWith('{{' . $type) && str($this->value)->endsWith('}}')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2024-01-24 14:54:55 +00:00
|
|
|
private function get_real_environment_variables(?string $environment_variable = null, $resource = null): string|null
|
2023-05-04 20:29:14 +00:00
|
|
|
{
|
2023-09-21 19:30:13 +00:00
|
|
|
if (!$environment_variable) {
|
|
|
|
return null;
|
|
|
|
}
|
2024-01-23 16:13:23 +00:00
|
|
|
$environment_variable = trim($environment_variable);
|
|
|
|
$type = str($environment_variable)->after("{{")->before(".")->value;
|
|
|
|
if (str($environment_variable)->startsWith("{{" . $type) && str($environment_variable)->endsWith('}}')) {
|
|
|
|
$variable = Str::after($environment_variable, "{$type}.");
|
2023-09-11 20:29:47 +00:00
|
|
|
$variable = Str::before($variable, '}}');
|
|
|
|
$variable = Str::of($variable)->trim()->value;
|
2024-01-31 12:40:15 +00:00
|
|
|
if (!collect(['team', 'project', 'environment'])->contains($type)) {
|
|
|
|
return $variable;
|
|
|
|
}
|
2024-01-24 14:54:55 +00:00
|
|
|
if ($type === 'environment') {
|
|
|
|
$id = $resource->environment->id;
|
|
|
|
} else if ($type === 'project') {
|
|
|
|
$id = $resource->environment->project->id;
|
|
|
|
} else {
|
2024-01-24 14:56:43 +00:00
|
|
|
$id = $resource->team()->id;
|
2024-01-24 14:54:55 +00:00
|
|
|
}
|
2024-01-24 14:56:43 +00:00
|
|
|
$environment_variable_found = SharedEnvironmentVariable::where("type", $type)->where('key', $variable)->where('team_id', $resource->team()->id)->where("{$type}_id", $id)->first();
|
2024-01-23 16:13:23 +00:00
|
|
|
if ($environment_variable_found) {
|
|
|
|
return $environment_variable_found->value;
|
|
|
|
}
|
2023-05-04 20:29:14 +00:00
|
|
|
}
|
2023-09-11 20:29:47 +00:00
|
|
|
return $environment_variable;
|
2023-05-04 20:29:14 +00:00
|
|
|
}
|
2024-01-23 16:13:23 +00:00
|
|
|
private function get_environment_variables(?string $environment_variable = null): string|null
|
|
|
|
{
|
|
|
|
if (!$environment_variable) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return trim(decrypt($environment_variable));
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-09-20 13:42:41 +00:00
|
|
|
private function set_environment_variables(?string $environment_variable = null): string|null
|
2023-05-04 20:29:14 +00:00
|
|
|
{
|
2023-09-20 13:42:41 +00:00
|
|
|
if (is_null($environment_variable) && $environment_variable == '') {
|
|
|
|
return null;
|
|
|
|
}
|
2023-05-05 08:51:58 +00:00
|
|
|
$environment_variable = trim($environment_variable);
|
2024-01-23 16:13:23 +00:00
|
|
|
$type = str($environment_variable)->after("{{")->before(".")->value;
|
|
|
|
if (str($environment_variable)->startsWith("{{" . $type) && str($environment_variable)->endsWith('}}')) {
|
|
|
|
return encrypt((string) str($environment_variable)->replace(' ', ''));
|
|
|
|
}
|
2023-09-11 20:29:47 +00:00
|
|
|
return encrypt($environment_variable);
|
2023-05-04 20:29:14 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-05-05 08:51:58 +00:00
|
|
|
protected function key(): Attribute
|
|
|
|
{
|
|
|
|
return Attribute::make(
|
2023-08-11 18:48:52 +00:00
|
|
|
set: fn (string $value) => Str::of($value)->trim(),
|
2023-05-05 08:51:58 +00:00
|
|
|
);
|
|
|
|
}
|
2023-05-04 20:29:14 +00:00
|
|
|
}
|