diff --git a/app/Actions/PHP/GetPHPIni.php b/app/Actions/PHP/GetPHPIni.php index d0dd1be..e940478 100644 --- a/app/Actions/PHP/GetPHPIni.php +++ b/app/Actions/PHP/GetPHPIni.php @@ -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')] diff --git a/app/Actions/PHP/UpdatePHPIni.php b/app/Actions/PHP/UpdatePHPIni.php index 2f253d9..2a356c5 100755 --- a/app/Actions/PHP/UpdatePHPIni.php +++ b/app/Actions/PHP/UpdatePHPIni.php @@ -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())) { diff --git a/app/Enums/PHPIniType.php b/app/Enums/PHPIniType.php new file mode 100644 index 0000000..2c6a5ef --- /dev/null +++ b/app/Enums/PHPIniType.php @@ -0,0 +1,10 @@ +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'), diff --git a/app/SSH/Services/PHP/PHP.php b/app/SSH/Services/PHP/PHP.php index ac65702..d8c8611 100644 --- a/app/SSH/Services/PHP/PHP.php +++ b/app/SSH/Services/PHP/PHP.php @@ -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) ); } } diff --git a/app/SSH/Services/PHP/scripts/get-php-ini.sh b/app/SSH/Services/PHP/scripts/get-php-ini.sh deleted file mode 100644 index 8e38788..0000000 --- a/app/SSH/Services/PHP/scripts/get-php-ini.sh +++ /dev/null @@ -1,3 +0,0 @@ -if ! cat /etc/php/__version__/cli/php.ini; then - echo 'VITO_SSH_ERROR' && exit 1 -fi diff --git a/resources/views/php/partials/installed-versions.blade.php b/resources/views/php/partials/installed-versions.blade.php index d162076..2410d12 100644 --- a/resources/views/php/partials/installed-versions.blade.php +++ b/resources/views/php/partials/installed-versions.blade.php @@ -37,16 +37,19 @@ class="cursor-pointer" > {{ __("Install Extension") }} - - {{ __("Edit php.ini") }} - + @foreach ([\App\Enums\PHPIniType::FPM, \App\Enums\PHPIniType::CLI] as $type) + + {{ __("Edit php.ini (:type)", ["type" => $type]) }} + + @endforeach + - @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") diff --git a/resources/views/php/partials/update-php-ini.blade.php b/resources/views/php/partials/update-php-ini.blade.php index 60bd45b..61c6018 100644 --- a/resources/views/php/partials/update-php-ini.blade.php +++ b/resources/views/php/partials/update-php-ini.blade.php @@ -1,15 +1,16 @@ - + $server]) }}" hx-swap="outerHTML" - hx-select="#update-php-ini-form" + hx-select="#update-php-ini-{{ $type }}-form" class="p-6" > + - {{ __("Update php.ini") }} + {{ __("Update php.ini (:type)", ["type" => $type]) }} diff --git a/tests/Feature/PHPTest.php b/tests/Feature/PHPTest.php index c7b131c..78238cd 100644 --- a/tests/Feature/PHPTest.php +++ b/tests/Feature/PHPTest.php @@ -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], + ]; } }