This way should be more reliable, because config() is available only after publishing module, however I can't publish module when it does not have config (module.json). In this way I can select all modules from database and generate module.json for each of them.
75 lines
1.9 KiB
PHP
75 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use App\Models\Module;
|
|
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
|
|
$modules = Module::get();
|
|
|
|
// iterate over all modules and enable or disable them
|
|
foreach ($modules as $module) {
|
|
$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 = json_decode($module->module_settings);
|
|
|
|
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($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));
|
|
}
|
|
}
|