templates controllers and module create in terminal
This commit is contained in:
parent
fb39da53a6
commit
3c0b78ea56
@ -90,4 +90,28 @@ class ModuleController extends ConsoleController
|
||||
}
|
||||
}
|
||||
|
||||
public function actionConstructModule(): void
|
||||
{
|
||||
$this->out->r("Введите название модуля:", 'yellow');
|
||||
$name = substr(fgets(STDIN), 0, -1);
|
||||
|
||||
$this->out->r("Введите автора модуля:", 'yellow');
|
||||
$author = substr(fgets(STDIN), 0, -1);
|
||||
|
||||
$slug = strtolower($name);
|
||||
|
||||
$moduleService = new ModuleService();
|
||||
$moduleService->createDirs($slug);
|
||||
|
||||
$moduleService->createManifest([
|
||||
'name' => $name,
|
||||
'author' => $author,
|
||||
'slug' => $slug
|
||||
]);
|
||||
|
||||
$moduleService->createControllers($slug);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -30,6 +30,7 @@ App::$collector->group(["prefix" => "module"], callback: function (RouteCollecto
|
||||
App::$collector->console('uninstall', [\kernel\console\controllers\ModuleController::class, 'actionUninstallModule']);
|
||||
App::$collector->console('pack', [\kernel\console\controllers\ModuleController::class, 'actionPackModule']);
|
||||
App::$collector->console('update', [\kernel\console\controllers\ModuleController::class, 'actionUpdateModule']);
|
||||
App::$collector->console('construct', [\kernel\console\controllers\ModuleController::class, 'actionConstructModule']);
|
||||
});
|
||||
|
||||
App::$collector->group(["prefix" => "kernel"], callback: function (RouteCollector $router){
|
||||
|
@ -529,4 +529,60 @@ class ModuleService
|
||||
return false;
|
||||
}
|
||||
|
||||
public function createDirs(string $slug): void
|
||||
{
|
||||
mkdir(KERNEL_APP_MODULES_DIR . "/$slug");
|
||||
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/controllers");
|
||||
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/migrations");
|
||||
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/models");
|
||||
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/routs");
|
||||
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/views");
|
||||
|
||||
mkdir(APP_DIR . "/modules/$slug");
|
||||
mkdir(APP_DIR . "/modules/$slug/controllers");
|
||||
mkdir(APP_DIR . "/modules/$slug/routs");
|
||||
}
|
||||
|
||||
public function createManifest(array $params): void
|
||||
{
|
||||
$name = $params['name'] ?? '';
|
||||
$author = $params['author'] ?? '';
|
||||
$slug = $params['slug'] ?? '';
|
||||
|
||||
$data = "{\n";
|
||||
$data .= " \"name\": \"$name\",\n";
|
||||
$data .= " \"version\": \"0.1\",\n";
|
||||
$data .= " \"author\": \"$author\",\n";
|
||||
$data .= " \"slug\": \"$slug\",\n";
|
||||
$data .= " \"description\": \"$name module\",\n";
|
||||
$data .= " \"module_class\": \"app\\\\modules\\\\$slug\\\\" . ucfirst($slug) . "Module\",\n";
|
||||
$data .= " \"module_class_file\": \"{APP}/modules/$slug/" . ucfirst($slug) . "Module.php\",\n";
|
||||
$data .= " \"routs\": \"routs/$slug.php\",\n";
|
||||
$data .= " \"migration_path\": \"migrations\"\n";
|
||||
$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);
|
||||
}
|
||||
|
||||
public function createControllers(string $slug): void
|
||||
{
|
||||
$data = file_get_contents(KERNEL_DIR . '/templates/controllers/kernel_controller_template');
|
||||
$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);
|
||||
// file_put_contents(APP_DIR . '/modules/' . $slug . '/controllers/' . ucfirst($slug) . 'Controller.php', $data );
|
||||
}
|
||||
|
||||
}
|
8
kernel/templates/controllers/app_controller_template
Normal file
8
kernel/templates/controllers/app_controller_template
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace app\modules\{slug}\controllers;
|
||||
|
||||
class {moduleController} extends \kernel\app_modules\{slug}\controllers\{moduleController}
|
||||
{
|
||||
|
||||
}
|
104
kernel/templates/controllers/kernel_controller_template
Normal file
104
kernel/templates/controllers/kernel_controller_template
Normal file
@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\{slug}\controllers;
|
||||
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
use kernel\AdminController;
|
||||
use kernel\app_modules\{slug}\models\forms\Create{model}Form;
|
||||
use kernel\app_modules\{slug}\models\{model};
|
||||
use kernel\app_modules\{slug}\services\{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
|
||||
{
|
||||
private {model}Service ${slug}Service;
|
||||
protected function init(): void
|
||||
{
|
||||
parent::init();
|
||||
$this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/{slug}/views/";
|
||||
$this->{slug}Service = new {model}Service();
|
||||
}
|
||||
|
||||
public function actionCreate(): void
|
||||
{
|
||||
$this->cgView->render("form.php");
|
||||
}
|
||||
|
||||
#[NoReturn] public function actionAdd(): void
|
||||
{
|
||||
${slug}Form = new Create{model}Form();
|
||||
${slug}Form->load($_REQUEST);
|
||||
if (${slug}Form->validate()){
|
||||
${slug} = $this->{slug}Service->create(${slug}Form);
|
||||
if (${slug}){
|
||||
$this->redirect("/admin/{slug}/view/" . ${slug}->id);
|
||||
}
|
||||
}
|
||||
$this->redirect("/admin/{slug}/create");
|
||||
}
|
||||
|
||||
public function actionIndex($page_number = 1): void
|
||||
{
|
||||
$this->cgView->render("index.php", ['page_number' => $page_number]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionView($id): void
|
||||
{
|
||||
${slug} = {model}::find($id);
|
||||
|
||||
if (!${slug}){
|
||||
throw new Exception(message: "The {slug} not found");
|
||||
}
|
||||
$this->cgView->render("view.php", ['{slug}' => ${slug}]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionUpdate($id): void
|
||||
{
|
||||
$model = {model}::find($id);
|
||||
if (!$model){
|
||||
throw new Exception(message: "The {slug} not found");
|
||||
}
|
||||
|
||||
$this->cgView->render("form.php", ['model' => $model]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionEdit($id): void
|
||||
{
|
||||
${slug} = {model}::find($id);
|
||||
if (!${slug}){
|
||||
throw new Exception(message: "The {slug} not found");
|
||||
}
|
||||
${slug}Form = new Create{model}Form();
|
||||
${slug}Service = new {model}Service();
|
||||
${slug}Form->load($_REQUEST);
|
||||
if (${slug}Form->validate()) {
|
||||
${slug} = ${slug}Service->update(${slug}Form, ${slug});
|
||||
if (${slug}) {
|
||||
$this->redirect("/admin/{slug}/view/" . ${slug}->id);
|
||||
}
|
||||
}
|
||||
$this->redirect("/admin/{slug}/update/" . $id);
|
||||
}
|
||||
|
||||
#[NoReturn] public function actionDelete($id): void
|
||||
{
|
||||
${slug} = {model}::find($id)->first();
|
||||
${slug}->delete();
|
||||
$this->redirect("/admin/{slug}/");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user