From a48088581fbc9fb2b5f54121ce74d731ef1eaf9a Mon Sep 17 00:00:00 2001 From: Kavalar Date: Mon, 23 Sep 2024 17:03:42 +0300 Subject: [PATCH] admin init command --- ...083125_add_status_column_at_user_table.php | 0 .../2024_08_30_083939_create_menu_table.php | 0 .../2024_09_02_155116_create_option_table.php | 0 ...3_163221_add_slug_column_at_menu_table.php | 0 .../2024_09_23_092858_create_post_table.php | 0 .../controllers/AdminConsoleController.php | 94 +++++++++++++++++++ .../controllers/MigrationController.php | 28 +++--- kernel/console/routs/cli.php | 4 + .../admin_themes/AdminThemesModule.php | 24 ++++- kernel/modules/admin_themes/manifest.json | 3 +- kernel/modules/menu/MenuModule.php | 24 ++++- kernel/modules/menu/manifest.json | 3 +- .../2024_09_23_125545_create_menu_table.php | 34 +++++++ .../option/controllers/OptionController.php | 9 +- .../2024_09_23_125716_create_option_table.php | 31 ++++++ kernel/modules/option/models/Option.php | 13 +++ .../option/models/forms/CreateOptionForm.php | 14 ++- kernel/modules/option/routs/option.php | 6 +- .../modules/option/service/OptionService.php | 14 +++ kernel/modules/option/views/form.php | 2 +- .../2024_09_23_130017_create_post_table.php | 31 ++++++ .../2024_09_23_125827_create_user_table.php | 35 +++++++ kernel/routs/admin.php | 2 + kernel/services/MigrationService.php | 35 +++++++ resources/default/css/style.css | 4 +- 25 files changed, 376 insertions(+), 34 deletions(-) rename {migrations => _migrations}/2024_07_31_083125_add_status_column_at_user_table.php (100%) rename {migrations => _migrations}/2024_08_30_083939_create_menu_table.php (100%) rename {migrations => _migrations}/2024_09_02_155116_create_option_table.php (100%) rename {migrations => _migrations}/2024_09_13_163221_add_slug_column_at_menu_table.php (100%) rename {kernel/modules/post/migrations => _migrations}/2024_09_23_092858_create_post_table.php (100%) create mode 100644 kernel/console/controllers/AdminConsoleController.php create mode 100644 kernel/modules/menu/migrations/2024_09_23_125545_create_menu_table.php create mode 100644 kernel/modules/option/migrations/2024_09_23_125716_create_option_table.php create mode 100644 kernel/modules/post/migrations/2024_09_23_130017_create_post_table.php create mode 100644 kernel/modules/user/migrations/2024_09_23_125827_create_user_table.php create mode 100644 kernel/services/MigrationService.php diff --git a/migrations/2024_07_31_083125_add_status_column_at_user_table.php b/_migrations/2024_07_31_083125_add_status_column_at_user_table.php similarity index 100% rename from migrations/2024_07_31_083125_add_status_column_at_user_table.php rename to _migrations/2024_07_31_083125_add_status_column_at_user_table.php diff --git a/migrations/2024_08_30_083939_create_menu_table.php b/_migrations/2024_08_30_083939_create_menu_table.php similarity index 100% rename from migrations/2024_08_30_083939_create_menu_table.php rename to _migrations/2024_08_30_083939_create_menu_table.php diff --git a/migrations/2024_09_02_155116_create_option_table.php b/_migrations/2024_09_02_155116_create_option_table.php similarity index 100% rename from migrations/2024_09_02_155116_create_option_table.php rename to _migrations/2024_09_02_155116_create_option_table.php diff --git a/migrations/2024_09_13_163221_add_slug_column_at_menu_table.php b/_migrations/2024_09_13_163221_add_slug_column_at_menu_table.php similarity index 100% rename from migrations/2024_09_13_163221_add_slug_column_at_menu_table.php rename to _migrations/2024_09_13_163221_add_slug_column_at_menu_table.php diff --git a/kernel/modules/post/migrations/2024_09_23_092858_create_post_table.php b/_migrations/2024_09_23_092858_create_post_table.php similarity index 100% rename from kernel/modules/post/migrations/2024_09_23_092858_create_post_table.php rename to _migrations/2024_09_23_092858_create_post_table.php diff --git a/kernel/console/controllers/AdminConsoleController.php b/kernel/console/controllers/AdminConsoleController.php new file mode 100644 index 0000000..f655ac6 --- /dev/null +++ b/kernel/console/controllers/AdminConsoleController.php @@ -0,0 +1,94 @@ +migrationService = new MigrationService(); + $this->optionService = new OptionService(); + $this->menuService = new MenuService(); + } + + /** + * @throws \Exception + */ + public function actionInit(): void + { + $out = $this->migrationService->runAtPath("kernel/modules/menu/migrations"); + $this->out->r("create menu table", "green"); + + $out = $this->migrationService->runAtPath("kernel/modules/option/migrations"); + $this->out->r("create option table", "green"); + + $out = $this->migrationService->runAtPath("kernel/modules/user/migrations"); + $this->out->r("create user table", "green"); + + $out = $this->migrationService->runAtPath("kernel/modules/post/migrations"); + $this->out->r("create post table", "green"); + + $this->optionService->createFromParams( + key: "admin_theme_paths", + value: "{\"paths\": [\"{KERNEL_ADMIN_THEMES}\", \"{APP}/admin_themes\"]}", + label: "Пути к темам админпанели" + ); + $this->out->r("create option admin_theme_paths", "green"); + + $this->optionService->createFromParams( + key: "active_admin_theme", + value: "/home/kavlar/php/MicroFrameWork/kernel/admin_themes/default", + label: "Активная тема админпанели" + ); + $this->out->r("create option active_admin_theme", "green"); + + $this->optionService->createFromParams( + key: "module_paths", + value: "{\"paths\": [\"{KERNEL_MODULES}\", \"{APP}/modules\"]}", + label: "Пути к модулям " + ); + $this->out->r("create option module_paths", "green"); + + $this->optionService->createFromParams( + key: "active_modules", + value: "{\"modules\":[\"admin_themes\"]}", + label: "Активные модули" + ); + $this->out->r("create option active_modules", "green"); + + $this->menuService->createItem([ + "label" => "Модули", + "url" => "/admin", + "slug" => "module", + ]); + $this->out->r("create item menu module", "green"); + + $this->menuService->createItem([ + "label" => "Настройки", + "url" => "/admin/settings", + "slug" => "settings", + ]); + $this->out->r("create item menu settings", "green"); + + $this->menuService->createItem([ + "label" => "Темы админпанели", + "url" => "/admin/settings/admin-themes", + "slug" => "admin-themes", + "parent_slug" => "settings" + ]); + $this->out->r("create item menu admin-themes", "green"); + } + +} \ No newline at end of file diff --git a/kernel/console/controllers/MigrationController.php b/kernel/console/controllers/MigrationController.php index e167fe1..02bb565 100644 --- a/kernel/console/controllers/MigrationController.php +++ b/kernel/console/controllers/MigrationController.php @@ -19,6 +19,7 @@ use kernel\services\ModuleService; class MigrationController extends ConsoleController { protected ModuleService $moduleService; + public function __construct() { parent::__construct(); @@ -35,8 +36,7 @@ class MigrationController extends ConsoleController $table->integer('batch'); }); $this->out->r("Success", 'green'); - } - catch (\Exception $e){ + } catch (\Exception $e) { $this->out->r($e->getMessage(), 'red'); } } @@ -60,7 +60,7 @@ class MigrationController extends ConsoleController ); $this->out->r(basename($res) . " created", 'green'); } catch (\Exception $e) { - $this->out->r('Message: ' .$e->getMessage(), 'red'); + $this->out->r('Message: ' . $e->getMessage(), 'red'); } } @@ -71,15 +71,18 @@ class MigrationController extends ConsoleController $dmr = new DatabaseMigrationRepository(App::$db->capsule->getDatabaseManager(), 'migration'); $m = new Migrator($dmr, App::$db->capsule->getDatabaseManager(), new Filesystem()); - $migrationPaths = array_merge($this->moduleService->getModulesMigrationsPaths(), [ROOT_DIR . '/migrations']); - //$migrationPaths = [ROOT_DIR . '/migrations']; + if (\kernel\App::$db->schema->hasTable('option')) { + $migrationPaths = array_merge($this->moduleService->getModulesMigrationsPaths(), [ROOT_DIR . '/migrations']); + } else { + $migrationPaths = [ROOT_DIR . '/migrations']; + } + $res = $m->run($migrationPaths); - foreach ($res as $re){ + foreach ($res as $re) { $this->out->r(basename($re), 'green'); } - } - catch (\Exception $e){ - $this->out->r('Message: ' .$e->getMessage(), 'red'); + } catch (\Exception $e) { + $this->out->r('Message: ' . $e->getMessage(), 'red'); } } @@ -94,12 +97,11 @@ class MigrationController extends ConsoleController $migrationPaths = [ROOT_DIR . '/migrations']; $res = $m->rollback($migrationPaths, ['step' => $step]); print_r($step); - foreach ($res as $re){ + foreach ($res as $re) { $this->out->r(basename($re), 'green'); } - } - catch (\Exception $e){ - $this->out->r('Message: ' .$e->getMessage(), 'red'); + } catch (\Exception $e) { + $this->out->r('Message: ' . $e->getMessage(), 'red'); } } } \ No newline at end of file diff --git a/kernel/console/routs/cli.php b/kernel/console/routs/cli.php index 244bf6c..4c6caa2 100644 --- a/kernel/console/routs/cli.php +++ b/kernel/console/routs/cli.php @@ -17,3 +17,7 @@ App::$collector->group(["prefix" => "admin-theme"], callback: function (RouteCol App::$collector->console('uninstall', [\kernel\console\controllers\AdminThemeController::class, 'actionUninstallTheme']); }); +App::$collector->group(["prefix" => "admin"], callback: function (RouteCollector $router){ + App::$collector->console('init', [\kernel\console\controllers\AdminConsoleController::class, 'actionInit']); +}); + diff --git a/kernel/modules/admin_themes/AdminThemesModule.php b/kernel/modules/admin_themes/AdminThemesModule.php index f95f56e..cfecb12 100644 --- a/kernel/modules/admin_themes/AdminThemesModule.php +++ b/kernel/modules/admin_themes/AdminThemesModule.php @@ -3,12 +3,32 @@ namespace kernel\modules\admin_themes; use kernel\Module; +use kernel\modules\menu\service\MenuService; class AdminThemesModule extends Module { - public function init() + public MenuService $menuService; + public function __construct() { - // TODO: Implement init() method. + $this->menuService = new MenuService(); + } + + /** + * @throws \Exception + */ + public function init(): void + { + $this->menuService->createItem([ + "label" => "Темы админпанели", + "url" => "/admin/settings/admin-themes", + "slug" => "admin-themes", + "parent_slug" => "settings" + ]); + } + + public function deactivate(): void + { + $this->menuService->removeItemBySlug("admin-themes"); } } \ No newline at end of file diff --git a/kernel/modules/admin_themes/manifest.json b/kernel/modules/admin_themes/manifest.json index 50341e3..a5b02ce 100644 --- a/kernel/modules/admin_themes/manifest.json +++ b/kernel/modules/admin_themes/manifest.json @@ -4,6 +4,7 @@ "author": "ITGuild", "slug": "admin_themes", "description": "Admin themes module", - "module_class": "AdminThemesModule", + "module_class": "kernel\\modules\\admin_themes\\AdminThemesModule", + "module_class_file": "{KERNEL_MODULES}/admin_themes/AdminThemesModule.php", "routs": "routs/adminThemes.php" } \ No newline at end of file diff --git a/kernel/modules/menu/MenuModule.php b/kernel/modules/menu/MenuModule.php index 0f138e1..86546ef 100644 --- a/kernel/modules/menu/MenuModule.php +++ b/kernel/modules/menu/MenuModule.php @@ -3,12 +3,32 @@ namespace kernel\modules\menu; use kernel\Module; +use kernel\modules\menu\service\MenuService; class MenuModule extends Module { - public function init() + public MenuService $menuService; + public function __construct() { - // TODO: Implement init() method. + $this->menuService = new MenuService(); + } + + /** + * @throws \Exception + */ + public function init(): void + { + $this->menuService->createItem([ + "label" => "Меню", + "url" => "/admin/settings/menu", + "slug" => "menu", + "parent_slug" => "settings" + ]); + } + + public function deactivate(): void + { + $this->menuService->removeItemBySlug("menu"); } } \ No newline at end of file diff --git a/kernel/modules/menu/manifest.json b/kernel/modules/menu/manifest.json index b8777ee..98872c4 100644 --- a/kernel/modules/menu/manifest.json +++ b/kernel/modules/menu/manifest.json @@ -4,6 +4,7 @@ "author": "ITGuild", "slug": "menu", "description": "Menu module", - "module_class": "MenuModule", + "module_class": "kernel\\modules\\menu\\MenuModule", + "module_class_file": "{KERNEL_MODULES}/menu/MenuModule.php", "routs": "routs/menu.php" } \ No newline at end of file diff --git a/kernel/modules/menu/migrations/2024_09_23_125545_create_menu_table.php b/kernel/modules/menu/migrations/2024_09_23_125545_create_menu_table.php new file mode 100644 index 0000000..272ae80 --- /dev/null +++ b/kernel/modules/menu/migrations/2024_09_23_125545_create_menu_table.php @@ -0,0 +1,34 @@ +schema->create('menu', function (Blueprint $table) { + $table->increments('id'); + $table->integer('parent_id')->default(0); + $table->string('icon_file', 255)->nullable(); + $table->string('icon_font', 255)->nullable(); + $table->string('label', 255); + $table->string('url', 255); + $table->string('slug', 255)->unique(); + $table->integer('status')->default(1); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + \kernel\App::$db->schema->dropIfExists('menu'); + } +}; diff --git a/kernel/modules/option/controllers/OptionController.php b/kernel/modules/option/controllers/OptionController.php index adff235..db84e99 100644 --- a/kernel/modules/option/controllers/OptionController.php +++ b/kernel/modules/option/controllers/OptionController.php @@ -76,21 +76,20 @@ class OptionController extends AdminController */ public function actionEdit(int $id): void { - Debug::prn($_REQUEST); $option = Option::find($id); if (!$option) { throw new \Exception('Option not found'); } $optionForm = new CreateOptionForm(); - $optionService = new OptionService(); $optionForm->load($_REQUEST); if ($optionForm->validate()) { - $option = $optionService->update($optionForm, $option); + $option = $this->optionService->update($optionForm, $option); if ($option) { - $this->redirect('/admin/option' . $option->id); + $this->redirect('/admin/option/' . $option->id); } } - $this->redirect('/admin/option/update' . $id); + + $this->redirect('/admin/option/update/' . $id); } #[NoReturn] public function actionDelete(int $id): void diff --git a/kernel/modules/option/migrations/2024_09_23_125716_create_option_table.php b/kernel/modules/option/migrations/2024_09_23_125716_create_option_table.php new file mode 100644 index 0000000..d2468a7 --- /dev/null +++ b/kernel/modules/option/migrations/2024_09_23_125716_create_option_table.php @@ -0,0 +1,31 @@ +schema->create('option', function (Blueprint $table) { + $table->increments('id'); + $table->string('key', 255); + $table->text('value')->nullable(); + $table->string('label', 255)->nullable(); + $table->integer('status')->default(1); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + \kernel\App::$db->schema->dropIfExists('option'); + } +}; diff --git a/kernel/modules/option/models/Option.php b/kernel/modules/option/models/Option.php index 26d335c..49eda92 100644 --- a/kernel/modules/option/models/Option.php +++ b/kernel/modules/option/models/Option.php @@ -14,6 +14,8 @@ use Illuminate\Database\Eloquent\Model; class Option extends Model { + const DISABLE_STATUS = 0; + const ACTIVE_STATUS = 1; protected $table = 'option'; protected $fillable = ['key', 'value', 'label', 'status']; @@ -29,4 +31,15 @@ class Option extends Model ]; } + /** + * @return string[] + */ + public static function getStatus(): array + { + return [ + self::DISABLE_STATUS => "Не активный", + self::ACTIVE_STATUS => "Активный", + ]; + } + } \ No newline at end of file diff --git a/kernel/modules/option/models/forms/CreateOptionForm.php b/kernel/modules/option/models/forms/CreateOptionForm.php index 921555e..28c9dbf 100644 --- a/kernel/modules/option/models/forms/CreateOptionForm.php +++ b/kernel/modules/option/models/forms/CreateOptionForm.php @@ -4,16 +4,22 @@ namespace kernel\modules\option\models\forms; use kernel\FormModel; +/** + * @property string $key + * @property string $value + * @property string $label + * @property integer $status + */ class CreateOptionForm extends FormModel { public function rules(): array { return [ - 'admin_theme_paths' => '', - 'active_admin_theme' => '', - 'module_paths' => '', - 'active_modules' => '' + 'key' => 'required|min-str-len:1|max-str-len:50', + 'value' => '', + 'label' => '', + 'status' => '' ]; } diff --git a/kernel/modules/option/routs/option.php b/kernel/modules/option/routs/option.php index d0c3f55..259d2b0 100644 --- a/kernel/modules/option/routs/option.php +++ b/kernel/modules/option/routs/option.php @@ -3,8 +3,8 @@ use kernel\App; use Phroute\Phroute\RouteCollector; -App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){ - App::$collector->group(["prefix" => "option"], callback: function (RouteCollector $router){ +App::$collector->group(["prefix" => "admin"], function (RouteCollector $router) { + App::$collector->group(["prefix" => "option"], callback: function (RouteCollector $router) { App::$collector->get('/', [\kernel\modules\option\controllers\OptionController::class, 'actionIndex']); App::$collector->get('/page/{page_number}', [\kernel\modules\option\controllers\OptionController::class, 'actionIndex']); App::$collector->get('/create', [\kernel\modules\option\controllers\OptionController::class, 'actionCreate']); @@ -13,5 +13,5 @@ App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){ App::$collector->any('/update/{id}', [\kernel\modules\option\controllers\OptionController::class, 'actionUpdate']); App::$collector->any("/edit/{id}", [\kernel\modules\option\controllers\OptionController::class, 'actionEdit']); App::$collector->get('/delete/{id}', [\kernel\modules\option\controllers\OptionController::class, 'actionDelete']); - }); + }); }); \ No newline at end of file diff --git a/kernel/modules/option/service/OptionService.php b/kernel/modules/option/service/OptionService.php index 2386440..ba674d9 100644 --- a/kernel/modules/option/service/OptionService.php +++ b/kernel/modules/option/service/OptionService.php @@ -35,6 +35,20 @@ class OptionService return false; } + public function createFromParams(string $key, string $value, string $label): false|Option + { + $model = new Option(); + $model->key = $key; + $model->value = $value; + $model->label = $label; + + if ($model->save()) { + return $model; + } + + return false; + } + // public function createOptionArr(): array // { // foreach (Option::all()->toArray() as $option) { diff --git a/kernel/modules/option/views/form.php b/kernel/modules/option/views/form.php index e7fde62..3dea601 100644 --- a/kernel/modules/option/views/form.php +++ b/kernel/modules/option/views/form.php @@ -38,7 +38,7 @@ $form->field(\itguild\forms\inputs\Select::class, 'status', [ 'value' => $model->status ?? '' ]) ->setLabel("Статус") - ->setOptions(['1', '2']) + ->setOptions(Option::getStatus()) ->render(); ?> diff --git a/kernel/modules/post/migrations/2024_09_23_130017_create_post_table.php b/kernel/modules/post/migrations/2024_09_23_130017_create_post_table.php new file mode 100644 index 0000000..6556a6a --- /dev/null +++ b/kernel/modules/post/migrations/2024_09_23_130017_create_post_table.php @@ -0,0 +1,31 @@ +schema->create('post', function (Blueprint $table) { + $table->increments('id'); + $table->string('title', 255)->nullable(false); + $table->string('slug', 255)->unique(); + $table->text('content')->nullable(false); + $table->integer('user_id'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + \kernel\App::$db->schema->dropIfExists('post'); + } +}; diff --git a/kernel/modules/user/migrations/2024_09_23_125827_create_user_table.php b/kernel/modules/user/migrations/2024_09_23_125827_create_user_table.php new file mode 100644 index 0000000..8f53302 --- /dev/null +++ b/kernel/modules/user/migrations/2024_09_23_125827_create_user_table.php @@ -0,0 +1,35 @@ +schema->create('user', function (Blueprint $table) { + $table->increments('id'); + $table->string('username', 255)->nullable(false); + $table->string('email', 255); + $table->string('password_hash', 255); + $table->integer('role')->default(1); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public static function down(): void + { + \kernel\App::$db->schema->dropIfExists('user'); + } +}; diff --git a/kernel/routs/admin.php b/kernel/routs/admin.php index 54b0fcb..cad4eba 100644 --- a/kernel/routs/admin.php +++ b/kernel/routs/admin.php @@ -7,8 +7,10 @@ use Phroute\Phroute\RouteCollector; App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){ + App::$collector->get('/', [\kernel\controllers\ModuleController::class, 'actionIndex']); App::$collector->group(["prefix" => "module"], function (RouteCollector $router){ App::$collector->get('/', [\kernel\controllers\ModuleController::class, 'actionIndex']); App::$collector->get('/activate', [\kernel\controllers\ModuleController::class, 'actionActivate']); }); + }); \ No newline at end of file diff --git a/kernel/services/MigrationService.php b/kernel/services/MigrationService.php new file mode 100644 index 0000000..9085637 --- /dev/null +++ b/kernel/services/MigrationService.php @@ -0,0 +1,35 @@ +moduleService = new ModuleService(); + } + + /** + * @throws \Exception + */ + public function runAtPath(string $path = ROOT_DIR . '/migrations'): array + { + try { + $dmr = new DatabaseMigrationRepository(App::$db->capsule->getDatabaseManager(), 'migration'); + + $m = new Migrator($dmr, App::$db->capsule->getDatabaseManager(), new Filesystem()); + + return $m->run($path); + } catch (\Exception $e) { + throw new \Exception('Не удалось поднять играции'); + } + } + +} \ No newline at end of file diff --git a/resources/default/css/style.css b/resources/default/css/style.css index f30a188..b27c186 100644 --- a/resources/default/css/style.css +++ b/resources/default/css/style.css @@ -8278,8 +8278,8 @@ a[data-toggle="collapse"] { transition: all 0.3s; } .btn.btn-primary { - background: #f8b739; - border-color: #f8b739; } + background: #465df6; + border-color: #111111; } .btn.btn-primary:hover, .btn.btn-primary:focus { background: #f8b739 !important; border-color: #f8b739 !important; }