From 3c0b78ea5677637fb168fa6cca3dd1a8ace08e0a Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Tue, 10 Dec 2024 15:28:34 +0300 Subject: [PATCH] templates controllers and module create in terminal --- .../console/controllers/ModuleController.php | 24 ++++ kernel/console/routs/cli.php | 1 + kernel/services/ModuleService.php | 56 ++++++++++ .../controllers/app_controller_template | 8 ++ .../controllers/kernel_controller_template | 104 ++++++++++++++++++ 5 files changed, 193 insertions(+) create mode 100644 kernel/templates/controllers/app_controller_template create mode 100644 kernel/templates/controllers/kernel_controller_template diff --git a/kernel/console/controllers/ModuleController.php b/kernel/console/controllers/ModuleController.php index db59f58..7ea9a63 100644 --- a/kernel/console/controllers/ModuleController.php +++ b/kernel/console/controllers/ModuleController.php @@ -90,4 +90,28 @@ class ModuleController extends ConsoleController } } + public function actionConstructModule(): void + { + $this->out->r("Введите название модуля:", 'yellow'); + $name = substr(fgets(STDIN), 0, -1); + + $this->out->r("Введите автора модуля:", 'yellow'); + $author = substr(fgets(STDIN), 0, -1); + + $slug = strtolower($name); + + $moduleService = new ModuleService(); + $moduleService->createDirs($slug); + + $moduleService->createManifest([ + 'name' => $name, + 'author' => $author, + 'slug' => $slug + ]); + + $moduleService->createControllers($slug); + + + } + } \ No newline at end of file diff --git a/kernel/console/routs/cli.php b/kernel/console/routs/cli.php index 693ff3c..869ad7b 100644 --- a/kernel/console/routs/cli.php +++ b/kernel/console/routs/cli.php @@ -30,6 +30,7 @@ App::$collector->group(["prefix" => "module"], callback: function (RouteCollecto App::$collector->console('uninstall', [\kernel\console\controllers\ModuleController::class, 'actionUninstallModule']); App::$collector->console('pack', [\kernel\console\controllers\ModuleController::class, 'actionPackModule']); App::$collector->console('update', [\kernel\console\controllers\ModuleController::class, 'actionUpdateModule']); + App::$collector->console('construct', [\kernel\console\controllers\ModuleController::class, 'actionConstructModule']); }); App::$collector->group(["prefix" => "kernel"], callback: function (RouteCollector $router){ diff --git a/kernel/services/ModuleService.php b/kernel/services/ModuleService.php index 50a3603..b75026b 100644 --- a/kernel/services/ModuleService.php +++ b/kernel/services/ModuleService.php @@ -529,4 +529,60 @@ class ModuleService return false; } + public function createDirs(string $slug): void + { + mkdir(KERNEL_APP_MODULES_DIR . "/$slug"); + 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/routs"); + mkdir(KERNEL_APP_MODULES_DIR . "/$slug/views"); + + mkdir(APP_DIR . "/modules/$slug"); + mkdir(APP_DIR . "/modules/$slug/controllers"); + mkdir(APP_DIR . "/modules/$slug/routs"); + } + + public function createManifest(array $params): void + { + $name = $params['name'] ?? ''; + $author = $params['author'] ?? ''; + $slug = $params['slug'] ?? ''; + + $data = "{\n"; + $data .= " \"name\": \"$name\",\n"; + $data .= " \"version\": \"0.1\",\n"; + $data .= " \"author\": \"$author\",\n"; + $data .= " \"slug\": \"$slug\",\n"; + $data .= " \"description\": \"$name module\",\n"; + $data .= " \"module_class\": \"app\\\\modules\\\\$slug\\\\" . ucfirst($slug) . "Module\",\n"; + $data .= " \"module_class_file\": \"{APP}/modules/$slug/" . ucfirst($slug) . "Module.php\",\n"; + $data .= " \"routs\": \"routs/$slug.php\",\n"; + $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); + } + + public function createControllers(string $slug): void + { + $data = file_get_contents(KERNEL_DIR . '/templates/controllers/kernel_controller_template'); + $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 ); + } + } \ No newline at end of file diff --git a/kernel/templates/controllers/app_controller_template b/kernel/templates/controllers/app_controller_template new file mode 100644 index 0000000..4c23652 --- /dev/null +++ b/kernel/templates/controllers/app_controller_template @@ -0,0 +1,8 @@ +cgView->viewPath = KERNEL_APP_MODULES_DIR . "/{slug}/views/"; + $this->{slug}Service = new {model}Service(); + } + + public function actionCreate(): void + { + $this->cgView->render("form.php"); + } + + #[NoReturn] public function actionAdd(): void + { + ${slug}Form = new Create{model}Form(); + ${slug}Form->load($_REQUEST); + if (${slug}Form->validate()){ + ${slug} = $this->{slug}Service->create(${slug}Form); + if (${slug}){ + $this->redirect("/admin/{slug}/view/" . ${slug}->id); + } + } + $this->redirect("/admin/{slug}/create"); + } + + public function actionIndex($page_number = 1): void + { + $this->cgView->render("index.php", ['page_number' => $page_number]); + } + + /** + * @throws Exception + */ + public function actionView($id): void + { + ${slug} = {model}::find($id); + + if (!${slug}){ + throw new Exception(message: "The {slug} not found"); + } + $this->cgView->render("view.php", ['{slug}' => ${slug}]); + } + + /** + * @throws Exception + */ + public function actionUpdate($id): void + { + $model = {model}::find($id); + if (!$model){ + throw new Exception(message: "The {slug} not found"); + } + + $this->cgView->render("form.php", ['model' => $model]); + } + + /** + * @throws Exception + */ + public function actionEdit($id): void + { + ${slug} = {model}::find($id); + if (!${slug}){ + throw new Exception(message: "The {slug} not found"); + } + ${slug}Form = new Create{model}Form(); + ${slug}Service = new {model}Service(); + ${slug}Form->load($_REQUEST); + if (${slug}Form->validate()) { + ${slug} = ${slug}Service->update(${slug}Form, ${slug}); + if (${slug}) { + $this->redirect("/admin/{slug}/view/" . ${slug}->id); + } + } + $this->redirect("/admin/{slug}/update/" . $id); + } + + #[NoReturn] public function actionDelete($id): void + { + ${slug} = {model}::find($id)->first(); + ${slug}->delete(); + $this->redirect("/admin/{slug}/"); + } +} \ No newline at end of file