andy87 / yii2-dnk-generator
My workflow for `template path` pattern
Requires
- yiisoft/yii2: *
This package is auto-updated.
Last update: 2024-12-16 03:16:19 UTC
README
Personal library on Yii2 advanced
for generate files pack and follow pattern: template path
, adapter
and other.
Navigation.
-
Description
-
Use
-
Profit
-
Setup
Description
Legend:
- S_P&M > == set
property
&methods
in to - Env:
all
== all environment ( common/console/backend/frontend ) - Env:
backend
== environmentbackend
- Env:
frontend
== environmentfrontend
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 labelscommon/components/db/Setup.php
- Data base config: CHARACTER, COLLATE, ENGINEcommon/components/db/Tables.php
- class with table names used Part::CONSTANT_NAMEcommon/components/resources/GridViewResource.php
- GridView Resources ( search, dataProvider )backend/components/controllers/BackendController.php
- Base backend controller with common methodsfrontend/components/controllers/FrontendController.php
- Base frontend controller with common methodscommon/components/resources/crud/CreateResource.php
- common resource for view createcommon/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
- migration
-
common/models
-
common/services
- common-service
common/services/UserService.php
template
- common-service
-
backend/models
-
backend/services
- backend-service
backend/services/UserService.php
template
- backend-service
-
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-create-resource
-
backend/controller
- backend-model-controller
backend/controller/cruds/UserController.php
template
- backend-model-controller
-
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
- backend-views-form
-
frontend/models
-
frontend/service
- frontend-service
frontend/service/UserService.php
template
- frontend-service
-
frontend/resources
-
frontend/controllers
- frontend-model-controller
frontend/consrollers/UserController.php
template
- frontend-model-controller
-
frontend/views
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
templatecommon/models/sources/UserSource.php
templatefrontend/service/UserService.php
template
command
php yii dnk/gen-models `string` `bool`
Generate model
files for needle table(part).
-
common/models
-
backend/models
-
frontend/models
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
- backend-model-controller
-
frontend/controllers
- frontend-model-controller
frontend/consrollers/UserController.php
template
- frontend-model-controller
command
php yii dnk/gen-services `string` `bool`
Generate service
files for needle table(part).
-
common/services
- common-service
common/services/UserService.php
template
- common-service
-
backend/services
- backend-service
backend/services/UserService.php
template
- backend-service
-
frontend/service
- frontend-service
frontend/service/UserService.php
template
- frontend-service
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/
-
backend/test/unit/
-
frontend/test/unit/
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
- common-service
-
backend/services
- backend-service
backend/services/UserService.php
template
- backend-service
-
frontend/service
- frontend-service
frontend/service/UserService.php
template
- frontend-service
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
- backend-views-form
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
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.
- exec command
composer require andy87/yii2-dnk-generator:dev-master
#OR
php composer.local require andy87/yii2-dnk-generator:dev-master
- 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/... commandparts
- table lis. for user * in command propertybaseClasses
- list of based class names for generated file
-
exec command dnk/setup
-
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` ] ] ] ]
- Add
Entity
Class:common\components\Entity
, addpublic const
/** @var int */ public const CAMEL_CASE = 2;
- Add
Tables
Class:common\components\db
, updatepublic const NAMES
/** @var int */ public const NAMES = [ // other code ... Entity::CAMEL_CASE => 'snake_case', ];