This commit is contained in:
Saeed Vaziry
2024-03-24 09:56:34 +01:00
committed by GitHub
parent 884f18db63
commit 4d051330d6
1055 changed files with 14493 additions and 20278 deletions

View File

@ -0,0 +1,29 @@
<?php
namespace App\Actions\PHP;
use App\Enums\ServiceStatus;
use App\Models\Server;
use Illuminate\Validation\ValidationException;
class ChangeDefaultCli
{
public function change(Server $server, array $input): void
{
$this->validate($server, $input);
$service = $server->php($input['version']);
$service->handler()->setDefaultCli();
$server->defaultService('php')->update(['is_default' => 0]);
$service->update(['is_default' => 1]);
$service->update(['status' => ServiceStatus::READY]);
}
public function validate(Server $server, array $input): void
{
if (! isset($input['version']) || ! in_array($input['version'], $server->installedPHPVersions())) {
throw ValidationException::withMessages(
['version' => __('This version is not installed')]
);
}
}
}

View File

@ -0,0 +1,33 @@
<?php
namespace App\Actions\PHP;
use App\Models\Server;
use Illuminate\Validation\ValidationException;
class GetPHPIni
{
public function getIni(Server $server, array $input): string
{
$this->validate($server, $input);
$php = $server->php($input['version']);
try {
return $php->handler()->getPHPIni();
} catch (\Throwable $e) {
throw ValidationException::withMessages(
['ini' => $e->getMessage()]
);
}
}
public function validate(Server $server, array $input): void
{
if (! isset($input['version']) || ! in_array($input['version'], $server->installedPHPVersions())) {
throw ValidationException::withMessages(
['version' => __('This version is not installed')]
);
}
}
}

View File

@ -28,7 +28,14 @@ public function install(Server $server, array $input): void
'is_default' => false,
]);
$php->save();
$php->install();
dispatch(function () use ($php) {
$php->handler()->install();
$php->status = ServiceStatus::READY;
$php->save();
})->catch(function () use ($php) {
$php->delete();
})->onConnection('ssh');
}
/**
@ -41,12 +48,12 @@ private function validate(Server $server, array $input): void
'required',
Rule::in(config('core.php_versions')),
],
])->validateWithBag('installPHP');
])->validate();
if (in_array($input['version'], $server->installedPHPVersions())) {
throw ValidationException::withMessages(
['version' => __('This version is already installed')]
)->errorBag('installPHP');
);
}
}
}

View File

@ -2,25 +2,35 @@
namespace App\Actions\PHP;
use App\Models\Server;
use App\Models\Service;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
class InstallPHPExtension
{
/**
* @throws ValidationException
*/
public function handle(Service $service, array $input): Service
public function install(Server $server, array $input): Service
{
$this->validate($server, $input);
/** @var Service $service */
$service = $server->php($input['version']);
$typeData = $service->type_data;
$typeData['extensions'] = $typeData['extensions'] ?? [];
$typeData['extensions'][] = $input['extension'];
$service->type_data = $typeData;
$service->save();
$this->validate($service, $input);
$service->handler()->installExtension($input['name']);
dispatch(function () use ($service, $input) {
$service->handler()->installExtension($input['extension']);
})->catch(function () use ($service, $input) {
$service->refresh();
$typeData = $service->type_data;
$typeData['extensions'] = array_values(array_diff($typeData['extensions'], [$input['extension']]));
$service->type_data = $typeData;
$service->save();
})->onConnection('ssh');
return $service;
}
@ -28,18 +38,25 @@ public function handle(Service $service, array $input): Service
/**
* @throws ValidationException
*/
private function validate(Service $service, array $input): void
private function validate(Server $server, array $input): void
{
Validator::make($input, [
'name' => [
'extension' => [
'required',
'in:'.implode(',', config('core.php_extensions')),
],
])->validateWithBag('installPHPExtension');
'version' => [
'required',
Rule::in($server->installedPHPVersions()),
],
])->validate();
if (in_array($input['name'], $service->type_data['extensions'])) {
/** @var Service $service */
$service = $server->php($input['version']);
if (in_array($input['extension'], $service->type_data['extensions'])) {
throw ValidationException::withMessages(
['name' => __('This extension already installed')]
['extension' => __('This extension already installed')]
)->errorBag('installPHPExtension');
}
}

View File

@ -2,36 +2,48 @@
namespace App\Actions\PHP;
use App\Enums\ServiceStatus;
use App\Models\Server;
use App\Models\Service;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
class UninstallPHP
{
public function uninstall(Server $server, string $version): void
public function uninstall(Server $server, array $input): void
{
$this->validate($server, $version);
$this->validate($server, $input);
/** @var Service $php */
$php = $server->services()->where('type', 'php')->where('version', $version)->first();
$php = $server->php($input['version']);
$php->status = ServiceStatus::UNINSTALLING;
$php->save();
$php->uninstall();
dispatch(function () use ($php) {
$php->handler()->uninstall();
$php->delete();
})->catch(function () use ($php) {
$php->status = ServiceStatus::FAILED;
$php->save();
})->onConnection('ssh');
}
/**
* @throws ValidationException
*/
private function validate(Server $server, string $version): void
private function validate(Server $server, array $input): void
{
$php = $server->services()->where('type', 'php')->where('version', $version)->first();
Validator::make($input, [
'version' => 'required|string',
])->validate();
if (! $php) {
if (! in_array($input['version'], $server->installedPHPVersions())) {
throw ValidationException::withMessages(
['version' => __('This version has not been installed yet!')]
['version' => __('This version is not installed')]
);
}
$hasSite = $server->sites()->where('php_version', $version)->first();
$hasSite = $server->sites()->where('php_version', $input['version'])->first();
if ($hasSite) {
throw ValidationException::withMessages(
['version' => __('Cannot uninstall this version because some sites are using it!')]

View File

@ -2,8 +2,9 @@
namespace App\Actions\PHP;
use App\Models\Service;
use App\Models\Server;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Throwable;
@ -13,14 +14,18 @@ class UpdatePHPIni
/**
* @throws ValidationException
*/
public function update(Service $service, string $ini): void
public function update(Server $server, array $input): void
{
$this->validate($server, $input);
$service = $server->php($input['version']);
$tmpName = Str::random(10).strtotime('now');
try {
/** @var \Illuminate\Filesystem\FilesystemAdapter $storageDisk */
$storageDisk = Storage::disk('local');
$storageDisk->put($tmpName, $ini);
$storageDisk->put($tmpName, $input['ini']);
$service->server->ssh('root')->upload(
$storageDisk->path($tmpName),
"/etc/php/$service->version/cli/php.ini"
@ -42,4 +47,21 @@ private function deleteTempFile(string $name): void
Storage::disk('local')->delete($name);
}
}
public function validate(Server $server, array $input): void
{
Validator::make($input, [
'ini' => [
'required',
'string',
],
'version' => 'required|string',
])->validate();
if (! in_array($input['version'], $server->installedPHPVersions())) {
throw ValidationException::withMessages(
['version' => __('This version is not installed')]
);
}
}
}