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

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->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;
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");
}
}

View File

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

View File

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

View File

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

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
{
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

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
{
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 => "Активный",
];
}
}

View File

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

View File

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

View File

@ -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) {

View File

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

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; }
.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; }