Skip to content

Commit

Permalink
enable php extension installation
Browse files Browse the repository at this point in the history
  • Loading branch information
saeedvaziry committed Oct 15, 2023
1 parent f51d790 commit f2f9ed2
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 5 deletions.
32 changes: 32 additions & 0 deletions app/Http/Livewire/Php/InstalledVersions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
namespace App\Http\Livewire\Php;

use App\Actions\PHP\InstallNewPHP;
use App\Actions\PHP\InstallPHPExtension;
use App\Actions\PHP\UpdatePHPIni;
use App\Models\Server;
use App\Models\Service;
use App\SSHCommands\PHP\GetPHPIniCommand;
use App\Traits\RefreshComponentOnBroadcast;
use Exception;
use Illuminate\Contracts\View\View;
use Livewire\Component;
use Throwable;
Expand All @@ -24,6 +26,10 @@ class InstalledVersions extends Component

public string $ini = 'Loading php.ini';

public ?int $extensionId = null;

public string $extension = '';

public function install(string $version): void
{
app(InstallNewPHP::class)->install($this->server, [
Expand All @@ -35,6 +41,7 @@ public function install(string $version): void

public function restart(int $id): void
{
/** @var Service $service */
$service = Service::query()->findOrFail($id);
$service->restart();

Expand All @@ -43,6 +50,7 @@ public function restart(int $id): void

public function uninstall(): void
{
/** @var Service $service */
$service = Service::query()->findOrFail($this->uninstallId);
$service->uninstall();

Expand All @@ -56,6 +64,7 @@ public function loadIni(int $id): void
$this->iniId = $id;
$this->ini = 'Loading php.ini';

/** @var Service $service */
$service = Service::query()->findOrFail($this->iniId);

try {
Expand All @@ -67,6 +76,7 @@ public function loadIni(int $id): void

public function saveIni(): void
{
/** @var Service $service */
$service = Service::query()->findOrFail($this->iniId);

app(UpdatePHPIni::class)->update($service, $this->all()['ini']);
Expand All @@ -76,10 +86,32 @@ public function saveIni(): void
session()->flash('status', 'ini-updated');
}

/**
* @throws Exception
*/
public function installExtension(): void
{
/** @var Service $service */
$service = Service::query()->findOrFail($this->extensionId);

app(InstallPHPExtension::class)->handle($service, [
'name' => $this->extension,
]);

session()->flash('status', 'started-installation');
}

public function render(): View
{
if ($this->extensionId) {
/** @var Service $php */
$php = Service::query()->findOrFail($this->extensionId);
$installedExtensions = $php->type_data['extensions'] ?? [];
}

return view('livewire.php.installed-versions', [
'phps' => $this->server->services()->where('type', 'php')->get(),
'installedExtensions' => $installedExtensions ?? [],
]);
}
}
3 changes: 2 additions & 1 deletion config/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,12 @@
*/
'php_extensions' => [
'imagick',
'geoip',
'exif',
'gmagick',
'gmp',
'intl',
'sqlite3',
'opcache',
],

/*
Expand Down
3 changes: 2 additions & 1 deletion resources/views/layouts/app.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
class="left-0 top-0 min-h-screen w-64 flex-none bg-gray-800 dark:bg-gray-800/50 p-3 dark:border-r-2 dark:border-gray-800">
<div class="h-16 block">
<div class="flex items-center justify-start text-3xl font-extrabold text-white">
Vito
<x-application-logo class="w-10 h-10 rounded-md"/>
<span class="ml-1">Deploy</span>
</div>
</div>

Expand Down
7 changes: 4 additions & 3 deletions resources/views/livewire/php/installed-versions.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
</x-secondary-button>
</x-slot>
<x-slot name="content">
{{--<x-dropdown-link class="cursor-pointer">--}}
{{-- {{ __("Install Extension") }}--}}
{{--</x-dropdown-link>--}}
<x-dropdown-link class="cursor-pointer" x-on:click="$wire.extensionId = {{ $php->id }}; $dispatch('open-modal', 'install-extension')">
{{ __("Install Extension") }}
</x-dropdown-link>
<x-dropdown-link class="cursor-pointer" x-on:click="$dispatch('open-modal', 'update-php-ini')" wire:click="loadIni({{ $php->id }})">
{{ __("Edit php.ini") }}
</x-dropdown-link>
Expand All @@ -52,6 +52,7 @@
</div>
@include('livewire.php.partials.uninstall-php')
@include('livewire.php.partials.update-php-ini')
@include('livewire.php.partials.install-extension')
@else
<x-simple-card>
<div class="text-center">
Expand Down
36 changes: 36 additions & 0 deletions resources/views/livewire/php/partials/install-extension.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<x-modal name="install-extension">
<form wire:submit.prevent="installExtension" class="p-6">
<h2 class="text-lg font-medium text-gray-900 dark:text-gray-100">
{{ __('Install Extension') }}
</h2>

<div class="mt-6">
<x-input-label for="extension" value="Name" />
<x-select-input wire:model.defer="extension" name="extension" class="mt-1 w-full">
<option value="" selected>{{ __("Select") }}</option>
@foreach(config('core.php_extensions') as $extension)
<option value="{{ $extension }}" @if(in_array($extension, $installedExtensions)) disabled @endif>
{{ $extension }} @if(in_array($extension, $installedExtensions)) ({{ __("Installed") }}) @endif
</option>
@endforeach
</x-select-input>
@error('name')
<x-input-error class="mt-2" :messages="$message" />
@enderror
</div>

<div class="mt-6 flex items-center justify-end">
@if (session('status') === 'started-installation')
<p class="mr-2">{{ __('Installation Started!') }}</p>
@endif

<x-secondary-button type="button" x-on:click="$dispatch('close')">
{{ __('Cancel') }}
</x-secondary-button>

<x-primary-button class="ml-3">
{{ __('Install') }}
</x-primary-button>
</div>
</form>
</x-modal>
39 changes: 39 additions & 0 deletions tests/Feature/Http/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Http\Livewire\Php\InstalledVersions;
use App\Jobs\Installation\InstallPHP;
use App\Jobs\Installation\UninstallPHP;
use App\Jobs\PHP\InstallPHPExtension;
use App\Jobs\PHP\SetDefaultCli;
use App\Models\Service;
use Illuminate\Foundation\Testing\RefreshDatabase;
Expand Down Expand Up @@ -68,4 +69,42 @@ public function test_change_default_php_cli(): void

Bus::assertDispatched(SetDefaultCli::class);
}

public function test_install_extension(): void
{
Bus::fake();

$this->actingAs($this->user);

Livewire::test(InstalledVersions::class, ['server' => $this->server])
->set('extensionId', $this->server->php('8.2')?->id)
->set('extension', 'gmp')
->call('installExtension')
->assertSuccessful();

Bus::assertDispatched(InstallPHPExtension::class);
}

public function test_extension_already_installed(): void
{
Bus::fake();

$this->actingAs($this->user);

$this->server->php('8.2')->update([
'type_data' => [
'extensions' => [
'gmp',
],
],
]);

Livewire::test(InstalledVersions::class, ['server' => $this->server])
->set('extensionId', $this->server->php('8.2')?->id)
->set('extension', 'gmp')
->call('installExtension')
->assertSuccessful();

Bus::assertNotDispatched(InstallPHPExtension::class);
}
}

0 comments on commit f2f9ed2

Please sign in to comment.