create module update

This commit is contained in:
Билай Станислав 2024-12-12 14:06:25 +03:00
parent f7253bafe9
commit 63d96eb461
6 changed files with 47 additions and 146 deletions

View File

@ -13,7 +13,7 @@ const KERNEL_MODULES_DIR = __DIR__ . "/kernel/modules";
const KERNEL_ADMIN_THEMES_DIR = __DIR__ . "/kernel/admin_themes"; const KERNEL_ADMIN_THEMES_DIR = __DIR__ . "/kernel/admin_themes";
const CONSOLE_DIR = __DIR__ . "/kernel/console"; const CONSOLE_DIR = __DIR__ . "/kernel/console";
const RESOURCES_DIR = __DIR__ . "/resources"; const RESOURCES_DIR = __DIR__ . "/resources";
const KERNEL_TEMPLATES_DIR = __DIR__ . "/kernel/templates";
const KERNEL_APP_MODULES_DIR = KERNEL_DIR . "/app_modules"; const KERNEL_APP_MODULES_DIR = KERNEL_DIR . "/app_modules";
const APP_DIR = ROOT_DIR . "/app"; const APP_DIR = ROOT_DIR . "/app";
@ -29,6 +29,7 @@ function getConst($text): array|false|string
"{KERNEL}" => KERNEL_DIR, "{KERNEL}" => KERNEL_DIR,
"{KERNEL_MODULES}" => KERNEL_MODULES_DIR, "{KERNEL_MODULES}" => KERNEL_MODULES_DIR,
"{KERNEL_APP_MODULES}" => KERNEL_APP_MODULES_DIR, "{KERNEL_APP_MODULES}" => KERNEL_APP_MODULES_DIR,
"{KERNEL_TEMPLATES}" => KERNEL_TEMPLATES_DIR,
"{CONSOLE}" => CONSOLE_DIR, "{CONSOLE}" => CONSOLE_DIR,
"{APP}" => APP_DIR, "{APP}" => APP_DIR,
]; ];

View File

@ -108,34 +108,14 @@ class ModuleController extends ConsoleController
$moduleService = new ModuleService(); $moduleService = new ModuleService();
$moduleService->createDirs($slug); $moduleService->createDirs($slug);
$moduleService->createManifest([ $moduleService->createModuleByParams([
'name' => $name, 'slug' => $slug,
'model' => ucfirst($slug),
'author' => $author, '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');
} }
} }

View File

@ -444,7 +444,7 @@ class ModuleService
public function isLastVersion(string $slug): bool 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 = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
$modules_info = json_decode($modules_info->getBody()->getContents(), true); $modules_info = json_decode($modules_info->getBody()->getContents(), true);
@ -473,10 +473,10 @@ class ModuleService
public function isShopModule(string $slug): bool 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'); $modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
if (!$this->issetModuleShopToken()){ if (!$this->issetModuleShopToken()) {
return false; return false;
} }
@ -505,7 +505,7 @@ class ModuleService
public function isServerAvailable(): bool public function isServerAvailable(): bool
{ {
if (null !== $this->serverAvailable){ if (null !== $this->serverAvailable) {
return $this->serverAvailable; return $this->serverAvailable;
} }
@ -522,7 +522,7 @@ class ModuleService
public function issetModuleShopToken(): bool public function issetModuleShopToken(): bool
{ {
if (!empty($_ENV['MODULE_SHOP_TOKEN'])){ if (!empty($_ENV['MODULE_SHOP_TOKEN'])) {
return true; return true;
} }
@ -534,7 +534,7 @@ class ModuleService
mkdir(KERNEL_APP_MODULES_DIR . "/$slug"); mkdir(KERNEL_APP_MODULES_DIR . "/$slug");
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/controllers"); mkdir(KERNEL_APP_MODULES_DIR . "/$slug/controllers");
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/migrations"); 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");
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/models/forms"); mkdir(KERNEL_APP_MODULES_DIR . "/$slug/models/forms");
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/routs"); mkdir(KERNEL_APP_MODULES_DIR . "/$slug/routs");
@ -545,120 +545,36 @@ class ModuleService
mkdir(APP_DIR . "/modules/$slug/routs"); mkdir(APP_DIR . "/modules/$slug/routs");
} }
public function createManifest(array $params): void public function createModuleByParams(array $params): void
{ {
$name = $params['name'] ?? ''; $slug = $params['slug'];
$author = $params['author'] ?? ''; $model = $params['model'];
$slug = $params['slug'] ?? '';
$data = "{\n"; $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/manifests/manifest_template', APP_DIR . "/modules/$slug/manifest.json", $params);
$data .= " \"name\": \"$name\",\n"; $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/controllers/kernel_controller_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/controllers/' . $model . 'Controller.php', $params);
$data .= " \"version\": \"0.1\",\n"; $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/controllers/app_controller_template', APP_DIR . '/modules/' . $slug . '/controllers/' . $model . 'Controller.php', $params);
$data .= " \"author\": \"$author\",\n"; $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/routs/kernel_routs_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/routs/' . $slug . '.php', $params);
$data .= " \"slug\": \"$slug\",\n"; $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/routs/app_routs_template', APP_DIR . '/modules/' . $slug . '/routs/' . $slug . '.php', $params);
$data .= " \"description\": \"$name module\",\n"; $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/module_files/kernel_module_file_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/' . $model . 'Module.php', $params);
$data .= " \"module_class\": \"app\\\\modules\\\\$slug\\\\" . ucfirst($slug) . "Module\",\n"; $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/module_files/app_module_file_template', APP_DIR . '/modules/' . $slug . '/' . $model . 'Module.php', $params);
$data .= " \"module_class_file\": \"{APP}/modules/$slug/" . ucfirst($slug) . "Module.php\",\n"; $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/models/model_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/models/' . $model . '.php', $params);
$data .= " \"routs\": \"routs/$slug.php\",\n"; $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/models/forms/create_form_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/models/forms/Create' . $model . 'Form.php', $params);
$data .= " \"migration_path\": \"migrations\"\n"; $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/services/service_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/services/' . $model . 'Service.php', $params);
$data .= "}"; $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);
file_put_contents(APP_DIR . "/modules/$slug/manifest.json", $data); $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 = file_get_contents($templatePath);
$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(KERNEL_DIR . '/templates/controllers/app_controller_template'); $data = str_replace('{slug}', $params['slug'], $data);
$data = str_replace('{slug}', $slug, $data); $data = str_replace('{model}', $params['model'], $data);
$data = str_replace('{model}', ucfirst($slug), $data); $data = str_replace('{name}', $params['name'], $data);
file_put_contents(APP_DIR . '/modules/' . $slug . '/controllers/' . ucfirst($slug) . 'Controller.php', $data ); $data = str_replace('{author}', $params['author'], $data);
}
public function createRoutes(string $slug): void file_put_contents($filePath, $data);
{
$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);
} }
} }

View File

@ -8,12 +8,6 @@ use kernel\AdminController;
use kernel\app_modules\{slug}\models\forms\Create{model}Form; use kernel\app_modules\{slug}\models\forms\Create{model}Form;
use kernel\app_modules\{slug}\models\{model}; use kernel\app_modules\{slug}\models\{model};
use kernel\app_modules\{slug}\service\{model}Service; 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 class {model}Controller extends AdminController
{ {

View File

@ -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"
}

View File

@ -3,7 +3,6 @@
namespace kernel\app_modules\{slug}\service; namespace kernel\app_modules\{slug}\service;
use kernel\helpers\Debug; use kernel\helpers\Debug;
use kernel\helpers\Slug;
use kernel\app_modules\{slug}\models\{model}; use kernel\app_modules\{slug}\models\{model};
use kernel\FormModel; use kernel\FormModel;