Skip to content

Commit

Permalink
update php fpm ini (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
saeedvaziry authored Jul 27, 2024
1 parent 55269db commit 9473d19
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 33 deletions.
12 changes: 11 additions & 1 deletion app/Actions/PHP/GetPHPIni.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()]
Expand All @@ -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')]
Expand Down
13 changes: 10 additions & 3 deletions app/Actions/PHP/UpdatePHPIni.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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']]),
]);
}

Expand All @@ -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())) {
Expand Down
10 changes: 10 additions & 0 deletions app/Enums/PHPIniType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Enums;

final class PHPIniType
{
const CLI = 'cli';

const FPM = 'fpm';
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/PHPController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
8 changes: 3 additions & 5 deletions app/SSH/Services/PHP/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
}
3 changes: 0 additions & 3 deletions app/SSH/Services/PHP/scripts/get-php-ini.sh

This file was deleted.

26 changes: 15 additions & 11 deletions resources/views/php/partials/installed-versions.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]) }}"
Expand Down Expand Up @@ -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>
9 changes: 5 additions & 4 deletions resources/views/php/partials/update-php-ini.blade.php
Original file line number Diff line number Diff line change
@@ -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">
Expand Down
27 changes: 22 additions & 5 deletions tests/Feature/PHPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests\Feature;

use App\Enums\PHPIniType;
use App\Enums\ServiceStatus;
use App\Facades\SSH;
use App\Models\Service;
Expand Down Expand Up @@ -136,31 +137,47 @@ 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]');

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

$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();

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

$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],
];
}
}

0 comments on commit 9473d19

Please sign in to comment.