From 63d96eb46183c5ff6248dd9f0de9571d6123b6a8 Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Thu, 12 Dec 2024 14:06:25 +0300 Subject: [PATCH] create module update --- bootstrap.php | 3 +- .../console/controllers/ModuleController.php | 30 +--- kernel/services/ModuleService.php | 142 ++++-------------- .../controllers/kernel_controller_template | 6 - kernel/templates/manifests/manifest_template | 11 ++ kernel/templates/services/service_template | 1 - 6 files changed, 47 insertions(+), 146 deletions(-) create mode 100644 kernel/templates/manifests/manifest_template diff --git a/bootstrap.php b/bootstrap.php index 0a85d73..b9b36fc 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -13,7 +13,7 @@ const KERNEL_MODULES_DIR = __DIR__ . "/kernel/modules"; const KERNEL_ADMIN_THEMES_DIR = __DIR__ . "/kernel/admin_themes"; const CONSOLE_DIR = __DIR__ . "/kernel/console"; const RESOURCES_DIR = __DIR__ . "/resources"; - +const KERNEL_TEMPLATES_DIR = __DIR__ . "/kernel/templates"; const KERNEL_APP_MODULES_DIR = KERNEL_DIR . "/app_modules"; const APP_DIR = ROOT_DIR . "/app"; @@ -29,6 +29,7 @@ function getConst($text): array|false|string "{KERNEL}" => KERNEL_DIR, "{KERNEL_MODULES}" => KERNEL_MODULES_DIR, "{KERNEL_APP_MODULES}" => KERNEL_APP_MODULES_DIR, + "{KERNEL_TEMPLATES}" => KERNEL_TEMPLATES_DIR, "{CONSOLE}" => CONSOLE_DIR, "{APP}" => APP_DIR, ]; diff --git a/kernel/console/controllers/ModuleController.php b/kernel/console/controllers/ModuleController.php index b181600..92d05a4 100644 --- a/kernel/console/controllers/ModuleController.php +++ b/kernel/console/controllers/ModuleController.php @@ -108,34 +108,14 @@ class ModuleController extends ConsoleController $moduleService = new ModuleService(); $moduleService->createDirs($slug); - $moduleService->createManifest([ - 'name' => $name, + $moduleService->createModuleByParams([ + 'slug' => $slug, + 'model' => ucfirst($slug), 'author' => $author, - 'slug' => $slug + 'name' => $name, ]); - $this->out->r("manifest.json создан", 'green'); - - $moduleService->createControllers($slug); - $this->out->r("Контроллеры созданы", 'green'); - - $moduleService->createRoutes($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'); - - $moduleService->createService($slug); - $this->out->r("Сервис создан", 'green'); - - $moduleService->createViews($slug); - $this->out->r("Представления созданы", 'green'); + $this->out->r("Модуль $slug создан", 'green'); } } \ No newline at end of file diff --git a/kernel/services/ModuleService.php b/kernel/services/ModuleService.php index 94ca517..97240ee 100644 --- a/kernel/services/ModuleService.php +++ b/kernel/services/ModuleService.php @@ -444,7 +444,7 @@ class ModuleService public function isLastVersion(string $slug): bool { - if ($this->isServerAvailable()){ + if ($this->isServerAvailable()) { $modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug'); $modules_info = json_decode($modules_info->getBody()->getContents(), true); @@ -473,10 +473,10 @@ class ModuleService public function isShopModule(string $slug): bool { - if ($this->isServerAvailable()){ + if ($this->isServerAvailable()) { $modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug'); - if (!$this->issetModuleShopToken()){ + if (!$this->issetModuleShopToken()) { return false; } @@ -505,7 +505,7 @@ class ModuleService public function isServerAvailable(): bool { - if (null !== $this->serverAvailable){ + if (null !== $this->serverAvailable) { return $this->serverAvailable; } @@ -522,7 +522,7 @@ class ModuleService public function issetModuleShopToken(): bool { - if (!empty($_ENV['MODULE_SHOP_TOKEN'])){ + if (!empty($_ENV['MODULE_SHOP_TOKEN'])) { return true; } @@ -534,7 +534,7 @@ class ModuleService 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/service"); + mkdir(KERNEL_APP_MODULES_DIR . "/$slug/services"); mkdir(KERNEL_APP_MODULES_DIR . "/$slug/models"); mkdir(KERNEL_APP_MODULES_DIR . "/$slug/models/forms"); mkdir(KERNEL_APP_MODULES_DIR . "/$slug/routs"); @@ -545,120 +545,36 @@ class ModuleService mkdir(APP_DIR . "/modules/$slug/routs"); } - public function createManifest(array $params): void + public function createModuleByParams(array $params): void { - $name = $params['name'] ?? ''; - $author = $params['author'] ?? ''; - $slug = $params['slug'] ?? ''; + $slug = $params['slug']; + $model = $params['model']; - $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 .= "}"; - - file_put_contents(APP_DIR . "/modules/$slug/manifest.json", $data); + $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/manifests/manifest_template', APP_DIR . "/modules/$slug/manifest.json", $params); + $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/controllers/kernel_controller_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/controllers/' . $model . 'Controller.php', $params); + $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/controllers/app_controller_template', APP_DIR . '/modules/' . $slug . '/controllers/' . $model . 'Controller.php', $params); + $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/routs/kernel_routs_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/routs/' . $slug . '.php', $params); + $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/routs/app_routs_template', APP_DIR . '/modules/' . $slug . '/routs/' . $slug . '.php', $params); + $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/module_files/kernel_module_file_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/' . $model . 'Module.php', $params); + $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/module_files/app_module_file_template', APP_DIR . '/modules/' . $slug . '/' . $model . 'Module.php', $params); + $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/models/model_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/models/' . $model . '.php', $params); + $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/models/forms/create_form_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/models/forms/Create' . $model . 'Form.php', $params); + $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/services/service_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/services/' . $model . 'Service.php', $params); + $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/views/index_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/views/index.php', $params); + $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/views/view_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/views/view.php', $params); + $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/views/form_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/views/form.php', $params); } - public function createControllers(string $slug): void + public function createModuleFileByTemplate(string $templatePath, string $filePath, array $params): 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); + $data = file_get_contents($templatePath); - $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 ); - } + $data = str_replace('{slug}', $params['slug'], $data); + $data = str_replace('{model}', $params['model'], $data); + $data = str_replace('{name}', $params['name'], $data); + $data = str_replace('{author}', $params['author'], $data); - public function createRoutes(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); - } - - public function createService(string $slug): void - { - $data = file_get_contents(KERNEL_DIR . '/templates/services/service_template'); - $data = str_replace('{slug}', $slug, $data); - $data = str_replace('{model}', ucfirst($slug), $data); - file_put_contents(KERNEL_APP_MODULES_DIR . '/' . $slug . '/service/' . ucfirst($slug) . 'Service.php', $data); - } - - public function createViews(string $slug): void - { - $this->createIndex($slug); - $this->createView($slug); - $this->createForm($slug); - } - - public function createIndex(string $slug): void - { - $data = file_get_contents(KERNEL_DIR . '/templates/views/index_template'); - $data = str_replace('{slug}', $slug, $data); - $data = str_replace('{model}', ucfirst($slug), $data); - file_put_contents(KERNEL_APP_MODULES_DIR . '/' . $slug . '/views/index.php', $data); - } - - public function createView(string $slug): void - { - $data = file_get_contents(KERNEL_DIR . '/templates/views/view_template'); - $data = str_replace('{slug}', $slug, $data); - $data = str_replace('{model}', ucfirst($slug), $data); - file_put_contents(KERNEL_APP_MODULES_DIR . '/' . $slug . '/views/view.php', $data); - } - - public function createForm(string $slug): void - { - $data = file_get_contents(KERNEL_DIR . '/templates/views/form_template'); - $data = str_replace('{slug}', $slug, $data); - $data = str_replace('{model}', ucfirst($slug), $data); - file_put_contents(KERNEL_APP_MODULES_DIR . '/' . $slug . '/views/form.php', $data); + file_put_contents($filePath, $data); } } \ No newline at end of file diff --git a/kernel/templates/controllers/kernel_controller_template b/kernel/templates/controllers/kernel_controller_template index 8afd414..80309ad 100644 --- a/kernel/templates/controllers/kernel_controller_template +++ b/kernel/templates/controllers/kernel_controller_template @@ -8,12 +8,6 @@ use kernel\AdminController; use kernel\app_modules\{slug}\models\forms\Create{model}Form; use kernel\app_modules\{slug}\models\{model}; use kernel\app_modules\{slug}\service\{model}Service; -use kernel\EntityRelation; -use kernel\Flash; -use kernel\helpers\Debug; -use kernel\models\Option; -use kernel\modules\menu\service\MenuService; -use kernel\Request; class {model}Controller extends AdminController { diff --git a/kernel/templates/manifests/manifest_template b/kernel/templates/manifests/manifest_template new file mode 100644 index 0000000..7704dd4 --- /dev/null +++ b/kernel/templates/manifests/manifest_template @@ -0,0 +1,11 @@ +{ + "name": "{name}", + "version": "0.1", + "author": "{author}", + "slug": "{slug}", + "description": "{name} module", + "module_class": "app\\modules\\{slug}\\{model}Module", + "module_class_file": "{APP}/modules/{slug}/{model}Module.php", + "routs": "routs/{slug}.php", + "migration_path": "migrations" +} \ No newline at end of file diff --git a/kernel/templates/services/service_template b/kernel/templates/services/service_template index ba4277d..21878db 100644 --- a/kernel/templates/services/service_template +++ b/kernel/templates/services/service_template @@ -3,7 +3,6 @@ namespace kernel\app_modules\{slug}\service; use kernel\helpers\Debug; -use kernel\helpers\Slug; use kernel\app_modules\{slug}\models\{model}; use kernel\FormModel;