From a9951102c07c67bcb341b350237f041a6b3c386f Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Tue, 10 Dec 2024 17:01:25 +0300 Subject: [PATCH] construct module --- .../Еtest/controllers/ЕtestController.php | 8 ++ app/modules/Еtest/manifest.json | 11 ++ app/modules/Еtest/routs/Еtest.php | 2 + app/modules/Еtest/ЕtestModule.php | 8 ++ .../Еtest/controllers/ЕtestController.php | 104 ++++++++++++++++++ kernel/app_modules/Еtest/routs/Еtest.php | 20 ++++ kernel/app_modules/Еtest/ЕtestModule.php | 31 ++++++ .../console/controllers/ModuleController.php | 20 +++- kernel/services/ModuleService.php | 62 ++++++++--- .../controllers/app_controller_template | 2 +- .../models/forms/create_form_template | 25 +++++ kernel/templates/models/model_template | 47 ++++++++ .../module_files/app_module_file_template | 8 ++ .../module_files/kernel_module_file_template | 31 ++++++ kernel/templates/routs/app_routs_template | 2 + kernel/templates/routs/kernel_routs_template | 20 ++++ 16 files changed, 386 insertions(+), 15 deletions(-) create mode 100644 app/modules/Еtest/controllers/ЕtestController.php create mode 100644 app/modules/Еtest/manifest.json create mode 100644 app/modules/Еtest/routs/Еtest.php create mode 100644 app/modules/Еtest/ЕtestModule.php create mode 100644 kernel/app_modules/Еtest/controllers/ЕtestController.php create mode 100644 kernel/app_modules/Еtest/routs/Еtest.php create mode 100644 kernel/app_modules/Еtest/ЕtestModule.php create mode 100644 kernel/templates/models/forms/create_form_template create mode 100644 kernel/templates/models/model_template create mode 100644 kernel/templates/module_files/app_module_file_template create mode 100644 kernel/templates/module_files/kernel_module_file_template create mode 100644 kernel/templates/routs/app_routs_template create mode 100644 kernel/templates/routs/kernel_routs_template diff --git a/app/modules/Еtest/controllers/ЕtestController.php b/app/modules/Еtest/controllers/ЕtestController.php new file mode 100644 index 0000000..c52a0ab --- /dev/null +++ b/app/modules/Еtest/controllers/ЕtestController.php @@ -0,0 +1,8 @@ +cgView->viewPath = KERNEL_APP_MODULES_DIR . "/Еtest/views/"; + $this->ЕtestService = new ЕtestService(); + } + + public function actionCreate(): void + { + $this->cgView->render("form.php"); + } + + #[NoReturn] public function actionAdd(): void + { + $ЕtestForm = new CreateЕtestForm(); + $ЕtestForm->load($_REQUEST); + if ($ЕtestForm->validate()){ + $Еtest = $this->ЕtestService->create($ЕtestForm); + if ($Еtest){ + $this->redirect("/admin/Еtest/view/" . $Еtest->id); + } + } + $this->redirect("/admin/Еtest/create"); + } + + public function actionIndex($page_number = 1): void + { + $this->cgView->render("index.php", ['page_number' => $page_number]); + } + + /** + * @throws Exception + */ + public function actionView($id): void + { + $Еtest = Еtest::find($id); + + if (!$Еtest){ + throw new Exception(message: "The Еtest not found"); + } + $this->cgView->render("view.php", ['Еtest' => $Еtest]); + } + + /** + * @throws Exception + */ + public function actionUpdate($id): void + { + $model = Еtest::find($id); + if (!$model){ + throw new Exception(message: "The Еtest not found"); + } + + $this->cgView->render("form.php", ['model' => $model]); + } + + /** + * @throws Exception + */ + public function actionEdit($id): void + { + $Еtest = Еtest::find($id); + if (!$Еtest){ + throw new Exception(message: "The Еtest not found"); + } + $ЕtestForm = new CreateЕtestForm(); + $ЕtestService = new ЕtestService(); + $ЕtestForm->load($_REQUEST); + if ($ЕtestForm->validate()) { + $Еtest = $ЕtestService->update($ЕtestForm, $Еtest); + if ($Еtest) { + $this->redirect("/admin/Еtest/view/" . $Еtest->id); + } + } + $this->redirect("/admin/Еtest/update/" . $id); + } + + #[NoReturn] public function actionDelete($id): void + { + $Еtest = Еtest::find($id)->first(); + $Еtest->delete(); + $this->redirect("/admin/Еtest/"); + } +} \ No newline at end of file diff --git a/kernel/app_modules/Еtest/routs/Еtest.php b/kernel/app_modules/Еtest/routs/Еtest.php new file mode 100644 index 0000000..94a11f4 --- /dev/null +++ b/kernel/app_modules/Еtest/routs/Еtest.php @@ -0,0 +1,20 @@ +group(["prefix" => "admin"], function (CgRouteCollector $router) { + App::$collector->group(["before" => "auth"], function (RouteCollector $router) { + App::$collector->group(["prefix" => "Еtest"], function (CGRouteCollector $router) { + App::$collector->get('/', [\app\modules\Еtest\controllers\ЕtestController::class, 'actionIndex']); + App::$collector->get('/page/{page_number}', [\app\modules\Еtest\controllers\ЕtestController::class, 'actionIndex']); + App::$collector->get('/create', [\app\modules\Еtest\controllers\ЕtestController::class, 'actionCreate']); + App::$collector->post("/", [\app\modules\Еtest\controllers\ЕtestController::class, 'actionAdd']); + App::$collector->get('/view/{id}', [\app\modules\Еtest\controllers\ЕtestController::class, 'actionView']); + App::$collector->any('/update/{id}', [\app\modules\Еtest\controllers\ЕtestController::class, 'actionUpdate']); + App::$collector->any("/edit/{id}", [\app\modules\Еtest\controllers\ЕtestController::class, 'actionEdit']); + App::$collector->get('/delete/{id}', [\app\modules\Еtest\controllers\ЕtestController::class, 'actionDelete']); + }); + }); +}); \ No newline at end of file diff --git a/kernel/app_modules/Еtest/ЕtestModule.php b/kernel/app_modules/Еtest/ЕtestModule.php new file mode 100644 index 0000000..c5a03a4 --- /dev/null +++ b/kernel/app_modules/Еtest/ЕtestModule.php @@ -0,0 +1,31 @@ +menuService = new MenuService(); + } + + public function init(): void + { + $this->menuService->createItem([ + "label" => "Тест", + "url" => "/admin/Еtest", + "slug" => "Еtest", + ]); + } + + public function deactivate(): void + { + $this->menuService->removeItemBySlug("Еtest"); + } +} \ No newline at end of file diff --git a/kernel/console/controllers/ModuleController.php b/kernel/console/controllers/ModuleController.php index 7ea9a63..534170e 100644 --- a/kernel/console/controllers/ModuleController.php +++ b/kernel/console/controllers/ModuleController.php @@ -92,13 +92,18 @@ class ModuleController extends ConsoleController public function actionConstructModule(): void { + $this->out->r("Введите slug модуля:", 'yellow'); + $slug = substr(fgets(STDIN), 0, -1); + $slug = strtolower($slug); + $this->out->r("Введите название модуля:", 'yellow'); $name = substr(fgets(STDIN), 0, -1); $this->out->r("Введите автора модуля:", 'yellow'); $author = substr(fgets(STDIN), 0, -1); - $slug = strtolower($name); + $this->out->r("Введите название пунтка меню для модуля:", 'yellow'); + $label = substr(fgets(STDIN), 0, -1); $moduleService = new ModuleService(); $moduleService->createDirs($slug); @@ -108,9 +113,22 @@ class ModuleController extends ConsoleController 'author' => $author, 'slug' => $slug ]); + $this->out->r("manifest.json создан", 'green'); $moduleService->createControllers($slug); + $this->out->r("Контроллеры созданы", 'green'); + $moduleService->createRouts($slug); + $this->out->r("Роуты созданы", 'green'); + + $moduleService->createModuleClassFiles($slug, $label); + $this->out->r("Файлы модуля созданы", 'green'); + + $moduleService->createModel($slug); + $this->out->r("Модель создана", 'green'); + + $moduleService->createFormModel($slug); + $this->out->r("Форма валидации для модели создана", 'green'); } diff --git a/kernel/services/ModuleService.php b/kernel/services/ModuleService.php index b75026b..92d8a3d 100644 --- a/kernel/services/ModuleService.php +++ b/kernel/services/ModuleService.php @@ -535,6 +535,7 @@ class ModuleService mkdir(KERNEL_APP_MODULES_DIR . "/$slug/controllers"); mkdir(KERNEL_APP_MODULES_DIR . "/$slug/migrations"); mkdir(KERNEL_APP_MODULES_DIR . "/$slug/models"); + mkdir(KERNEL_APP_MODULES_DIR . "/$slug/models/forms"); mkdir(KERNEL_APP_MODULES_DIR . "/$slug/routs"); mkdir(KERNEL_APP_MODULES_DIR . "/$slug/views"); @@ -561,18 +562,6 @@ class ModuleService $data .= " \"migration_path\": \"migrations\"\n"; $data .= "}"; -// $data = "{ -// \"name\": \"$name\", -// \"version\": \"0.2\", -// \"author\": \"$author\", -// \"slug\": \"$slug\", -// \"description\": \"$name module\", -// \"module_class\": \"app\\\\modules\\\\$slug\\\\" . ucfirst($slug) . "Module\", -// \"module_class_file\": \"{APP}/modules/$slug/" . ucfirst($slug) . "Module.php\", -// \"routs\": \"routs/$slug.php\", -// \"migration_path\": \"migrations\" -//}"; - file_put_contents(APP_DIR . "/modules/$slug/manifest.json", $data); } @@ -582,7 +571,54 @@ class ModuleService $data = str_replace('{slug}', $slug, $data); $data = str_replace('{model}', ucfirst($slug), $data); file_put_contents(KERNEL_APP_MODULES_DIR . '/' . $slug . '/controllers/' . ucfirst($slug) . 'Controller.php', $data); -// file_put_contents(APP_DIR . '/modules/' . $slug . '/controllers/' . ucfirst($slug) . 'Controller.php', $data ); + + $data = file_get_contents(KERNEL_DIR . '/templates/controllers/app_controller_template'); + $data = str_replace('{slug}', $slug, $data); + $data = str_replace('{model}', ucfirst($slug), $data); + file_put_contents(APP_DIR . '/modules/' . $slug . '/controllers/' . ucfirst($slug) . 'Controller.php', $data ); + } + + public function createRouts(string $slug): void + { + $data = file_get_contents(KERNEL_DIR . '/templates/routs/kernel_routs_template'); + $data = str_replace('{slug}', $slug, $data); + $data = str_replace('{model}', ucfirst($slug), $data); + file_put_contents(KERNEL_APP_MODULES_DIR . '/' . $slug . '/routs/' . $slug . '.php', $data); + + $data = file_get_contents(KERNEL_DIR . '/templates/routs/app_routs_template'); + $data = str_replace('{slug}', $slug, $data); + $data = str_replace('{model}', ucfirst($slug), $data); + file_put_contents(APP_DIR . '/modules/' . $slug . '/routs/' . $slug . '.php', $data ); + } + + public function createModuleClassFiles(string $slug, string $label): void + { + $data = file_get_contents(KERNEL_DIR . '/templates/module_files/kernel_module_file_template'); + $data = str_replace('{slug}', $slug, $data); + $data = str_replace('{model}', ucfirst($slug), $data); + $data = str_replace('{label}', $label, $data); + file_put_contents(KERNEL_APP_MODULES_DIR . '/' . $slug . '/' . ucfirst($slug) . 'Module.php', $data); + + $data = file_get_contents(KERNEL_DIR . '/templates/module_files/app_module_file_template'); + $data = str_replace('{slug}', $slug, $data); + $data = str_replace('{model}', ucfirst($slug), $data); + file_put_contents(APP_DIR . '/modules/' . $slug . '/' . ucfirst($slug) . 'Module.php', $data); + } + + public function createModel(string $slug): void + { + $data = file_get_contents(KERNEL_DIR . '/templates/models/model_template'); + $data = str_replace('{slug}', $slug, $data); + $data = str_replace('{model}', ucfirst($slug), $data); + file_put_contents(KERNEL_APP_MODULES_DIR . '/' . $slug . '/models/' . ucfirst($slug) . '.php', $data); + } + + public function createFormModel(string $slug): void + { + $data = file_get_contents(KERNEL_DIR . '/templates/models/forms/create_form_template'); + $data = str_replace('{slug}', $slug, $data); + $data = str_replace('{model}', ucfirst($slug), $data); + file_put_contents(KERNEL_APP_MODULES_DIR . '/' . $slug . '/models/forms/Create' . ucfirst($slug) . 'Form.php', $data); } } \ No newline at end of file diff --git a/kernel/templates/controllers/app_controller_template b/kernel/templates/controllers/app_controller_template index 4c23652..03e0772 100644 --- a/kernel/templates/controllers/app_controller_template +++ b/kernel/templates/controllers/app_controller_template @@ -2,7 +2,7 @@ namespace app\modules\{slug}\controllers; -class {moduleController} extends \kernel\app_modules\{slug}\controllers\{moduleController} +class {model}Controller extends \kernel\app_modules\{slug}\controllers\{model}Controller { } \ No newline at end of file diff --git a/kernel/templates/models/forms/create_form_template b/kernel/templates/models/forms/create_form_template new file mode 100644 index 0000000..e5b4f2b --- /dev/null +++ b/kernel/templates/models/forms/create_form_template @@ -0,0 +1,25 @@ + 'required|min-str-len:5|max-str-len:30', + // 'entity' => 'required', + // 'slug' => '', + // 'status' => '' + // ]; + return [ + + ]; + } + +} \ No newline at end of file diff --git a/kernel/templates/models/model_template b/kernel/templates/models/model_template new file mode 100644 index 0000000..cc6f55a --- /dev/null +++ b/kernel/templates/models/model_template @@ -0,0 +1,47 @@ + 'Заголовок', + // 'entity' => 'Сущность', + // 'slug' => 'Slug', + // 'status' => 'Статус', + // ] + + return [ + + ]; + } + + /** + * @return string[] + */ + public static function getStatus(): array + { + return [ + self::DISABLE_STATUS => "Не активный", + self::ACTIVE_STATUS => "Активный", + ]; + } + +} \ No newline at end of file diff --git a/kernel/templates/module_files/app_module_file_template b/kernel/templates/module_files/app_module_file_template new file mode 100644 index 0000000..ef58aee --- /dev/null +++ b/kernel/templates/module_files/app_module_file_template @@ -0,0 +1,8 @@ +menuService = new MenuService(); + } + + public function init(): void + { + $this->menuService->createItem([ + "label" => "{label}", + "url" => "/admin/{slug}", + "slug" => "{slug}", + ]); + } + + public function deactivate(): void + { + $this->menuService->removeItemBySlug("{slug}"); + } +} \ No newline at end of file diff --git a/kernel/templates/routs/app_routs_template b/kernel/templates/routs/app_routs_template new file mode 100644 index 0000000..a9558ba --- /dev/null +++ b/kernel/templates/routs/app_routs_template @@ -0,0 +1,2 @@ +group(["prefix" => "admin"], function (CgRouteCollector $router) { + App::$collector->group(["before" => "auth"], function (RouteCollector $router) { + App::$collector->group(["prefix" => "{slug}"], function (CGRouteCollector $router) { + App::$collector->get('/', [\app\modules\{slug}\controllers\{model}Controller::class, 'actionIndex']); + App::$collector->get('/page/{page_number}', [\app\modules\{slug}\controllers\{model}Controller::class, 'actionIndex']); + App::$collector->get('/create', [\app\modules\{slug}\controllers\{model}Controller::class, 'actionCreate']); + App::$collector->post("/", [\app\modules\{slug}\controllers\{model}Controller::class, 'actionAdd']); + App::$collector->get('/view/{id}', [\app\modules\{slug}\controllers\{model}Controller::class, 'actionView']); + App::$collector->any('/update/{id}', [\app\modules\{slug}\controllers\{model}Controller::class, 'actionUpdate']); + App::$collector->any("/edit/{id}", [\app\modules\{slug}\controllers\{model}Controller::class, 'actionEdit']); + App::$collector->get('/delete/{id}', [\app\modules\{slug}\controllers\{model}Controller::class, 'actionDelete']); + }); + }); +}); \ No newline at end of file