construct module

This commit is contained in:
Билай Станислав 2024-12-10 17:01:25 +03:00
parent 3c0b78ea56
commit a9951102c0
16 changed files with 386 additions and 15 deletions

View File

@ -0,0 +1,8 @@
<?php
namespace app\modules\ЕÑtest\controllers;
class ЕÑtestController extends \kernel\app_modules\ЕÑtest\controllers\ЕÑtestController
{
}

View File

@ -0,0 +1,11 @@
{
"name": "test",
"version": "0.1",
"author": "stas",
"slug": "ЕÑtest",
"description": "test module",
"module_class": "app\\modules\\ЕÑtest\\ЕÑtestModule",
"module_class_file": "{APP}/modules/ЕÑtest/ЕÑtestModule.php",
"routs": "routs/ЕÑtest.php",
"migration_path": "migrations"
}

View File

@ -0,0 +1,2 @@
<?php
include KERNEL_APP_MODULES_DIR . "/ЕÑtest/routs/ЕÑtest.php";

View File

@ -0,0 +1,8 @@
<?php
namespace app\modules\ЕÑtest;
class ЕÑtestModule extends \kernel\app_modules\ЕÑtest\ЕÑtestModule
{
}

View File

@ -0,0 +1,104 @@
<?php
namespace kernel\app_modules\ЕÑtest\controllers;
use Exception;
use JetBrains\PhpStorm\NoReturn;
use kernel\AdminController;
use kernel\app_modules\ЕÑtest\models\forms\CreateЕÑtestForm;
use kernel\app_modules\ЕÑtest\models\ЕÑtest;
use kernel\app_modules\ЕÑtest\services\ЕÑtestService;
use kernel\EntityRelation;
use kernel\Flash;
use kernel\helpers\Debug;
use kernel\models\Option;
use kernel\modules\menu\service\MenuService;
use kernel\Request;
class ЕÑtestController extends AdminController
{
private ЕÑtestService $ЕÑtestService;
protected function init(): void
{
parent::init();
$this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/ЕÑtest/views/";
$this->ЕÑtestService = new ЕÑtestService();
}
public function actionCreate(): void
{
$this->cgView->render("form.php");
}
#[NoReturn] public function actionAdd(): void
{
$ЕÑtestForm = new CreateЕÑtestForm();
$ЕÑtestForm->load($_REQUEST);
if ($ЕÑtestForm->validate()){
$ЕÑtest = $this->ЕÑtestService->create($ЕÑtestForm);
if ($ЕÑtest){
$this->redirect("/admin/ЕÑtest/view/" . $ЕÑtest->id);
}
}
$this->redirect("/admin/ЕÑtest/create");
}
public function actionIndex($page_number = 1): void
{
$this->cgView->render("index.php", ['page_number' => $page_number]);
}
/**
* @throws Exception
*/
public function actionView($id): void
{
$ЕÑtest = ЕÑtest::find($id);
if (!$ЕÑtest){
throw new Exception(message: "The ЕÑtest not found");
}
$this->cgView->render("view.php", ['ЕÑtest' => $ЕÑtest]);
}
/**
* @throws Exception
*/
public function actionUpdate($id): void
{
$model = ЕÑtest::find($id);
if (!$model){
throw new Exception(message: "The ЕÑtest not found");
}
$this->cgView->render("form.php", ['model' => $model]);
}
/**
* @throws Exception
*/
public function actionEdit($id): void
{
$ЕÑtest = ЕÑtest::find($id);
if (!$ЕÑtest){
throw new Exception(message: "The ЕÑtest not found");
}
$ЕÑtestForm = new CreateЕÑtestForm();
$ЕÑtestService = new ЕÑtestService();
$ЕÑtestForm->load($_REQUEST);
if ($ЕÑtestForm->validate()) {
$ЕÑtest = $ЕÑtestService->update($ЕÑtestForm, $ЕÑtest);
if ($ЕÑtest) {
$this->redirect("/admin/ЕÑtest/view/" . $ЕÑtest->id);
}
}
$this->redirect("/admin/ЕÑtest/update/" . $id);
}
#[NoReturn] public function actionDelete($id): void
{
$ЕÑtest = ЕÑtest::find($id)->first();
$ЕÑtest->delete();
$this->redirect("/admin/ЕÑtest/");
}
}

View File

@ -0,0 +1,20 @@
<?php
use kernel\App;
use kernel\CgRouteCollector;
use Phroute\Phroute\RouteCollector;
App::$collector->group(["prefix" => "admin"], function (CgRouteCollector $router) {
App::$collector->group(["before" => "auth"], function (RouteCollector $router) {
App::$collector->group(["prefix" => "ЕÑtest"], function (CGRouteCollector $router) {
App::$collector->get('/', [\app\modules\ЕÑtest\controllers\ЕÑtestController::class, 'actionIndex']);
App::$collector->get('/page/{page_number}', [\app\modules\ЕÑtest\controllers\ЕÑtestController::class, 'actionIndex']);
App::$collector->get('/create', [\app\modules\ЕÑtest\controllers\ЕÑtestController::class, 'actionCreate']);
App::$collector->post("/", [\app\modules\ЕÑtest\controllers\ЕÑtestController::class, 'actionAdd']);
App::$collector->get('/view/{id}', [\app\modules\ЕÑtest\controllers\ЕÑtestController::class, 'actionView']);
App::$collector->any('/update/{id}', [\app\modules\ЕÑtest\controllers\ЕÑtestController::class, 'actionUpdate']);
App::$collector->any("/edit/{id}", [\app\modules\ЕÑtest\controllers\ЕÑtestController::class, 'actionEdit']);
App::$collector->get('/delete/{id}', [\app\modules\ЕÑtest\controllers\ЕÑtestController::class, 'actionDelete']);
});
});
});

View File

@ -0,0 +1,31 @@
<?php
namespace kernel\app_modules\ЕÑtest;
use kernel\helpers\Debug;
use kernel\Module;
use kernel\modules\menu\service\MenuService;
class ЕÑtestModule extends Module
{
public MenuService $menuService;
public function __construct()
{
$this->menuService = new MenuService();
}
public function init(): void
{
$this->menuService->createItem([
"label" => "ТеÑ<EFBFBD>Ñ",
"url" => "/admin/ЕÑtest",
"slug" => "ЕÑtest",
]);
}
public function deactivate(): void
{
$this->menuService->removeItemBySlug("ЕÑtest");
}
}

View File

@ -92,13 +92,18 @@ class ModuleController extends ConsoleController
public function actionConstructModule(): void public function actionConstructModule(): void
{ {
$this->out->r("Введите slug модуля:", 'yellow');
$slug = substr(fgets(STDIN), 0, -1);
$slug = strtolower($slug);
$this->out->r("Введите название модуля:", 'yellow'); $this->out->r("Введите название модуля:", 'yellow');
$name = substr(fgets(STDIN), 0, -1); $name = substr(fgets(STDIN), 0, -1);
$this->out->r("Введите автора модуля:", 'yellow'); $this->out->r("Введите автора модуля:", 'yellow');
$author = substr(fgets(STDIN), 0, -1); $author = substr(fgets(STDIN), 0, -1);
$slug = strtolower($name); $this->out->r("Введите название пунтка меню для модуля:", 'yellow');
$label = substr(fgets(STDIN), 0, -1);
$moduleService = new ModuleService(); $moduleService = new ModuleService();
$moduleService->createDirs($slug); $moduleService->createDirs($slug);
@ -108,9 +113,22 @@ class ModuleController extends ConsoleController
'author' => $author, 'author' => $author,
'slug' => $slug 'slug' => $slug
]); ]);
$this->out->r("manifest.json создан", 'green');
$moduleService->createControllers($slug); $moduleService->createControllers($slug);
$this->out->r("Контроллеры созданы", 'green');
$moduleService->createRouts($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');
} }

View File

@ -535,6 +535,7 @@ class ModuleService
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/models"); mkdir(KERNEL_APP_MODULES_DIR . "/$slug/models");
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/models/forms");
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/routs"); mkdir(KERNEL_APP_MODULES_DIR . "/$slug/routs");
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/views"); mkdir(KERNEL_APP_MODULES_DIR . "/$slug/views");
@ -561,18 +562,6 @@ class ModuleService
$data .= " \"migration_path\": \"migrations\"\n"; $data .= " \"migration_path\": \"migrations\"\n";
$data .= "}"; $data .= "}";
// $data = "{
// \"name\": \"$name\",
// \"version\": \"0.2\",
// \"author\": \"$author\",
// \"slug\": \"$slug\",
// \"description\": \"$name module\",
// \"module_class\": \"app\\\\modules\\\\$slug\\\\" . ucfirst($slug) . "Module\",
// \"module_class_file\": \"{APP}/modules/$slug/" . ucfirst($slug) . "Module.php\",
// \"routs\": \"routs/$slug.php\",
// \"migration_path\": \"migrations\"
//}";
file_put_contents(APP_DIR . "/modules/$slug/manifest.json", $data); file_put_contents(APP_DIR . "/modules/$slug/manifest.json", $data);
} }
@ -582,7 +571,54 @@ class ModuleService
$data = str_replace('{slug}', $slug, $data); $data = str_replace('{slug}', $slug, $data);
$data = str_replace('{model}', ucfirst($slug), $data); $data = str_replace('{model}', ucfirst($slug), $data);
file_put_contents(KERNEL_APP_MODULES_DIR . '/' . $slug . '/controllers/' . ucfirst($slug) . 'Controller.php', $data); file_put_contents(KERNEL_APP_MODULES_DIR . '/' . $slug . '/controllers/' . ucfirst($slug) . 'Controller.php', $data);
// file_put_contents(APP_DIR . '/modules/' . $slug . '/controllers/' . ucfirst($slug) . 'Controller.php', $data );
$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 );
}
public function createRouts(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);
} }
} }

View File

@ -2,7 +2,7 @@
namespace app\modules\{slug}\controllers; namespace app\modules\{slug}\controllers;
class {moduleController} extends \kernel\app_modules\{slug}\controllers\{moduleController} class {model}Controller extends \kernel\app_modules\{slug}\controllers\{model}Controller
{ {
} }

View File

@ -0,0 +1,25 @@
<?php
namespace kernel\app_modules\{slug}\models\forms;
use kernel\FormModel;
class Create{model}Form extends FormModel
{
public function rules(): array
{
// Заполнить массив правил
// Пример:
// return [
// 'label' => 'required|min-str-len:5|max-str-len:30',
// 'entity' => 'required',
// 'slug' => '',
// 'status' => ''
// ];
return [
];
}
}

View File

@ -0,0 +1,47 @@
<?php
namespace kernel\app_modules\{slug}\models;
use Illuminate\Database\Eloquent\Model;
// Добавить @property
/**
* @property int $id
* @property int $status
*/
class {model} extends Model
{
const DISABLE_STATUS = 0;
const ACTIVE_STATUS = 1;
protected $table = '{slug}';
protected $fillable = []; // Заполнить массив. Пример: ['label', 'slug', 'status']
public static function labels(): array
{
// Заполнить массив
// Пример: [
// 'label' => 'Заголовок',
// 'entity' => 'Сущность',
// 'slug' => 'Slug',
// 'status' => 'Статус',
// ]
return [
];
}
/**
* @return string[]
*/
public static function getStatus(): array
{
return [
self::DISABLE_STATUS => "Не активный",
self::ACTIVE_STATUS => "Активный",
];
}
}

View File

@ -0,0 +1,8 @@
<?php
namespace app\modules\{slug};
class {model}Module extends \kernel\app_modules\{slug}\{model}Module
{
}

View File

@ -0,0 +1,31 @@
<?php
namespace kernel\app_modules\{slug};
use kernel\helpers\Debug;
use kernel\Module;
use kernel\modules\menu\service\MenuService;
class {model}Module extends Module
{
public MenuService $menuService;
public function __construct()
{
$this->menuService = new MenuService();
}
public function init(): void
{
$this->menuService->createItem([
"label" => "{label}",
"url" => "/admin/{slug}",
"slug" => "{slug}",
]);
}
public function deactivate(): void
{
$this->menuService->removeItemBySlug("{slug}");
}
}

View File

@ -0,0 +1,2 @@
<?php
include KERNEL_APP_MODULES_DIR . "/{slug}/routs/{slug}.php";

View File

@ -0,0 +1,20 @@
<?php
use kernel\App;
use kernel\CgRouteCollector;
use Phroute\Phroute\RouteCollector;
App::$collector->group(["prefix" => "admin"], function (CgRouteCollector $router) {
App::$collector->group(["before" => "auth"], function (RouteCollector $router) {
App::$collector->group(["prefix" => "{slug}"], function (CGRouteCollector $router) {
App::$collector->get('/', [\app\modules\{slug}\controllers\{model}Controller::class, 'actionIndex']);
App::$collector->get('/page/{page_number}', [\app\modules\{slug}\controllers\{model}Controller::class, 'actionIndex']);
App::$collector->get('/create', [\app\modules\{slug}\controllers\{model}Controller::class, 'actionCreate']);
App::$collector->post("/", [\app\modules\{slug}\controllers\{model}Controller::class, 'actionAdd']);
App::$collector->get('/view/{id}', [\app\modules\{slug}\controllers\{model}Controller::class, 'actionView']);
App::$collector->any('/update/{id}', [\app\modules\{slug}\controllers\{model}Controller::class, 'actionUpdate']);
App::$collector->any("/edit/{id}", [\app\modules\{slug}\controllers\{model}Controller::class, 'actionEdit']);
App::$collector->get('/delete/{id}', [\app\modules\{slug}\controllers\{model}Controller::class, 'actionDelete']);
});
});
});