module manager

This commit is contained in:
Kavalar 2024-09-12 16:01:04 +03:00
parent 2105c7bc61
commit 95e80ab87b
21 changed files with 254 additions and 3 deletions

View File

@ -1,6 +1,7 @@
{
"name": "Custom",
"version": "0.1",
"author": "Kavalar",
"slug": "custom",
"description": "Custom admin theme",
"preview": "nrnv2024_640x360.jpg",

View File

@ -10,7 +10,7 @@ class AdminController extends Controller
{
protected AdminThemeService $adminThemeService;
protected function init()
protected function init(): void
{
$this->adminThemeService = new AdminThemeService();
$active_theme = $this->adminThemeService->getActiveAdminThemeInfo();

9
kernel/Module.php Normal file
View File

@ -0,0 +1,9 @@
<?php
namespace kernel;
abstract class Module
{
abstract public function init();
}

View File

@ -1,6 +1,7 @@
{
"name": "Default",
"version": "0.1",
"author": "ItGuild",
"slug": "default",
"description": "Default admin theme",
"preview": "preview.png",

View File

@ -1,6 +1,7 @@
{
"name": "Simple",
"version": "0.1",
"author": "ItGuild",
"slug": "simple",
"description": "Simple admin theme",
"preview": "preview.png",

View File

@ -0,0 +1,50 @@
<?php
namespace kernel\controllers;
use DirectoryIterator;
use kernel\AdminController;
use kernel\helpers\Debug;
use kernel\models\Option;
use kernel\modules\user\service\UserService;
use kernel\services\ModuleService;
class ModuleController extends AdminController
{
protected ModuleService $moduleService;
protected function init(): void
{
parent::init();
$this->cgView->viewPath = KERNEL_DIR . "/views/module/";
$this->moduleService = new ModuleService();
}
public function actionIndex(): void
{
$admin_theme_paths = Option::where("key", "module_paths")->first();
$dirs = [];
if ($admin_theme_paths){
$path = json_decode($admin_theme_paths->value);
foreach ($path->paths as $p){
$dirs[] = getConst($p);
}
}
$modules_info = [];
foreach ($dirs as $dir){
$i = 1;
foreach (new DirectoryIterator($dir) as $fileInfo) {
$info = [];
if($fileInfo->isDot()) continue;
$info['id'] = $i;
$modules_info[] = array_merge($info, $this->moduleService->getModuleInfo($fileInfo->getPathname()));
$i++;
}
}
$this->cgView->render("index.php", ['modules_info' => $modules_info, 'moduleService' => $this->moduleService]);
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace kernel\modules\admin_themes;
use kernel\Module;
class AdminThemesModule extends Module
{
public function init()
{
// TODO: Implement init() method.
}
}

View File

@ -30,7 +30,13 @@ class AdminThemeController extends AdminController
$info_to_table = [];
$meta = [];
$meta['columns'] = ["preview" => "Превью", "name" => "Название", "version" => "Версия", "description" => "Описание"];
$meta['columns'] = [
"preview" => "Превью",
"name" => "Название",
"author" => "Автор",
"version" => "Версия",
"description" => "Описание"
];
$meta['params'] = ["class" => "table table-bordered"];
$meta['perPage'] = 10;
$meta['baseUrl'] = "/admin/settings/admin-themes";

View File

@ -0,0 +1,8 @@
{
"name": "Admin themes",
"version": "0.1",
"author": "ITGuild",
"slug": "admin_themes",
"description": "Admin themes module",
"module_class": "AdminThemesModule"
}

View File

@ -15,7 +15,7 @@ $table->columns([
$table->addAction(function ($row, $url){
$path = $row['path'];
$active_admin_theme = Option::where("key", "active_admin_theme")->first();
$btn = "<a class='btn btn-primary' href='$url/activate/?p=$path' style='margin: 3px; width: 150px;' >Активировать</a>";;
$btn = "<a class='btn btn-primary' href='$url/activate/?p=$path' style='margin: 3px; width: 150px;' >Активировать</a>";
if ($path === $active_admin_theme->value){
$btn = "Активна";
}

View File

@ -0,0 +1,14 @@
<?php
namespace kernel\modules\menu;
use kernel\Module;
class MenuModule extends Module
{
public function init()
{
// TODO: Implement init() method.
}
}

View File

@ -0,0 +1,8 @@
{
"name": "Menu",
"version": "0.1",
"author": "ITGuild",
"slug": "menu",
"description": "Menu module",
"module_class": "MenuModule"
}

View File

@ -0,0 +1,14 @@
<?php
namespace kernel\modules\post;
use kernel\Module;
class PostModule extends Module
{
public function init()
{
// TODO: Implement init() method.
}
}

View File

@ -0,0 +1,8 @@
{
"name": "Post",
"version": "0.1",
"author": "ITGuild",
"slug": "post",
"description": "Post module",
"module_class": "PostModule"
}

View File

@ -0,0 +1,15 @@
<?php
namespace kernel\modules\user;
use kernel\Module;
class UserModule extends Module
{
public function init()
{
// TODO: Implement init() method.
}
}

View File

@ -0,0 +1,8 @@
{
"name": "User",
"version": "0.1",
"author": "ITGuild",
"slug": "user",
"description": "User module",
"module_class": "UserModule"
}

View File

@ -7,6 +7,9 @@ use Phroute\Phroute\RouteCollector;
App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){
App::$collector->group(["prefix" => "module"], function (RouteCollector $router){
App::$collector->get('/', [\kernel\controllers\ModuleController::class, 'actionIndex']);
});
App::$collector->group(["prefix" => "post"], function (RouteCollector $router){
App::$collector->get('/', [\kernel\modules\post\controllers\PostController::class, 'actionIndex']);
App::$collector->get('/page/{page_number}', [\kernel\modules\post\controllers\PostController::class, 'actionIndex']);
@ -17,6 +20,16 @@ App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){
App::$collector->any("/edit/{id}", [\kernel\modules\post\controllers\PostController::class, 'actionEdit']);
App::$collector->get('/delete/{id}', [\kernel\modules\post\controllers\PostController::class, 'actionDelete']);
});
App::$collector->group(["prefix" => "user"], callback: function (RouteCollector $router){
App::$collector->get('/', [\kernel\modules\user\controllers\UserController::class, 'actionIndex']);
App::$collector->get('/page/{page_number}', [\kernel\modules\user\controllers\UserController::class, 'actionIndex']);
App::$collector->get('/create', [\kernel\modules\user\controllers\UserController::class, 'actionCreate']);
App::$collector->post("/", [\kernel\modules\user\controllers\UserController::class, 'actionAdd']);
App::$collector->get('/{id}', [\kernel\modules\user\controllers\UserController::class, 'actionView']);
App::$collector->any('/update/{id}', [\kernel\modules\user\controllers\UserController::class, 'actionUpdate']);
App::$collector->any("/edit/{id}", [\kernel\modules\user\controllers\UserController::class, 'actionEdit']);
App::$collector->get('/delete/{id}', [\kernel\modules\user\controllers\UserController::class, 'actionDelete']);
});
App::$collector->group(["prefix" => "settings"], function (RouteCollector $router){
App::$collector->group(["prefix" => "menu"], function (RouteCollector $router){
App::$collector->get('/', [\kernel\modules\menu\controllers\MenuController::class, 'actionIndex']);

View File

@ -0,0 +1,39 @@
<?php
namespace kernel\services;
use kernel\helpers\Manifest;
use kernel\models\Option;
class ModuleService
{
public function getModuleInfo(string $module): false|array|string
{
$info = [];
$info['path'] = $module;
if (file_exists($module . "/manifest.json")){
$manifest = file_get_contents($module . "/manifest.json");
$manifest = Manifest::getWithVars($manifest);
$info = array_merge($info, $manifest);
}
return $info;
}
public function isActive(string $slug): bool
{
$active_modules= Option::where("key", "active_modules")->first();
if ($active_modules){
$path = json_decode($active_modules->value);
foreach ($path->modules as $p){
if ($p === $slug){
return true;
}
}
}
return false;
}
}

View File

@ -0,0 +1,41 @@
<?php
/**
* @var array $modules_info
* @var \kernel\services\ModuleService $moduleService
*/
$meta = [];
$meta['columns'] = [
"name" => "Название",
"author" => "Автор",
"version" => "Версия",
"description" => "Описание"
];
$meta['params'] = ["class" => "table table-bordered"];
$meta['perPage'] = 10;
$meta['baseUrl'] = "/admin/module";
$meta['currentPage'] = 1;
$meta['total'] = count($modules_info);
$info_to_table['meta'] = $meta;
$info_to_table['data'] = $modules_info;
$table = new \Itguild\Tables\ListJsonTable(json_encode($info_to_table, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
$table->addAction(function ($row, $url) use ($moduleService){
$slug = $row['slug'];
if ($moduleService->isActive($slug)){
$label = "Деактивировать";
$btn_type = "warning";
}
else {
$label = "Активировать";
$btn_type = "primary";
}
$btn = "<a class='btn btn-$btn_type' href='$url/activate/?slug=$slug' style='margin: 3px; width: 150px;' >$label</a>";
return $btn;
});
$table->create();
$table->render();

Binary file not shown.

View File

@ -1,6 +1,7 @@
{
"name": "Custom",
"version": "0.1",
"author": "Kavalar",
"slug": "custom",
"description": "Custom admin theme",
"preview": "nrnv2024_640x360.jpg",