'integer', 'type_data' => 'json', 'port' => 'integer', 'progress' => 'integer', 'aliases' => 'array', 'source_control_id' => 'integer', ]; public static function boot(): void { parent::boot(); static::deleting(function (Site $site) { $site->queues()->delete(); $site->ssls()->delete(); $site->deployments()->delete(); $site->deploymentScript()->delete(); }); static::created(function (Site $site) { $site->deploymentScript()->create([ 'name' => 'default', 'content' => '', ]); }); } public function server(): BelongsTo { return $this->belongsTo(Server::class); } public function logs(): HasMany { return $this->hasMany(ServerLog::class); } public function deployments(): HasMany { return $this->hasMany(Deployment::class); } public function gitHook(): HasOne { return $this->hasOne(GitHook::class); } public function deploymentScript(): HasOne { return $this->hasOne(DeploymentScript::class); } public function queues(): HasMany { return $this->hasMany(Queue::class); } public function ssls(): HasMany { return $this->hasMany(Ssl::class); } /** * @throws SourceControlIsNotConnected */ public function sourceControl(): SourceControl|HasOne|null|Model { $sourceControl = null; if (! $this->source_control && ! $this->source_control_id) { return null; } if ($this->source_control) { $sourceControl = SourceControl::query()->where('provider', $this->source_control)->first(); } if ($this->source_control_id) { $sourceControl = SourceControl::query()->find($this->source_control_id); } if (! $sourceControl) { throw new SourceControlIsNotConnected($this->source_control); } return $sourceControl; } /** * @throws SourceControlIsNotConnected */ public function getFullRepositoryUrl() { return $this->sourceControl()->provider()->fullRepoUrl($this->repository, $this->getSshKeyName()); } public function getAliasesString(): string { if (count($this->aliases) > 0) { return implode(' ', $this->aliases); } return ''; } public function type(): SiteType { $typeClass = config('core.site_types_class.'.$this->type); return new $typeClass($this); } public function php(): ?Service { if ($this->php_version) { return $this->server->php($this->php_version); } return null; } public function changePHPVersion($version): void { $this->server->webserver()->handler()->changePHPVersion($this, $version); $this->php_version = $version; $this->save(); } public function activeSsl(): HasOne { return $this->hasOne(Ssl::class) ->where('expires_at', '>=', now()) ->orderByDesc('id'); } public function getUrl(): string { if ($this->activeSsl) { return 'https://'.$this->domain; } return 'http://'.$this->domain; } public function getWebDirectoryPath(): string { if ($this->web_directory) { return $this->path.'/'.$this->web_directory; } return $this->path; } /** * @throws SourceControlIsNotConnected */ public function enableAutoDeployment(): void { if ($this->gitHook) { return; } if (! $this->sourceControl()?->getRepo($this->repository)) { throw new SourceControlIsNotConnected($this->source_control); } $gitHook = new GitHook([ 'site_id' => $this->id, 'source_control_id' => $this->sourceControl()->id, 'secret' => Str::uuid()->toString(), 'actions' => ['deploy'], 'events' => ['push'], ]); $gitHook->save(); $gitHook->deployHook(); } /** * @throws SourceControlIsNotConnected */ public function disableAutoDeployment(): void { if (! $this->sourceControl()?->getRepo($this->repository)) { throw new SourceControlIsNotConnected($this->source_control); } $this->gitHook?->destroyHook(); } public function isAutoDeployment(): bool { return (bool) $this->gitHook; } public function getSshKeyName(): string { return str('site_'.$this->id)->toString(); } public function hasFeature(string $feature): bool { return in_array($feature, $this->type()->supportedFeatures()); } public function getEnv(): string { return $this->server->os()->readFile($this->path.'/.env'); } }