update php fpm ini (#258)

This commit is contained in:
Saeed Vaziry 2024-07-27 12:51:13 +02:00 committed by GitHub
parent 55269dbcde
commit 9473d198e1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 77 additions and 33 deletions

View File

@ -2,8 +2,11 @@
namespace App\Actions\PHP;
use App\Enums\PHPIniType;
use App\Models\Server;
use App\SSH\Services\PHP\PHP;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
class GetPHPIni
@ -18,7 +21,7 @@ public function getIni(Server $server, array $input): string
/** @var PHP $handler */
$handler = $php->handler();
return $handler->getPHPIni();
return $handler->getPHPIni($input['type']);
} catch (\Throwable $e) {
throw ValidationException::withMessages(
['ini' => $e->getMessage()]
@ -28,6 +31,13 @@ public function getIni(Server $server, array $input): string
public function validate(Server $server, array $input): void
{
Validator::make($input, [
'type' => [
'required',
Rule::in([PHPIniType::CLI, PHPIniType::FPM]),
],
])->validate();
if (! isset($input['version']) || ! in_array($input['version'], $server->installedPHPVersions())) {
throw ValidationException::withMessages(
['version' => __('This version is not installed')]

View File

@ -2,10 +2,13 @@
namespace App\Actions\PHP;
use App\Enums\PHPIniType;
use App\Models\Server;
use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Throwable;
@ -22,19 +25,19 @@ public function update(Server $server, array $input): void
$tmpName = Str::random(10).strtotime('now');
try {
/** @var \Illuminate\Filesystem\FilesystemAdapter $storageDisk */
/** @var FilesystemAdapter $storageDisk */
$storageDisk = Storage::disk('local');
$storageDisk->put($tmpName, $input['ini']);
$service->server->ssh('root')->upload(
$storageDisk->path($tmpName),
"/etc/php/$service->version/cli/php.ini"
sprintf('/etc/php/%s/%s/php.ini', $service->version, $input['type'])
);
$this->deleteTempFile($tmpName);
} catch (Throwable) {
$this->deleteTempFile($tmpName);
throw ValidationException::withMessages([
'ini' => __("Couldn't update php.ini file!"),
'ini' => __("Couldn't update php.ini (:type) file!", ['type' => $input['type']]),
]);
}
@ -56,6 +59,10 @@ public function validate(Server $server, array $input): void
'string',
],
'version' => 'required|string',
'type' => [
'required',
Rule::in([PHPIniType::CLI, PHPIniType::FPM]),
],
])->validate();
if (! in_array($input['version'], $server->installedPHPVersions())) {

10
app/Enums/PHPIniType.php Normal file
View File

@ -0,0 +1,10 @@
<?php
namespace App\Enums;
final class PHPIniType
{
const CLI = 'cli';
const FPM = 'fpm';
}

View File

@ -81,7 +81,7 @@ public function updateIni(Server $server, Request $request): RedirectResponse
app(UpdatePHPIni::class)->update($server, $request->input());
Toast::success('PHP ini updated!');
Toast::success(__('PHP ini (:type) updated!', ['type' => $request->input('type')]));
return back()->with([
'ini' => $request->input('ini'),

View File

@ -102,12 +102,10 @@ public function installComposer(): void
);
}
public function getPHPIni(): string
public function getPHPIni(string $type): string
{
return $this->service->server->ssh()->exec(
$this->getScript('get-php-ini.sh', [
'version' => $this->service->version,
])
return $this->service->server->os()->readFile(
sprintf('/etc/php/%s/%s/php.ini', $this->service->version, $type)
);
}
}

View File

@ -1,3 +0,0 @@
if ! cat /etc/php/__version__/cli/php.ini; then
echo 'VITO_SSH_ERROR' && exit 1
fi

View File

@ -37,16 +37,19 @@ class="cursor-pointer"
>
{{ __("Install Extension") }}
</x-dropdown-link>
<x-dropdown-link
class="cursor-pointer"
x-on:click="version = '{{ $php->version }}'; $dispatch('open-modal', 'update-php-ini'); document.getElementById('ini').value = 'Loading...';"
hx-get="{{ route('servers.php.get-ini', ['server' => $server, 'version' => $php->version]) }}"
hx-swap="outerHTML"
hx-target="#update-php-ini-form"
hx-select="#update-php-ini-form"
>
{{ __("Edit php.ini") }}
</x-dropdown-link>
@foreach ([\App\Enums\PHPIniType::FPM, \App\Enums\PHPIniType::CLI] as $type)
<x-dropdown-link
class="cursor-pointer"
x-on:click="version = '{{ $php->version }}'; $dispatch('open-modal', 'update-php-ini-{{ $type }}'); document.getElementById('ini').value = 'Loading...';"
hx-get="{{ route('servers.php.get-ini', ['server' => $server, 'version' => $php->version, 'type' => $type]) }}"
hx-swap="outerHTML"
hx-target="#update-php-ini-{{ $type }}-form"
hx-select="#update-php-ini-{{ $type }}-form"
>
{{ __("Edit php.ini (:type)", ["type" => $type]) }}
</x-dropdown-link>
@endforeach
<x-dropdown-link
class="cursor-pointer"
href="{{ route('servers.services.restart', ['server' => $server, 'service' => $php]) }}"
@ -85,6 +88,7 @@ class="cursor-pointer"
method="delete"
x-bind:action="uninstallAction"
/>
@include("php.partials.update-php-ini")
@include("php.partials.update-php-ini", ["type" => \App\Enums\PHPIniType::CLI])
@include("php.partials.update-php-ini", ["type" => \App\Enums\PHPIniType::FPM])
@include("php.partials.install-extension")
</div>

View File

@ -1,15 +1,16 @@
<x-modal name="update-php-ini">
<x-modal name="update-php-ini-{{ $type }}">
<form
id="update-php-ini-form"
id="update-php-ini-{{ $type }}-form"
hx-post="{{ route("servers.php.update-ini", ["server" => $server]) }}"
hx-swap="outerHTML"
hx-select="#update-php-ini-form"
hx-select="#update-php-ini-{{ $type }}-form"
class="p-6"
>
<input type="hidden" name="type" value="{{ $type }}" />
<input type="hidden" name="version" :value="version" />
<h2 class="text-lg font-medium text-gray-900 dark:text-gray-100">
{{ __("Update php.ini") }}
{{ __("Update php.ini (:type)", ["type" => $type]) }}
</h2>
<div class="mt-6">

View File

@ -2,6 +2,7 @@
namespace Tests\Feature;
use App\Enums\PHPIniType;
use App\Enums\ServiceStatus;
use App\Facades\SSH;
use App\Models\Service;
@ -136,7 +137,10 @@ public function test_extension_already_installed(): void
]))->assertSessionHasErrors();
}
public function test_get_php_ini(): void
/**
* @dataProvider php_ini_data
*/
public function test_get_php_ini(string $version, string $type): void
{
SSH::fake('[PHP ini]');
@ -144,11 +148,15 @@ public function test_get_php_ini(): void
$this->get(route('servers.php.get-ini', [
'server' => $this->server,
'version' => '8.2',
'version' => $version,
'type' => $type,
]))->assertSessionHas('ini');
}
public function test_update_php_ini(): void
/**
* @dataProvider php_ini_data
*/
public function test_update_php_ini(string $version, string $type): void
{
SSH::fake();
@ -156,11 +164,20 @@ public function test_update_php_ini(): void
$this->post(route('servers.php.update-ini', [
'server' => $this->server,
'version' => '8.2',
'version' => $version,
'type' => $type,
'ini' => 'new ini',
]))
->assertSessionDoesntHaveErrors()
->assertSessionHas('toast.type', 'success')
->assertSessionHas('toast.message', 'PHP ini updated!');
->assertSessionHas('toast.message', __('PHP ini (:type) updated!', ['type' => $type]));
}
public static function php_ini_data(): array
{
return [
['8.2', PHPIniType::FPM],
['8.2', PHPIniType::CLI],
];
}
}