enable php extension installation (#68)

This commit is contained in:
Saeed Vaziry 2023-10-15 10:06:50 +02:00 committed by GitHub
parent f51d7900f0
commit 9d13cc0756
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 115 additions and 5 deletions

View File

@ -3,11 +3,13 @@
namespace App\Http\Livewire\Php; namespace App\Http\Livewire\Php;
use App\Actions\PHP\InstallNewPHP; use App\Actions\PHP\InstallNewPHP;
use App\Actions\PHP\InstallPHPExtension;
use App\Actions\PHP\UpdatePHPIni; use App\Actions\PHP\UpdatePHPIni;
use App\Models\Server; use App\Models\Server;
use App\Models\Service; use App\Models\Service;
use App\SSHCommands\PHP\GetPHPIniCommand; use App\SSHCommands\PHP\GetPHPIniCommand;
use App\Traits\RefreshComponentOnBroadcast; use App\Traits\RefreshComponentOnBroadcast;
use Exception;
use Illuminate\Contracts\View\View; use Illuminate\Contracts\View\View;
use Livewire\Component; use Livewire\Component;
use Throwable; use Throwable;
@ -24,6 +26,10 @@ class InstalledVersions extends Component
public string $ini = 'Loading php.ini'; public string $ini = 'Loading php.ini';
public ?int $extensionId = null;
public string $extension = '';
public function install(string $version): void public function install(string $version): void
{ {
app(InstallNewPHP::class)->install($this->server, [ app(InstallNewPHP::class)->install($this->server, [
@ -35,6 +41,7 @@ public function install(string $version): void
public function restart(int $id): void public function restart(int $id): void
{ {
/** @var Service $service */
$service = Service::query()->findOrFail($id); $service = Service::query()->findOrFail($id);
$service->restart(); $service->restart();
@ -43,6 +50,7 @@ public function restart(int $id): void
public function uninstall(): void public function uninstall(): void
{ {
/** @var Service $service */
$service = Service::query()->findOrFail($this->uninstallId); $service = Service::query()->findOrFail($this->uninstallId);
$service->uninstall(); $service->uninstall();
@ -56,6 +64,7 @@ public function loadIni(int $id): void
$this->iniId = $id; $this->iniId = $id;
$this->ini = 'Loading php.ini'; $this->ini = 'Loading php.ini';
/** @var Service $service */
$service = Service::query()->findOrFail($this->iniId); $service = Service::query()->findOrFail($this->iniId);
try { try {
@ -67,6 +76,7 @@ public function loadIni(int $id): void
public function saveIni(): void public function saveIni(): void
{ {
/** @var Service $service */
$service = Service::query()->findOrFail($this->iniId); $service = Service::query()->findOrFail($this->iniId);
app(UpdatePHPIni::class)->update($service, $this->all()['ini']); app(UpdatePHPIni::class)->update($service, $this->all()['ini']);
@ -76,10 +86,32 @@ public function saveIni(): void
session()->flash('status', 'ini-updated'); 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 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', [ return view('livewire.php.installed-versions', [
'phps' => $this->server->services()->where('type', 'php')->get(), 'phps' => $this->server->services()->where('type', 'php')->get(),
'installedExtensions' => $installedExtensions ?? [],
]); ]);
} }
} }

View File

@ -292,11 +292,12 @@
*/ */
'php_extensions' => [ 'php_extensions' => [
'imagick', 'imagick',
'geoip',
'exif', 'exif',
'gmagick', 'gmagick',
'gmp', 'gmp',
'intl', 'intl',
'sqlite3',
'opcache',
], ],
/* /*

View File

@ -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"> 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="h-16 block">
<div class="flex items-center justify-start text-3xl font-extrabold text-white"> <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>
</div> </div>

View File

@ -28,9 +28,9 @@
</x-secondary-button> </x-secondary-button>
</x-slot> </x-slot>
<x-slot name="content"> <x-slot name="content">
{{--<x-dropdown-link class="cursor-pointer">--}} <x-dropdown-link class="cursor-pointer" x-on:click="$wire.extensionId = {{ $php->id }}; $dispatch('open-modal', 'install-extension')">
{{-- {{ __("Install Extension") }}--}} {{ __("Install Extension") }}
{{--</x-dropdown-link>--}} </x-dropdown-link>
<x-dropdown-link class="cursor-pointer" x-on:click="$dispatch('open-modal', 'update-php-ini')" wire:click="loadIni({{ $php->id }})"> <x-dropdown-link class="cursor-pointer" x-on:click="$dispatch('open-modal', 'update-php-ini')" wire:click="loadIni({{ $php->id }})">
{{ __("Edit php.ini") }} {{ __("Edit php.ini") }}
</x-dropdown-link> </x-dropdown-link>
@ -52,6 +52,7 @@
</div> </div>
@include('livewire.php.partials.uninstall-php') @include('livewire.php.partials.uninstall-php')
@include('livewire.php.partials.update-php-ini') @include('livewire.php.partials.update-php-ini')
@include('livewire.php.partials.install-extension')
@else @else
<x-simple-card> <x-simple-card>
<div class="text-center"> <div class="text-center">

View File

@ -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>

View File

@ -7,6 +7,7 @@
use App\Http\Livewire\Php\InstalledVersions; use App\Http\Livewire\Php\InstalledVersions;
use App\Jobs\Installation\InstallPHP; use App\Jobs\Installation\InstallPHP;
use App\Jobs\Installation\UninstallPHP; use App\Jobs\Installation\UninstallPHP;
use App\Jobs\PHP\InstallPHPExtension;
use App\Jobs\PHP\SetDefaultCli; use App\Jobs\PHP\SetDefaultCli;
use App\Models\Service; use App\Models\Service;
use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\RefreshDatabase;
@ -68,4 +69,42 @@ public function test_change_default_php_cli(): void
Bus::assertDispatched(SetDefaultCli::class); 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);
}
} }