Files
core/app/Providers/ModuleManager.php
T

80 lines
2.2 KiB
PHP

<?php
namespace App\Providers;
use App\Models\Module;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;
class ModuleManager extends ServiceProvider
{
/**
* Bootstrap the application services.
* @return void
*/
public function boot()
{
// ignore running from artisan
if (App::runningInConsole()) {
return;
}
// 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(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));
}
}