146 lines
3.0 KiB
PHP
146 lines
3.0 KiB
PHP
<?php
|
|
|
|
use App\Modules\EmptyModule;
|
|
use App\Modules\ModuleContract;
|
|
use App\Modules\TurkmenNumberFormatter\Repositories\TurkmenNumberFormatter;
|
|
use Illuminate\Contracts\Cache\Repository as CacheRepository;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\File;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Str;
|
|
|
|
/**
|
|
* Application locales
|
|
*
|
|
* @return array<array-key, mixed>
|
|
*/
|
|
function appLocales(): array
|
|
{
|
|
return config()->array('app.locales');
|
|
}
|
|
|
|
/**
|
|
* Get module
|
|
*/
|
|
function module(string $moduleName): ModuleContract
|
|
{
|
|
$moduleClass = 'App\\Modules\\'.$moduleName.'\\'.$moduleName.'Module';
|
|
|
|
if (class_exists($moduleClass)) {
|
|
/** @var ModuleContract $module */
|
|
$module = new $moduleClass;
|
|
|
|
return $module;
|
|
}
|
|
|
|
return emptyModule();
|
|
}
|
|
|
|
/**
|
|
* Empty module
|
|
*/
|
|
function emptyModule(): ModuleContract
|
|
{
|
|
return new EmptyModule;
|
|
}
|
|
|
|
/**
|
|
* Modules directory path
|
|
*/
|
|
function modules_path(string $path = ''): string
|
|
{
|
|
return app_path('Modules/'.$path);
|
|
}
|
|
|
|
/**
|
|
* Modules
|
|
*
|
|
* @return Collection<array-key, string>
|
|
*/
|
|
function modules(bool $withDisabled = false): Collection
|
|
{
|
|
if (temp_cache()->has('modules')) {
|
|
/** @var Collection<array-key, string> $modules */
|
|
$modules = temp_cache('modules');
|
|
|
|
return $modules;
|
|
}
|
|
|
|
/** @var array<int, string> */
|
|
$modulesDir = File::directories(modules_path());
|
|
$modules = collect();
|
|
|
|
foreach ($modulesDir as $modulePath) {
|
|
$moduleName = Str::afterLast($modulePath, '/');
|
|
|
|
$moduleOptions = [
|
|
'path' => $modulePath,
|
|
'name' => $moduleName.'Module',
|
|
'enabled' => module($moduleName)->isEnabled(),
|
|
];
|
|
|
|
// Include all
|
|
if ($withDisabled) {
|
|
$modules->push($moduleOptions);
|
|
|
|
continue;
|
|
}
|
|
|
|
if ($moduleOptions['enabled']) {
|
|
$modules->push($moduleOptions);
|
|
}
|
|
}
|
|
|
|
temp_cache()->put('modules', $modules);
|
|
|
|
return $modules;
|
|
}
|
|
|
|
/**
|
|
* Temprory cache for single request
|
|
*
|
|
* @return ($key is '' ? CacheRepository : mixed)
|
|
*/
|
|
function temp_cache(string $key = ''): mixed
|
|
{
|
|
$tempCache = cache()->driver('array');
|
|
|
|
return ($key === '') ? $tempCache : $tempCache->get($key);
|
|
}
|
|
|
|
/**
|
|
* Log
|
|
*/
|
|
function logDB(): void
|
|
{
|
|
if (! app()->isLocal()) {
|
|
return;
|
|
}
|
|
|
|
DB::listen(function ($query) {
|
|
Log::info($query->sql, $query->bindings, $query->time);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get latest number
|
|
*/
|
|
function getLatestNumber(string $tableName = 'incoming_letters', string $column = 'number'): int
|
|
{
|
|
/** @var null|object{max_number: string} $data */
|
|
$data = DB::table($tableName)->select(DB::raw("MAX(CAST(REGEXP_REPLACE({$column}, '[^0-9]', '') AS UNSIGNED)) AS max_number"))->first();
|
|
|
|
return $data ? intval($data->max_number) : 0;
|
|
}
|
|
|
|
/**
|
|
* Written format of money in turkmen
|
|
*
|
|
* @param string $money
|
|
*/
|
|
function moneyFormatInTurkmenLetter(string $money): string
|
|
{
|
|
return TurkmenNumberFormatter::format(floatval($money));
|
|
}
|