This commit is contained in:
Saeed Vaziry
2023-07-02 12:47:50 +02:00
commit 5c72f12490
825 changed files with 41659 additions and 0 deletions

View File

@ -0,0 +1,52 @@
<?php
namespace App\Actions\PHP;
use App\Enums\ServiceStatus;
use App\Models\Server;
use App\Models\Service;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
class InstallNewPHP
{
public function install(Server $server, array $input): void
{
$this->validate($server, $input);
$php = new Service([
'server_id' => $server->id,
'type' => 'php',
'type_data' => [
'extensions' => [],
'settings' => config('core.php_settings'),
],
'name' => 'php',
'version' => $input['version'],
'status' => ServiceStatus::INSTALLING,
'is_default' => false,
]);
$php->save();
$php->install();
}
/**
* @throws ValidationException
*/
private function validate(Server $server, array $input): void
{
Validator::make($input, [
'version' => [
'required',
Rule::in(config('core.php_versions')),
],
])->validateWithBag('installPHP');
if (in_array($input['version'], $server->installedPHPVersions())) {
throw ValidationException::withMessages(
['version' => __('This version is already installed')]
)->errorBag('installPHP');
}
}
}

View File

@ -0,0 +1,46 @@
<?php
namespace App\Actions\PHP;
use App\Models\Service;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
class InstallPHPExtension
{
/**
* @throws ValidationException
*/
public function handle(Service $service, array $input): Service
{
$typeData = $service->type_data;
$typeData['extensions'] = $typeData['extensions'] ?? [];
$service->type_data = $typeData;
$service->save();
$this->validate($service, $input);
$service->handler()->installExtension($input['name']);
return $service;
}
/**
* @throws ValidationException
*/
private function validate(Service $service, array $input): void
{
Validator::make($input, [
'name' => [
'required',
'in:'.implode(',', config('core.php_extensions')),
],
])->validateWithBag('installPHPExtension');
if (in_array($input['name'], $service->type_data['extensions'])) {
throw ValidationException::withMessages(
['name' => __('This extension already installed')]
)->errorBag('installPHPExtension');
}
}
}

View File

@ -0,0 +1,39 @@
<?php
namespace App\Actions\PHP;
use App\Models\Server;
use Illuminate\Validation\ValidationException;
class UninstallPHP
{
public function uninstall(Server $server, string $version): void
{
$this->validate($server, $version);
$php = $server->services()->where('type', 'php')->where('version', $version)->first();
$php->uninstall();
}
/**
* @throws ValidationException
*/
private function validate(Server $server, string $version): void
{
$php = $server->services()->where('type', 'php')->where('version', $version)->first();
if (! $php) {
throw ValidationException::withMessages(
['version' => __('This version has not been installed yet!')]
);
}
$hasSite = $server->sites()->where('php_version', $version)->first();
if ($hasSite) {
throw ValidationException::withMessages(
['version' => __('Cannot uninstall this version because some sites are using it!')]
);
}
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace App\Actions\PHP;
use App\Models\Service;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Throwable;
class UpdatePHPIni
{
/**
* @throws ValidationException
*/
public function update(Service $service, string $ini): void
{
$tmpName = Str::random(10).strtotime('now');
try {
Storage::disk('local')->put($tmpName, $ini);
$service->server->ssh('root')->upload(
Storage::disk('local')->path($tmpName),
"/etc/php/$service->version/cli/php.ini"
);
$this->deleteTempFile($tmpName);
} catch (Throwable) {
$this->deleteTempFile($tmpName);
throw ValidationException::withMessages([
'ini' => __("Couldn't update php.ini file!"),
]);
}
}
private function deleteTempFile(string $name): void
{
if (Storage::disk('local')->exists($name)) {
Storage::disk('local')->delete($name);
}
}
}