FEATURE #1753: remove pingpong/modules and use custom solution

This commit is contained in:
Michal Kváček
2017-07-31 15:32:33 +02:00
parent f876a08429
commit 72c64dc6d1
48 changed files with 1230 additions and 1156 deletions
+2 -3
View File
@@ -64,13 +64,12 @@ class AppServiceProvider extends ServiceProvider
}
// register PingPong module extension
$this->app->register(ModulesServiceProvider::class);
$this->app->register(MenusServiceProvider::class);
$this->app->register(LaravelLocalizationServiceProvider::class);
// create singletons for registering widgets
$this->app->singleton('registerWidget', function ($app) {
return new WidgetRegistrator($app);
return new WidgetRegistrator();
});
// create singleton for display pipeline - pipeline used for displaying visitor pages
@@ -85,7 +84,7 @@ class AppServiceProvider extends ServiceProvider
// create registrator for template directories
$this->app->singleton('registerTemplateDir', function ($app) {
return new TemplateRegistrator($app);
return new TemplateRegistrator();
});
$this->app->singleton('registerSettingsExtension', function ($app) {
-93
View File
@@ -1,93 +0,0 @@
<?php
namespace App\Providers;
use App\Models\Module;
use Illuminate\Support\Facades\Cache;
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
$modules = Cache::rememberForever('enabled-modules', function () {
$modules = Module::where('enabled', true)
->get();
foreach ($modules as $module) {
$module->module_settings = json_decode($module->module_settings);
}
return $modules;
});
// iterate over all modules and enable or disable them
foreach ($modules as $module) {
if ($module->is_template) {
if (isset($module->module_settings->providers) && !empty($module->module_settings->providers)) {
foreach ($module->module_settings->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->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));
}
}
-79
View File
@@ -1,79 +0,0 @@
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
abstract class ModuleServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
protected $alias = false;
protected $path = false;
abstract protected function setModuleAlias();
abstract protected function setModulePath();
/**
* Boot the application events.
*
* @return void
*/
public function boot()
{
$this->setModuleAlias();
$this->setModulePath();
$this->registerTranslations();
$this->registerConfig();
$this->registerViews();
}
/**
* Register config.
*
* @return void
*/
protected function registerConfig()
{
$this->publishes([$this->path . '/../Config/config.php' => config_path($this->alias . '.php')]);
$this->mergeConfigFrom($this->path . '/../Config/config.php', $this->alias);
}
public function register()
{
}
/**
* Register views.
*
* @return void
*/
protected function registerViews()
{
$viewPath = base_path('resources/views/modules/' . $this->alias);
$sourcePath = $this->path . '/../Resources/views';
$this->loadViewsFrom(array_merge(array_map(function ($path) {
return $path . '/modules/' . $this->alias;
}, \Config::get('view.paths')), [$sourcePath]), $this->alias);
if (is_dir($sourcePath)) {
$this->publishes([$sourcePath => $viewPath], 'module-views');
}
}
/**
* Register translations.
*
* @return void
*/
protected function registerTranslations()
{
$this->loadTranslationsFrom($this->path . '/../Resources/lang', $this->alias);
}
}
-64
View File
@@ -1,64 +0,0 @@
<?php
namespace App\Providers;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\ServiceProvider;
use Pingpong\Modules\Json;
class SubmodulesManager extends ServiceProvider
{
/**
* Boot submodules
*
* @return void
*/
public function boot()
{
$enabledModules = \Module::enabled();
foreach ($enabledModules as $module) {
$modulePath = $module->getPath();
$config = new Json($modulePath . DIRECTORY_SEPARATOR . 'module.json');
// Does this module specify any providers? If not, consider this module with submodules and register them
if ($config->get('providers')) {
continue;
}
$submodules = \File::directories($modulePath);
foreach ($submodules as $submodulePath) {
try {
$config = new Json($submodulePath . DIRECTORY_SEPARATOR . 'module.json');
if (!$config->get('active')) {
continue;
}
$serviceProviders = $config->get('providers', []);
foreach ($serviceProviders as $serviceProvider) {
$this->app->register($serviceProvider);
}
$initFiles = $config->get('files', []);
foreach ($initFiles as $file) {
require $submodulePath . DIRECTORY_SEPARATOR . $file;
}
} catch (FileNotFoundException $e) {
// ignore missing module and do not register this submodule
}
}
}
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
}
}
+1 -1
View File
@@ -94,7 +94,7 @@ class TemplateServiceProvider extends ServiceProvider
return;
}
$templateDir = base_path('templates/' . $template->module_settings->staticName . '/Resources');
$templateDir = base_path('templates/' . $template->module_settings['staticName']. '/Resources');
$assetsDir = $templateDir . '/' . TemplateContract::ASSETS_DIRECTORY;
$viewsDir = $templateDir . '/views';