andy87/yii2-dnk-generator

My workflow for `template path` pattern

dev-master 2023-03-15 23:23 UTC

This package is auto-updated.

Last update: 2024-11-16 03:07:22 UTC


README

Personal library on Yii2 advanced for generate files pack and follow pattern: template path, adapter and other.

Navigation.

Description

Legend:

  • S_P&M > == set property & methods in to
  • Env:all == all environment ( common/console/backend/frontend )
  • Env:backend == environment backend
  • Env:frontend == environment frontend

Models & Forms.

📄 common/components/core/BaseModel.php            // S_P&M > all children models on Env:`all`
|
└─📄 common/models/sources/__Model__Source.php     // model generated by `gii`, S_P&M > all children models on Env:all
  |
  ├─📄 common/models/items/__Model__.php           // S_P&M > models by part `__model__` on Env:`all`
  |  |
  | ├─📄 backend/models/items/__Model__.php        // S_P&M > `model` by part `__model__` on Env:`backend`
  | | └─📄 backend/models/forms/__Model__Form.php  // S_P&M > `form` on model `backend/models/items/__Model__` on Env:`backend`
  | |
  | └─📄 frontend/models/items/__Model__.php       // S_P&M > `model` by part `__model__` on Env:`frontend`
  |   └─📄 frontend/models/forms/__Model__Form.php // S_P&M > `form` on model `frontend/models/items/__Model__` on Env:`frontend`
  |
  └─📄 common/models/search/__Model__Search.php      // model for generated by `gii`, S_P&M > all search children models on Env:all
    ├─📄 backend/models/search/__Model__Search.php   // S_P&M > `search model` by part `__model__` on Env:`backend`
    └─📄 frontend/models/search/__Model__Search.php  // S_P&M > `search model` by part `__model__` on Env:`frontend`

Services.

📄 common/components/core/BaseService.php              // S_P&M > all services on Env:`all`
|
├─📄 backend/components/services/__Model__Service.php  // S_P&M > services by part `__Model__` on Env:`backend`
|
└─📄 frontend/components/services/__Model__Service.php // S_P&M > services by part `__Model__` on Env:`frontend`

Controllers.

📄 common/components/core/BaseController.php                 // S_P&M > all controllers on Env:`all`
|
├─📄 backend/components/controllers/BackendController.php    // S_P&M > all controllers on Env:`backend`
| └─📄 backend/components/controllers/CrudController.php     // S_P&M > all controllers generated with `crud` on Env:`backend`
|   └─📄 backend/controllers/crud/__Model__Controller.php    // S_P&M > generated `crud` controllers for moled `__Model__` on Env:`backend`
|
└─📄 frontend/components/controllers/FrontendController.php  // S_P&M > all controllers on Env:`frontend`
  └─📄 frontend/controllers/__Model__Controller.php          // S_P&M > controllers for moled `__Model__` on Env:`frontend`

Resources.

📄 common/components/core/BaseResource.php                         // S_P&M > all resources on Env:`all` 
|
├─📄 backend/components/core/resources/BackendResource.php         // S_P&M > all resources on Env:`backend`
| |
| ├─📄 backend/components/resources/CrudListResource.php          // S_P&M > all resources for view `index` on Env:`backend`
| | └─📄 backend/resources/__model__/__Model__ListResource.php    // S_P&M > resources for moled `__Model__` on view `index` Env:`backend`
| |
| ├─📄 backend/components/resources/CrudReadResource.php           // S_P&M > all resources for view `view` on Env:`backend`
| | └─📄 backend/resources/__model__/__Model__ReadResource.php     // S_P&M > resources for moled `__Model__` on view `read` Env:`backend`
| |
| └─📄 backend/components/resources/CrudUpdateResource.php         // S_P&M > all resources for view `update` on Env:`backend`
|   └─📄 backend/resources/__model__/__Model__UpdateResource.php   // S_P&M > resources for moled `__Model__` on view `update` Env:`backend`
|
└─📄 frontend/components/core/resources/FrontendResource.php       // S_P&M > all resources on Env:`frontend` 
  |
  ├─📄 frontend/resources/ReadResource.php                         // S_P&M > all resources for view `view` on Env:`frontend`
  | └─📄 frontend/resources/__model__/__Model__ReadResource.php    // S_P&M > resources for moled `__Model__` on view `read` Env:`frontend`
  |
  └─📄 frontend/resources/ListResource                             // S_P&M > all resources for view `list` on `frontend` environment
    └─📄 frontend/resources/__model__/__Model__ListResource.php    // S_P&M > resources for moled `__Model__` on view `list` Env:`frontend`

Usage/Examples.

command

php yii dnk/setup `bool`

description

Generate files:

  • common/components/Entity.php - service parts with labels
  • common/components/db/Setup.php - Data base config: CHARACTER, COLLATE, ENGINE
  • common/components/db/Tables.php - class with table names used Part::CONSTANT_NAME
  • common/components/resources/GridViewResource.php - GridView Resources ( search, dataProvider )
  • backend/components/controllers/BackendController.php - Base backend controller with common methods
  • frontend/components/controllers/FrontendController.php - Base frontend controller with common methods
  • common/components/resources/crud/CreateResource.php - common resource for view create
  • common/components/resources/crud/ListResource.php - common resource for view list(index)
  • common/components/resources/crud/ReadResource.php - common resource for view read(view)
  • common/components/resources/crud/UpdateResource.php - common resource for view update

command

php yii dnk/run `string` `bool`

generate basic file pack:

description

  • console/migration

    • migration console/migration/m000000_000000_create_table__user.php template
  • common/models

    • common-model-source common/models/sources/UserSource.php template
    • common-model-item common/models/items/User.php template
    • common-model-search common/models/search/UserSearch.php template
  • common/services

    • common-service common/services/UserService.php template
  • backend/models

    • backend-model-item backend/models/items/User.php template
    • backend-model-form backend/models/forms/UserForm.php template
    • backend-model-search backend/models/search/UserSearch.php template
  • backend/services

    • backend-service backend/services/UserService.php template
  • backend/resources

    • backend-create-resource backend/resources/user/UserCreateResource.php template
    • backend-list-resource backend/resources/user/UserListResource.php template
    • backend-list-resource backend/resources/user/UserReadResource.php template
    • backend-update-resource backend/resources/user/UserUpdateResource.php template
  • backend/controller

    • backend-model-controller backend/controller/cruds/UserController.php template
  • backend/views

    • backend-views-form backend/views/user/_form-user.php template
    • backend-views-create backend/views/user/user-create.php template
    • backend-views-update backend/views/user/user-update.php template
    • backend-views-read backend/views/user/user-read.php template
    • backend-views-list backend/views/user/user-list.php template
  • frontend/models

    • frontend-model-item frontend/models/items/User.php template
    • frontend-model-form frontend/models/forms/UserForm.php template
    • frontend-model-search frontend/models/search/UserSearch.php template
  • frontend/service

    • frontend-service frontend/service/UserService.php template
  • frontend/resources

    • frontend-list-resource frontend/resources/user/UserListResource.php template
    • frontend-read-resourcefrontend/resources/user/UserReadResource.php template
  • frontend/controllers

    • frontend-model-controller frontend/consrollers/UserController.php template
  • frontend/views

    • frontend-views-read frontend/views/user/user-read.php template
    • frontend-views-list frontend/views/user/user-list.php template

command

php yii dnk/gii-model `string` `bool`

Generate gii model for select table(part).

* - generate list from config parts

Examples

php yii dnk/gii-model user
php yii dnk/gii-model *

alias

php yii gii/model

command

php yii dnk/gii-crud `string` `bool`

Generate gii crud for select table(part).
* - generate list from config parts

Examples

php yii dnk/gii-crud user
php yii dnk/gii-crud *

alias php yii gii/crud

command

php yii dnk/list `string` `string` `bool`

Generate selected fileType(list fileType) for needle table(part).

description

Generate once file.
php yii dnk/list `string` `string` `bool`

* - generate list from config parts

generate only: common/services/UserService.php template

Generate selected files.
php yii dnk/list user common-services,common-model-source,frontend-service

generate file list:

  • common/services/UserService.php template
  • common/models/sources/UserSource.php template
  • frontend/service/UserService.php template

command

php yii dnk/gen-models `string` `bool`

Generate model files for needle table(part).

  • common/models

    • common-model-source common/models/sources/UserSource.php template
    • common-model-item common/models/items/User.php template
  • backend/models

    • backend-model-item backend/models/items/User.php template
    • backend-model-form backend/models/forms/UserForm.php template
  • frontend/models

    • frontend-model-item frontend/models/items/User.php template
    • frontend-model-form frontend/models/forms/UserForm.php template

command

php yii dnk/gen-controllers `string` `bool`

Generate controller files for needle table(part).

  • backend/controller

    • backend-model-controller backend/controller/cruds/UserController.php template
  • frontend/controllers

    • frontend-model-controller frontend/consrollers/UserController.php template

command

php yii dnk/gen-services `string` `bool`

Generate service files for needle table(part).

  • common/services

    • common-service common/services/UserService.php template
  • backend/services

    • backend-service backend/services/UserService.php template
  • frontend/service

    • frontend-service frontend/service/UserService.php template

command

php yii dnk/gen-tests `string` `bool`

Generate test files for needle table(part).

Example:

php yii dnk/gen-tests user
  • common/test/unit/

    • service common/tests/unit/services/UserServiceTest.php template
    • item common/tests/unit/models/items/UserTest.php template
  • backend/test/unit/

    • service backend/tests/unit/services/UserServiceTest.php template
    • item backend/tests/unit/models/items/UserTest.php template
    • form backend/tests/unit/models/items/UserFormTest.php template
  • frontend/test/unit/

    • service frontend/tests/unit/services/UserServiceTest.php template
    • item frontend/tests/unit/models/items/UserTest.php template
    • form frontend/tests/unit/models/items/UserFormTest.php template

command

php yii dnk/gen-backend-service `string` `bool`

Generate service files for needle table(part).

  • common/services

    • common-service common/services/UserService.php template
  • backend/services

    • backend-service backend/services/UserService.php template
  • frontend/service

    • frontend-service frontend/service/UserService.php template

command

php yii dnk/gen-backend-views `string` `bool`

Generate views files for needle table(part).

  • backend/views
    • backend-views-form backend/views/user/_form-user.php template
    • backend-views-create backend/views/user/user-create.php template
    • backend-views-update backend/views/user/user-update.php template
    • backend-views-read backend/views/user/user-read.php template
    • backend-views-list backend/views/user/user-list.php template

alias php yii dnk/list user backend-views-form,backend-views-create,backend-views-update,backend-views-read,backend-views-list

command

php yii dnk/gen-frontend-views `string` `bool`

Generate views files for needle table(part).

  • frontend/views
    • frontend-views-read frontend/views/user/user-read.php template
    • frontend-views-list frontend/views/user/user-list.php template

alias

php yii dnk/list user frontend-views-read,frontend-views-list

Profit.

Service profit

Example for model User

once endpoint for creating on frontend, backend & tests

common/UserService::create(UserForm $userForm);

backend/controllers/UserController {
   action create {
      if ($this->request->isPost)
      {
         (new backend/UserService())->create($this->request->post());
      }
   }
}

frontend/controllers/UserController {
   action create {
      if ($this->request->isPost)
      {
         (new frontend/UserService())->create($this->request->post());
      }
   }
}

frontend/controllers/api/v1/UserController {
   action create {
      if ($this->request->isPost)
      {
         (new frontend/UserService())->create($this->request->post());
      }
   }
}


// Test for all controllers...

tests/unit/user/CreateTest {
   test{
      $userForm = new UserForm();
      (new common/UserService())->create($userForm);
   }
}

Resource Profit

example:
frontend/resources/user/UsereViewProfileResources {
   public const TEMPLATE = 'user-view-profile';
   
   pub string $display_name;
   pub string $age;
   
   __construct($user) {
      $this->display_name = $user->name;
      $this->age = $user->age;
      // ... other code
   }
}

frontend/controllers/UserController {
   action viewProfile(int $id) {
      $user = new (UserService())->findByID($id);
      $R = new UsereViewProfileResources($user);
      
      return $R->content();
   }
}

frontend/controllers/api/v1/UserController {
   action viewProfile(int $id) {
      $user = new (UserService())->findByID($id);
      $R = new UsereViewProfileResources($user);
      
      return $R->json();
   }
}

frontend/views/user/user-view-profile:
<?php

use frontend/resources/user/UsereViewProfileResources;
 
/** @var UsereViewProfileResources $R - autocomplete and there is no possibility to make a typo */

?>

<div>Profile: <?= $R->display_name ?></div> 
<div>Age: <?= $R->age ?></div>

Yii2 setups

Install.

  1. exec command
composer require andy87/yii2-dnk-generator:dev-master
#OR
php composer.local require andy87/yii2-dnk-generator:dev-master

2. Update config file:

  • advanced: @console\config\main.php
//.. other code

return [
    // .. other config
    
    'controllerMap' => [
        // .. other config
        
        'dnk' => [  // new block
            'class'     => andy87\dnk\GenerateController::class
        ]
        
    ]
]
  • root - directory with folders 'frontend',backend',common' ...
  • command - part of gii/... command
  • parts - table lis. for user * in command property
  • baseClasses - list of based class names for generated file
  1. exec command dnk/setup

  2. Update config for you require, by example:

use common\components\db\Tables;
use common\components\core\BaseModel;
use andy87\dnk\GenerateController;
use andy87\dnk\source\base\BaseController;
use andy87\dnk\source\base\BaseMigration;
use andy87\dnk\source\base\BaseResource;
use andy87\dnk\source\base\BaseService;

//.. other code

return [
    // .. other config

    'controllerMap' => [
        // .. other config
        
        'dnk' => [  // new block
            'class'     => GenerateController::class,
            'command'   => [
                GenerateController::COMMAND_MODEL => implode(' ', [
                    '--modelClass={{CamelCase}}Source',
                    '--ns=common\\models\\sources',
                    '--tableName={{snake_case}}',
                    '--baseClass={{BaseModelClassName}}',
                    '&& yes'
                ]),
                GenerateController::COMMAND_CRUD => implode(' ', [
                    '--modelClass={{CamelCase}}',
                    '--controllerNamespace=backend\\controllers\\crud',
                    '--baseControllerClass=backend\\controllers\\cruds\\{{CamelCase}}Controller',
                    '--viewPath=@backend\\views\\cruds\\{{snake_case}}',
                    '--enableI18N=1',
                    '&& yes'
                ])
            ],
            'entityList' => array_values(Tables::DATA),
            'baseClasses' => [
                GenerateController::BASE_MIGRATE_CLASS => BaseMigration::class, // extends for `Migrations`
                GenerateController::BASE_CONTROLLER_CLASS => BaseController::class, // extends for `Controllers`
                GenerateController::BASE_MODEL_CLASS => BaseModel::class, // extends for `Source` models
                GenerateController::BASE_SERVICE_CLASS => BaseService::class, // extends for `Services`
                GenerateController::BASE_RESOURCE_CLASS => BaseResource::class, // extends for `Resources`
            ]
        ]
    ]
]
  1. Add Entity
    Class: common\components\Entity, add public const
/** @var int */ 
public const CAMEL_CASE = 2;
  1. Add Tables
    Class: common\components\db, update public const NAMES
/** @var int */ 
public const NAMES = [
    // other code ...
    Entity::CAMEL_CASE => 'snake_case',
];