From f20ba63277534c084730b1c16b0a9255c1af8fe4 Mon Sep 17 00:00:00 2001 From: Kavalar Date: Fri, 13 Sep 2024 16:51:41 +0300 Subject: [PATCH] module routing --- app/modules/tag/manifest.json | 8 +++++ kernel/App.php | 9 ++++++ kernel/modules/user/manifest.json | 3 +- kernel/routs/admin.php | 10 ------ kernel/services/ModuleService.php | 51 ++++++++++++++++++++++++++----- 5 files changed, 62 insertions(+), 19 deletions(-) create mode 100644 app/modules/tag/manifest.json diff --git a/app/modules/tag/manifest.json b/app/modules/tag/manifest.json new file mode 100644 index 0000000..2612837 --- /dev/null +++ b/app/modules/tag/manifest.json @@ -0,0 +1,8 @@ +{ + "name": "Tags", + "version": "0.1", + "author": "ITGuild", + "slug": "tag", + "description": "Tags module", + "module_class": "TagModule" +} \ No newline at end of file diff --git a/kernel/App.php b/kernel/App.php index e9ef17b..855e082 100644 --- a/kernel/App.php +++ b/kernel/App.php @@ -4,6 +4,8 @@ namespace kernel; +use kernel\helpers\Debug; +use kernel\services\ModuleService; use Phroute\Phroute\Dispatcher; class App @@ -15,6 +17,8 @@ class App static Header $header; + public ModuleService $moduleService; + public static Database $db; public function run(): void @@ -29,6 +33,7 @@ class App public function load(): static { + $this->moduleService = new ModuleService(); App::$collector = new CgRouteCollector(); $this->setRouting(); @@ -39,6 +44,10 @@ class App { include KERNEL_DIR . "/routs/admin.php"; include ROOT_DIR . "/rout.php"; + $modules_routs = $this->moduleService->getModulesRouts(); + foreach ($modules_routs as $rout){ + include "$rout"; + } } public static function create(): App diff --git a/kernel/modules/user/manifest.json b/kernel/modules/user/manifest.json index d5bd6c9..f3972e1 100644 --- a/kernel/modules/user/manifest.json +++ b/kernel/modules/user/manifest.json @@ -4,5 +4,6 @@ "author": "ITGuild", "slug": "user", "description": "User module", - "module_class": "UserModule" + "module_class": "UserModule", + "routs": "routs/user.php" } \ No newline at end of file diff --git a/kernel/routs/admin.php b/kernel/routs/admin.php index a4f89fd..74738b7 100644 --- a/kernel/routs/admin.php +++ b/kernel/routs/admin.php @@ -21,16 +21,6 @@ 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']); diff --git a/kernel/services/ModuleService.php b/kernel/services/ModuleService.php index b583eb0..d9d0364 100644 --- a/kernel/services/ModuleService.php +++ b/kernel/services/ModuleService.php @@ -14,7 +14,7 @@ class ModuleService { $info = []; $info['path'] = $module; - if (file_exists($module . "/manifest.json")){ + if (file_exists($module . "/manifest.json")) { $manifest = file_get_contents($module . "/manifest.json"); $manifest = Manifest::getWithVars($manifest); $info = array_merge($info, $manifest); @@ -26,10 +26,10 @@ class ModuleService public function isActive(string $slug): bool { $active_modules = Option::where("key", "active_modules")->first(); - if ($active_modules){ + if ($active_modules) { $path = json_decode($active_modules->value); - foreach ($path->modules as $p){ - if ($p === $slug){ + foreach ($path->modules as $p) { + if ($p === $slug) { return true; } } @@ -56,16 +56,16 @@ class ModuleService { $module_paths = Option::where("key", "module_paths")->first(); $dirs = []; - if ($module_paths){ + if ($module_paths) { $path = json_decode($module_paths->value); - foreach ($path->paths as $p){ + foreach ($path->paths as $p) { $dirs[] = getConst($p); } } - foreach ($dirs as $dir){ + foreach ($dirs as $dir) { foreach (new DirectoryIterator($dir) as $fileInfo) { - if(basename($fileInfo->getPathname()) === $slug) { + if (basename($fileInfo->getPathname()) === $slug) { return $fileInfo->getPathname(); } } @@ -73,4 +73,39 @@ class ModuleService return false; } + public function getActiveModules(): array + { + $modules = []; + $module_paths = Option::where("key", "module_paths")->first(); + $dirs = []; + if ($module_paths) { + $path = json_decode($module_paths->value); + foreach ($path->paths as $p) { + $dirs[] = getConst($p); + } + } + + foreach ($dirs as $dir) { + foreach (new DirectoryIterator($dir) as $fileInfo) { + if($fileInfo->isDot()) continue; + $modules[] = $this->getModuleInfo($fileInfo->getPathname()); + } + } + + return $modules; + } + + public function getModulesRouts(): array + { + $routs = []; + $modules = $this->getActiveModules(); + foreach ($modules as $module){ + if (isset($module['routs'])){ + $routs[] = $module['path'] . "/" . $module['routs']; + } + } + + return $routs; + } + } \ No newline at end of file