2023-03-27 08:44:31 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2023-03-30 07:47:04 +00:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
2023-03-29 10:27:02 +00:00
|
|
|
use Spatie\Activitylog\Models\Activity;
|
2023-05-04 20:29:14 +00:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2023-03-29 10:27:02 +00:00
|
|
|
|
2023-03-27 08:44:31 +00:00
|
|
|
class Application extends BaseModel
|
|
|
|
{
|
2023-03-30 09:09:39 +00:00
|
|
|
protected static function booted()
|
|
|
|
{
|
|
|
|
static::created(function ($application) {
|
|
|
|
ApplicationSetting::create([
|
|
|
|
'application_id' => $application->id,
|
|
|
|
]);
|
|
|
|
});
|
2023-04-26 08:02:19 +00:00
|
|
|
static::deleting(function ($application) {
|
|
|
|
$application->settings()->delete();
|
|
|
|
$application->persistentStorages()->delete();
|
|
|
|
});
|
2023-03-30 09:09:39 +00:00
|
|
|
}
|
|
|
|
|
2023-05-04 13:45:53 +00:00
|
|
|
|
2023-04-25 12:43:35 +00:00
|
|
|
protected $fillable = [
|
|
|
|
'name',
|
|
|
|
'description',
|
|
|
|
'git_repository',
|
|
|
|
'git_branch',
|
|
|
|
'build_pack',
|
|
|
|
'environment_id',
|
|
|
|
'destination_id',
|
|
|
|
'destination_type',
|
|
|
|
'source_id',
|
|
|
|
'source_type',
|
|
|
|
'ports_mappings',
|
|
|
|
'ports_exposes',
|
2023-04-26 11:01:09 +00:00
|
|
|
'publish_directory',
|
2023-04-25 12:43:35 +00:00
|
|
|
];
|
2023-04-26 11:01:09 +00:00
|
|
|
public function publishDirectory(): Attribute
|
|
|
|
{
|
|
|
|
return Attribute::make(
|
|
|
|
set: fn ($value) => $value ? '/' . ltrim($value, '/') : null,
|
|
|
|
);
|
|
|
|
}
|
2023-05-08 10:22:45 +00:00
|
|
|
public function gitLocation(): Attribute
|
|
|
|
{
|
|
|
|
return Attribute::make(
|
|
|
|
get: fn () => is_null($this->git_commit_sha)
|
|
|
|
? "{$this->source->html_url}/{$this->git_repository}/tree/{$this->git_branch}"
|
|
|
|
: "{$this->source->html_url}/{$this->git_repository}/tree/{$this->git_commit_sha}"
|
|
|
|
|
|
|
|
);
|
|
|
|
}
|
2023-04-26 11:01:09 +00:00
|
|
|
public function baseDirectory(): Attribute
|
|
|
|
{
|
|
|
|
return Attribute::make(
|
|
|
|
set: fn ($value) => '/' . ltrim($value, '/'),
|
|
|
|
);
|
|
|
|
}
|
2023-04-26 12:29:33 +00:00
|
|
|
public function portsMappings(): Attribute
|
|
|
|
{
|
|
|
|
return Attribute::make(
|
|
|
|
set: fn ($value) => $value === "" ? null : $value,
|
|
|
|
);
|
|
|
|
}
|
2023-04-24 11:25:02 +00:00
|
|
|
public function portsMappingsArray(): Attribute
|
2023-03-30 07:47:04 +00:00
|
|
|
{
|
|
|
|
return Attribute::make(
|
2023-04-24 11:25:02 +00:00
|
|
|
get: fn () =>
|
|
|
|
is_null($this->ports_mappings)
|
2023-03-30 07:47:04 +00:00
|
|
|
? []
|
2023-04-26 12:29:33 +00:00
|
|
|
: explode(',', $this->ports_mappings),
|
2023-03-30 07:47:04 +00:00
|
|
|
|
|
|
|
);
|
|
|
|
}
|
2023-04-24 11:25:02 +00:00
|
|
|
public function portsExposesArray(): Attribute
|
2023-03-30 07:47:04 +00:00
|
|
|
{
|
|
|
|
return Attribute::make(
|
2023-04-24 11:25:02 +00:00
|
|
|
get: fn () =>
|
|
|
|
is_null($this->ports_exposes)
|
2023-03-30 07:47:04 +00:00
|
|
|
? []
|
2023-04-24 11:25:02 +00:00
|
|
|
: explode(',', $this->ports_exposes)
|
2023-03-30 07:47:04 +00:00
|
|
|
);
|
|
|
|
}
|
2023-05-04 20:29:14 +00:00
|
|
|
public function environment_variables(): HasMany
|
|
|
|
{
|
|
|
|
return $this->hasMany(EnvironmentVariable::class);
|
|
|
|
}
|
2023-05-05 12:48:40 +00:00
|
|
|
public function runtime_environment_variables(): HasMany
|
|
|
|
{
|
|
|
|
return $this->hasMany(EnvironmentVariable::class)->where('key', 'not like', 'NIXPACKS_%');
|
|
|
|
}
|
2023-05-05 07:02:50 +00:00
|
|
|
public function build_environment_variables(): HasMany
|
|
|
|
{
|
2023-05-05 12:48:40 +00:00
|
|
|
return $this->hasMany(EnvironmentVariable::class)->where('is_build_time', true)->where('key', 'not like', 'NIXPACKS_%');
|
2023-05-05 07:02:50 +00:00
|
|
|
}
|
2023-05-05 08:51:58 +00:00
|
|
|
public function nixpacks_environment_variables(): HasMany
|
|
|
|
{
|
|
|
|
return $this->hasMany(EnvironmentVariable::class)->where('key', 'like', 'NIXPACKS_%');
|
|
|
|
}
|
2023-04-26 12:29:33 +00:00
|
|
|
public function environment()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Environment::class);
|
|
|
|
}
|
|
|
|
public function settings()
|
|
|
|
{
|
|
|
|
return $this->hasOne(ApplicationSetting::class);
|
|
|
|
}
|
|
|
|
public function destination()
|
|
|
|
{
|
|
|
|
return $this->morphTo();
|
|
|
|
}
|
|
|
|
public function source()
|
|
|
|
{
|
|
|
|
return $this->morphTo();
|
|
|
|
}
|
|
|
|
public function persistentStorages()
|
|
|
|
{
|
|
|
|
return $this->morphMany(LocalPersistentVolume::class, 'resource');
|
|
|
|
}
|
|
|
|
|
2023-03-29 10:52:22 +00:00
|
|
|
public function deployments()
|
|
|
|
{
|
2023-05-03 06:51:03 +00:00
|
|
|
return Activity::where('subject_id', $this->id)->where('properties->type', '=', 'deployment')->orderBy('created_at', 'desc')->get();
|
2023-03-29 10:52:22 +00:00
|
|
|
}
|
2023-03-29 10:27:02 +00:00
|
|
|
public function get_deployment(string $deployment_uuid)
|
2023-03-28 13:47:37 +00:00
|
|
|
{
|
2023-05-03 06:51:03 +00:00
|
|
|
return Activity::where('subject_id', $this->id)->where('properties->type_uuid', '=', $deployment_uuid)->first();
|
2023-03-28 13:47:37 +00:00
|
|
|
}
|
2023-03-27 08:44:31 +00:00
|
|
|
}
|