Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

6.3 KB
Raw Blame
<?php

namespace App\Livewire\SharedVariables\Environment;

use App\Livewire\Concerns\RemembersCanvasView;
use App\Models\Application;
use App\Models\Project;
use App\Models\SharedEnvironmentVariable;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\DB;
use Livewire\Component;

class Show extends Component
{
    use AuthorizesRequests;
    use RemembersCanvasView;

    public Project $project;

    public Application $application;

    public $environment;

    public array $parameters;

    public string $view = 'normal';

    public ?string $variables = null;

    protected $listeners = ['refreshEnvs' => 'refreshEnvs', 'saveKey', 'environmentVariableDeleted' => 'refreshEnvs'];

    public function saveKey($data)
    {
        try {
            $this->authorize('update', $this->environment);

            $found = $this->environment->environment_variables()->where('key', $data['key'])->first();
            if ($found) {
                throw new \Exception('Variable already exists.');
            }
            $this->environment->environment_variables()->create([
                'key' => $data['key'],
                'value' => $data['value'],
                'is_multiline' => $data['is_multiline'],
                'is_literal' => $data['is_literal'],
                'comment' => $data['comment'] ?? null,
                'type' => 'environment',
                'team_id' => $this->project->team_id,
            ]);
            $this->environment->refresh();
            $this->getDevView();
        } catch (\Throwable $e) {
            return handleError($e, $this);
        }
    }

    public function mount(?string $project_uuid = null, ?string $environment_uuid = null)
    {
        $this->parameters = get_route_parameters();
        $projectUuid = $project_uuid ?? request()->route('project_uuid');
        $environmentUuid = $environment_uuid ?? request()->route('environment_uuid');

        $this->project = Project::ownedByCurrentTeam()->where('uuid', $projectUuid)->firstOrFail();
        $this->environment = $this->project->environments()->where('uuid', $environmentUuid)->firstOrFail();
        if ($this->prefersCanvasView('environment_variables')) {
            return $this->redirectRoute('shared-variables.environment.canvas', ['project_uuid' => $this->project->uuid, 'environment_uuid' => $this->environment->uuid], navigate: true);
        }
        $this->getDevView();
    }

    public function switch()
    {
        $this->authorize('view', $this->environment);
        $this->view = $this->view === 'normal' ? 'dev' : 'normal';
        $this->getDevView();
    }

    public function getDevView()
    {
        $this->variables = $this->formatEnvironmentVariables($this->environment->environment_variables->sortBy('key'));
    }

    private function formatEnvironmentVariables($variables)
    {
        $canViewValues = auth()->user()?->isAdminOfTeam($this->project->team_id) ?? false;

        return $variables->map(fn ($item) => formatEnvVariableForDevView($item, $canViewValues))->join("\n");
    }

    public function submit()
    {
        // Parse before the save, so that an invalid "# coolify:" line changes nothing and keeps the edited text.
        try {
            $this->authorize('update', $this->environment);
            $variables = parseEnvDevView($this->variables, SharedEnvironmentVariable::DEV_VIEW_FLAG_DEFAULTS);
        } catch (\Throwable $e) {
            return handleError($e, $this);
        }

        try {
            $this->handleBulkSubmit($variables);
            $this->getDevView();
        } catch (\Throwable $e) {
            return handleError($e, $this);
        } finally {
            $this->refreshEnvs();
        }
    }

    private function handleBulkSubmit(array $variables)
    {
        $changesMade = false;

        DB::transaction(function () use ($variables, &$changesMade) {
            // Delete removed variables
            $deletedCount = $this->deleteRemovedVariables($variables);
            if ($deletedCount > 0) {
                $changesMade = true;
            }

            // Update or create variables
            $updatedCount = $this->updateOrCreateVariables($variables);
            if ($updatedCount > 0) {
                $changesMade = true;
            }
        });

        // Only dispatch success after transaction has committed
        if ($changesMade) {
            $this->dispatch('success', 'Environment variables updated.');
        }
    }

    private function deleteRemovedVariables($variables)
    {
        $variablesToDelete = $this->environment->environment_variables()->whereNotIn('key', array_keys($variables))->get();

        if ($variablesToDelete->isEmpty()) {
            return 0;
        }

        $this->environment->environment_variables()->whereNotIn('key', array_keys($variables))->delete();

        return $variablesToDelete->count();
    }

    private function updateOrCreateVariables($variables)
    {
        $count = 0;
        foreach ($variables as $key => $data) {
            $value = is_array($data) ? ($data['value'] ?? '') : $data;

            $found = $this->environment->environment_variables()->where('key', $key)->first();

            if ($found) {
                if (! $found->is_shown_once && ! $found->is_multiline && formatEnvValueForDevView($found->value) !== null) {
                    if ($found->value !== $value) {
                        $found->value = $value;
                    }
                    $found->fill($data['settings']);
                    if ($found->isDirty()) {
                        $found->save();
                        $count++;
                    }
                }
            } else {
                $this->environment->environment_variables()->create([
                    'key' => $key,
                    'value' => $value,
                    'is_multiline' => false,
                    'is_literal' => false,
                    ...$data['settings'],
                    'type' => 'environment',
                    'team_id' => $this->project->team_id,
                ]);
                $count++;
            }
        }

        return $count;
    }

    public function refreshEnvs()
    {
        $this->environment->refresh();
        $this->getDevView();
    }

    public function render()
    {
        return view('livewire.shared-variables.environment.show');
    }
}