mbi / code-generator-bundle
此包提供实体子包的生成器
dev-master
2020-02-02 20:58 UTC
Requires
- php: ^7.1
- doctrine/orm: ^2.5
- friendsofphp/php-cs-fixer: ^2.16
- phpunit/phpunit: ^7.0
- psr/log: ^1.0
- sensio/framework-extra-bundle: ^3.0.13|^4.0|^5.0
- slevomat/coding-standard: ^4.7|^5.0|^6.1
- squizlabs/php_codesniffer: ^3.5
- symfony/asset: ^3.4|^4.3
- symfony/config: ^3.4|^4.0
- symfony/console: ^3.4|^4.0
- symfony/debug: ^3.4|^4.0
- symfony/dependency-injection: ^3.4|^4.0
- symfony/event-dispatcher: ^3.4|^4.0
- symfony/finder: ^3.4|^4.0
- symfony/framework-bundle: ^3.4|^4.0
- symfony/http-foundation: ^3.4|^4.0
- symfony/http-kernel: ^3.4|^4.0
- symfony/phpunit-bridge: ^4.1
- symfony/twig-bundle: ^2.8|^3.0|^4.0
- symfony/validator: ^3.4|^4.3
- symfony/yaml: ^3.4|^4.0
- twig/twig: ^2.0
This package is auto-updated.
Last update: 2024-09-29 05:35:57 UTC
README
一个用于创建库子包的 Symfony Bundle。具有实体(doctrine)+数据对象、工厂、仓库和集成服务。可能还会提供更多蓝图和栈。
安装
需要 composer 包
composer require --dev mbi/code-generator-bundle:dev-master
在 bundles.php
中注册包
return [
...
\Mbi\CodeGeneratorBundle\MbiCodeGeneratorBundle::class => ['dev' => true, 'test' => true],
...
];
(如果需要)添加到配置中
mbi_code_generator:
default_class_annotations:
author: "AUTHOR NAME"
license: "LICENSE"
使用命令创建子包
php bin/console --env=test mbi:cgb:generate:package
Enter name for Entity (like Blog): Blog
Enter namespace for Subpackage (like Mbi\Application\Content\Blog): Mbi\Application\Content\Blog
Enter relative path for Subpackage (will be appended: e.g. kernel-project-dir + src/Application/Content):src/Application/Content
Create entity-property? (Y|n)Y
Enter property-name: name
Enter property-type (string,bool,int,datetime,float):
[0] string
[1] bool
[2] int
[3] datetime
[4] float
> 0
type-option nullable [/^(0|1)$/]: 0
type-option length [/^[0-9]+$/]: 255
Create entity-property? (Y|n)
Enter property-name: created
Enter property-type (string,bool,int,datetime,float):
[0] string
[1] bool
[2] int
[3] datetime
[4] float
> 3
type-option nullable [/^(0|1)$/]: 0
Create entity-property? (Y|n)n
5 classes generated
- Mbi\Application\Content\Blog\Entity\Blog
- Mbi\Application\Content\Blog\Form\BlogData
- Mbi\Application\Content\Blog\BlogFactory
- Mbi\Application\Content\Blog\BlogRepository
- Mbi\Application\Content\Blog\BlogService
想要更多?
您可以尝试创建并处理自己的蓝图,请随意:扩展 Mbi\CodeGeneratorBundle\Library\Blueprint\AbstractBlueprint
并使用类构建器来模板化一个类
$this->classBuilder = $this->classBuilderFactory->createBuilder();
$this->classBuilder->initClassTemplate(
$baseClassName,
$path,
$namespace,
$parentClass
);
$this->classBuilder->useTrait(TRAIT::class);
$this->classBuilder->implementInterface(INTERFACE::class);
$property = $this->getClassBuilder()->addPropertyByInstruction(
PropertyInstruction::create('PROPERTY_NAME', DataType::INTEGER, true, false, false, true),
[
DoctrineAnnotationInstruction::createByClass(Id::class),
DoctrineAnnotationInstruction::createByClass(GeneratedValue::class, ['strategy' => 'AUTO']),
]
);
$this->getClassBuilder()->createMethod(
'METHOD_NAME',
[BaseAnnotationInstruction::createByIdentAndValue(AnnotationTemplate::IDENT_RETURN,'entity')],
[],
function () use ($entityClassName) {
return 'return new '.$entityClassName.'();';
},
MethodTemplate::VISIBILITY_PROTECTED
);
查看可用的蓝图(Mbi\CodeGeneratorBundle\Blueprint
)以了解其工作方式和可能实现的功能。
使用 Mbi\CodeGeneratorBundle\Library\Blueprint\BlueprintProcessor
生成并持久化类模板
$generatedTemplate = $this->get('mbi_code_generator.blueprint_processor')->generate(
SomeBlueprint::class,
'NewEntity',
'/path/to/namespace',
'Package/Namespace',
BlueprintSettings::create(),
TemplateStack::create()
);