lasthourcloud/bootstrap/helpers/shared.php

97 lines
3.0 KiB
PHP
Raw Normal View History

2023-05-24 12:26:50 +00:00
<?php
2023-06-12 10:00:01 +00:00
use App\Models\InstanceSettings;
2023-05-24 12:26:50 +00:00
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Route;
use Visus\Cuid2\Cuid2;
use Illuminate\Support\Str;
2023-06-09 13:55:21 +00:00
function general_error_handler(\Throwable|null $err = null, $that = null, $isJson = false, $customErrorMessage = null)
2023-05-24 12:26:50 +00:00
{
try {
2023-06-09 13:55:21 +00:00
ray('ERROR OCCURED: ' . $err->getMessage());
if ($err instanceof QueryException) {
if ($err->errorInfo[0] === '23505') {
throw new \Exception($customErrorMessage ?? 'Duplicate entry found.', '23505');
} else if (count($err->errorInfo) === 4) {
throw new \Exception($customErrorMessage ?? $err->errorInfo[3]);
2023-05-24 12:26:50 +00:00
} else {
2023-06-09 13:55:21 +00:00
throw new \Exception($customErrorMessage ?? $err->errorInfo[2]);
2023-05-24 12:26:50 +00:00
}
} else {
2023-06-09 13:55:21 +00:00
throw new \Exception($customErrorMessage ?? $err->getMessage());
2023-05-24 12:26:50 +00:00
}
} catch (\Throwable $error) {
if ($that) {
2023-06-09 13:55:21 +00:00
return $that->emit('error', $customErrorMessage ?? $error->getMessage());
2023-05-24 12:26:50 +00:00
} elseif ($isJson) {
return response()->json([
'code' => $error->getCode(),
'error' => $error->getMessage(),
]);
} else {
2023-06-09 13:55:21 +00:00
ray($customErrorMessage);
2023-05-30 13:52:17 +00:00
ray($error);
2023-05-24 12:26:50 +00:00
}
}
}
function get_parameters()
{
return Route::current()->parameters();
}
function get_latest_version_of_coolify()
{
2023-06-06 07:22:48 +00:00
$response = Http::get('https://cdn.coollabs.io/coolify/versions.json');
2023-05-24 12:26:50 +00:00
$versions = $response->json();
return data_get($versions, 'coolify.v4.version');
}
function generate_random_name()
{
$generator = \Nubs\RandomNameGenerator\All::create();
$cuid = new Cuid2(7);
return Str::kebab("{$generator->getName()}-{$cuid}");
}
2023-05-24 13:47:04 +00:00
function generate_application_name(string $git_repository, string $git_branch)
{
$cuid = new Cuid2(7);
return Str::kebab("{$git_repository}:{$git_branch}-{$cuid}");
}
2023-06-12 10:00:01 +00:00
function is_transactional_emails_active()
{
2023-06-13 08:51:58 +00:00
return data_get(InstanceSettings::get(), 'extra_attributes.smtp_active');
2023-06-12 10:00:01 +00:00
}
2023-06-12 14:39:48 +00:00
function set_transanctional_email_settings()
{
$settings = InstanceSettings::get();
config()->set('mail.default', 'smtp');
config()->set('mail.mailers.smtp', [
"transport" => "smtp",
"host" => $settings->extra_attributes?->get('smtp_host'),
"port" => $settings->extra_attributes?->get('smtp_port'),
"encryption" => $settings->extra_attributes?->get('smtp_encryption'),
"username" => $settings->extra_attributes?->get('smtp_username'),
"password" => $settings->extra_attributes?->get('smtp_password'),
"timeout" => $settings->extra_attributes?->get('smtp_timeout'),
"local_domain" => null,
]);
}
2023-06-13 13:01:11 +00:00
function base_url()
{
$settings = InstanceSettings::get();
if ($settings->fqdn) {
return $settings->fqdn;
} else {
if (config('app.env') === 'local') {
return url('/') . ':8080';
}
return url('/');
}
}