$variables * @property string $status * @property Carbon $created_at * @property Carbon $updated_at * @property Script $script * @property ?ServerLog $serverLog * @property ?Server $server */ class ScriptExecution extends AbstractModel { /** @use HasFactory<\Database\Factories\ScriptExecutionFactory> */ use HasFactory; protected $fillable = [ 'script_id', 'server_id', 'server_log_id', 'user', 'variables', 'status', ]; protected $casts = [ 'script_id' => 'integer', 'server_id' => 'integer', 'server_log_id' => 'integer', 'variables' => 'array', ]; /** * @var array */ public static array $statusColors = [ ScriptExecutionStatus::EXECUTING => 'warning', ScriptExecutionStatus::COMPLETED => 'success', ScriptExecutionStatus::FAILED => 'danger', ]; /** * @return BelongsTo */ public function script(): BelongsTo { return $this->belongsTo(Script::class); } public function getContent(): string { $content = $this->script->content; foreach ($this->variables as $variable => $value) { if (is_string($value) && ($value !== '' && $value !== '0')) { $content = str_replace('${'.$variable.'}', $value, $content); } } return $content; } /** * @return BelongsTo */ public function serverLog(): BelongsTo { return $this->belongsTo(ServerLog::class); } /** * @return BelongsTo */ public function server(): BelongsTo { return $this->belongsTo(Server::class); } public function getServer(): ?Server { if ($this->server_id) { return $this->server; } if ($this->server_log_id) { return $this->serverLog?->server; } return null; } }