97 lines
1.9 KiB
PHP
97 lines
1.9 KiB
PHP
<?php
|
|
|
|
use App\Modules\EmptyModule;
|
|
use App\Modules\ModuleContract;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\File;
|
|
use Illuminate\Support\Str;
|
|
|
|
/**
|
|
* Application locales
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
function appLocales(): array
|
|
{
|
|
return config()->array('app.locales');
|
|
}
|
|
|
|
/**
|
|
* Get module
|
|
*/
|
|
function module(string $moduleName): ModuleContract
|
|
{
|
|
$moduleClass = 'App\\Modules\\'.$moduleName.'\\'.$moduleName.'Module';
|
|
|
|
return class_exists($moduleClass) ? (new $moduleClass) : 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')) {
|
|
return temp_cache('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
|
|
*/
|
|
function temp_cache(string $key = ''): mixed
|
|
{
|
|
$tempCache = cache()->driver('array');
|
|
|
|
return ($key !== '')
|
|
? $tempCache->get($key)
|
|
: cache()->driver('array');
|
|
}
|