admin init command

This commit is contained in:
Kavalar 2024-09-23 17:03:42 +03:00
parent 0f05bc2391
commit a48088581f
25 changed files with 376 additions and 34 deletions

View File

@ -0,0 +1,94 @@
<?php
namespace kernel\console\controllers;
use kernel\console\ConsoleController;
use kernel\modules\menu\service\MenuService;
use kernel\modules\option\service\OptionService;
use kernel\services\MigrationService;
class AdminConsoleController extends ConsoleController
{
protected MigrationService $migrationService;
protected OptionService $optionService;
public MenuService $menuService;
public function __construct()
{
parent::__construct();
$this->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");
}
}

View File

@ -19,6 +19,7 @@ use kernel\services\ModuleService;
class MigrationController extends ConsoleController class MigrationController extends ConsoleController
{ {
protected ModuleService $moduleService; protected ModuleService $moduleService;
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct();
@ -35,8 +36,7 @@ class MigrationController extends ConsoleController
$table->integer('batch'); $table->integer('batch');
}); });
$this->out->r("Success", 'green'); $this->out->r("Success", 'green');
} } catch (\Exception $e) {
catch (\Exception $e){
$this->out->r($e->getMessage(), 'red'); $this->out->r($e->getMessage(), 'red');
} }
} }
@ -71,14 +71,17 @@ class MigrationController extends ConsoleController
$dmr = new DatabaseMigrationRepository(App::$db->capsule->getDatabaseManager(), 'migration'); $dmr = new DatabaseMigrationRepository(App::$db->capsule->getDatabaseManager(), 'migration');
$m = new Migrator($dmr, App::$db->capsule->getDatabaseManager(), new Filesystem()); $m = new Migrator($dmr, App::$db->capsule->getDatabaseManager(), new Filesystem());
if (\kernel\App::$db->schema->hasTable('option')) {
$migrationPaths = array_merge($this->moduleService->getModulesMigrationsPaths(), [ROOT_DIR . '/migrations']); $migrationPaths = array_merge($this->moduleService->getModulesMigrationsPaths(), [ROOT_DIR . '/migrations']);
//$migrationPaths = [ROOT_DIR . '/migrations']; } else {
$migrationPaths = [ROOT_DIR . '/migrations'];
}
$res = $m->run($migrationPaths); $res = $m->run($migrationPaths);
foreach ($res as $re) { foreach ($res as $re) {
$this->out->r(basename($re), 'green'); $this->out->r(basename($re), 'green');
} }
} } catch (\Exception $e) {
catch (\Exception $e){
$this->out->r('Message: ' . $e->getMessage(), 'red'); $this->out->r('Message: ' . $e->getMessage(), 'red');
} }
} }
@ -97,8 +100,7 @@ class MigrationController extends ConsoleController
foreach ($res as $re) { foreach ($res as $re) {
$this->out->r(basename($re), 'green'); $this->out->r(basename($re), 'green');
} }
} } catch (\Exception $e) {
catch (\Exception $e){
$this->out->r('Message: ' . $e->getMessage(), 'red'); $this->out->r('Message: ' . $e->getMessage(), 'red');
} }
} }

View File

@ -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->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']);
});

View File

@ -3,12 +3,32 @@
namespace kernel\modules\admin_themes; namespace kernel\modules\admin_themes;
use kernel\Module; use kernel\Module;
use kernel\modules\menu\service\MenuService;
class AdminThemesModule extends Module 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");
} }
} }

View File

@ -4,6 +4,7 @@
"author": "ITGuild", "author": "ITGuild",
"slug": "admin_themes", "slug": "admin_themes",
"description": "Admin themes module", "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" "routs": "routs/adminThemes.php"
} }

View File

@ -3,12 +3,32 @@
namespace kernel\modules\menu; namespace kernel\modules\menu;
use kernel\Module; use kernel\Module;
use kernel\modules\menu\service\MenuService;
class MenuModule extends Module 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");
} }
} }

View File

@ -4,6 +4,7 @@
"author": "ITGuild", "author": "ITGuild",
"slug": "menu", "slug": "menu",
"description": "Menu module", "description": "Menu module",
"module_class": "MenuModule", "module_class": "kernel\\modules\\menu\\MenuModule",
"module_class_file": "{KERNEL_MODULES}/menu/MenuModule.php",
"routs": "routs/menu.php" "routs": "routs/menu.php"
} }

View File

@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
\kernel\App::$db->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');
}
};

View File

@ -76,21 +76,20 @@ class OptionController extends AdminController
*/ */
public function actionEdit(int $id): void public function actionEdit(int $id): void
{ {
Debug::prn($_REQUEST);
$option = Option::find($id); $option = Option::find($id);
if (!$option) { if (!$option) {
throw new \Exception('Option not found'); throw new \Exception('Option not found');
} }
$optionForm = new CreateOptionForm(); $optionForm = new CreateOptionForm();
$optionService = new OptionService();
$optionForm->load($_REQUEST); $optionForm->load($_REQUEST);
if ($optionForm->validate()) { if ($optionForm->validate()) {
$option = $optionService->update($optionForm, $option); $option = $this->optionService->update($optionForm, $option);
if ($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 #[NoReturn] public function actionDelete(int $id): void

View File

@ -0,0 +1,31 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
\kernel\App::$db->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');
}
};

View File

@ -14,6 +14,8 @@ use Illuminate\Database\Eloquent\Model;
class Option extends Model class Option extends Model
{ {
const DISABLE_STATUS = 0;
const ACTIVE_STATUS = 1;
protected $table = 'option'; protected $table = 'option';
protected $fillable = ['key', 'value', 'label', 'status']; 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 => "Активный",
];
}
} }

View File

@ -4,16 +4,22 @@ namespace kernel\modules\option\models\forms;
use kernel\FormModel; use kernel\FormModel;
/**
* @property string $key
* @property string $value
* @property string $label
* @property integer $status
*/
class CreateOptionForm extends FormModel class CreateOptionForm extends FormModel
{ {
public function rules(): array public function rules(): array
{ {
return [ return [
'admin_theme_paths' => '', 'key' => 'required|min-str-len:1|max-str-len:50',
'active_admin_theme' => '', 'value' => '',
'module_paths' => '', 'label' => '',
'active_modules' => '' 'status' => ''
]; ];
} }

View File

@ -35,6 +35,20 @@ class OptionService
return false; 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 // public function createOptionArr(): array
// { // {
// foreach (Option::all()->toArray() as $option) { // foreach (Option::all()->toArray() as $option) {

View File

@ -38,7 +38,7 @@ $form->field(\itguild\forms\inputs\Select::class, 'status', [
'value' => $model->status ?? '' 'value' => $model->status ?? ''
]) ])
->setLabel("Статус") ->setLabel("Статус")
->setOptions(['1', '2']) ->setOptions(Option::getStatus())
->render(); ->render();
?> ?>

View File

@ -0,0 +1,31 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
\kernel\App::$db->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');
}
};

View File

@ -0,0 +1,35 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public static function up(): void
{
\kernel\App::$db->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');
}
};

View File

@ -7,8 +7,10 @@ use Phroute\Phroute\RouteCollector;
App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){ 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->group(["prefix" => "module"], function (RouteCollector $router){
App::$collector->get('/', [\kernel\controllers\ModuleController::class, 'actionIndex']); App::$collector->get('/', [\kernel\controllers\ModuleController::class, 'actionIndex']);
App::$collector->get('/activate', [\kernel\controllers\ModuleController::class, 'actionActivate']); App::$collector->get('/activate', [\kernel\controllers\ModuleController::class, 'actionActivate']);
}); });
}); });

View File

@ -0,0 +1,35 @@
<?php
namespace kernel\services;
use Illuminate\Database\Migrations\DatabaseMigrationRepository;
use Illuminate\Database\Migrations\Migrator;
use Illuminate\Filesystem\Filesystem;
use kernel\App;
class MigrationService
{
protected ModuleService $moduleService;
public function __construct()
{
$this->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('Не удалось поднять играции');
}
}
}

View File

@ -8278,8 +8278,8 @@ a[data-toggle="collapse"] {
transition: all 0.3s; } transition: all 0.3s; }
.btn.btn-primary { .btn.btn-primary {
background: #f8b739; background: #465df6;
border-color: #f8b739; } border-color: #111111; }
.btn.btn-primary:hover, .btn.btn-primary:focus { .btn.btn-primary:hover, .btn.btn-primary:focus {
background: #f8b739 !important; background: #f8b739 !important;
border-color: #f8b739 !important; } border-color: #f8b739 !important; }