Files
core/app/Providers/ModuleManager.php
T

94 lines
2.6 KiB
PHP

<?php
namespace App\Providers;
use App\Models\Module;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;
class ModuleManager extends ServiceProvider
{
/**
* Bootstrap the application services.
* @return void
*/
public function boot()
{
// this service provider needs published config - config is merged right after this service provider
try {
$modules = Module::where('enabled', true)
->get();
} catch (\Exception $e) {
// ignore error
return;
}
// iterate over all modules and enable or disable them
foreach ($modules as $module) {
$module->moduleSettings = json_decode($module->moduleSettings);
if ($module->is_template) {
if (isset($module->moduleSettings->providers) && !empty($module->moduleSettings->providers)) {
foreach ($module->moduleSettings->providers as $provider) {
$this->app->register($provider);
}
}
continue;
}
$this->createModulesJson($module);
}
}
/**
* Register the application services.
* @return void
*/
public function register()
{
}
/**
* Read config file and merge it with module config in database.
* This method REQUIRES published config of given module (otherwise config(modulename) is not available)!
*
* @param Module $module
*
* @throws \ErrorException when module.json is not writable
*/
private function createModulesJson(Module $module)
{
$config = $module->moduleSettings;
if (is_null($config)) {
return;
}
// path to module.json
$path = config('modules.paths.modules') . DIRECTORY_SEPARATOR . $config->staticName . DIRECTORY_SEPARATOR . 'module.json';
// check if module config is writable
if (!is_writable(dirname($path))) {
Log::warning('Cannot write to ' . $path);
return;
}
// set enabled/disabled status for this module
$data = new \stdClass();
$data->name = $config->staticName;
$data->description = $module->description;
$data->alias = $module->alias;
$data->providers = $config->providers;
$data->aliases = $config->aliases;
$data->order = $config->order;
$data->files = $config->files;
$data->active = $module->enabled;
// create file
file_put_contents($path, json_encode($data));
}
}