popo / symfony-bridge
Symfony 的 POPO 生成器捆绑包
1.0.1
2023-08-01 14:05 UTC
Requires
- popo/generator: ^6.3
- symfony/config: ^6.3
- symfony/dependency-injection: ^6.3
- symfony/http-kernel: ^6.3
Requires (Dev)
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.2
README
Symfony 捆绑包,用于 POPO 生成器。
安装
composer require popo/symfony-bridge --dev
设置
创建 config/packages/popo.yaml
文件,并设置 POPO 架构文件的存放位置(例如 config/packages/popo
)。
简单版本
# config/packages/popo.yaml popo: config: - schemaPath: config/packages/popo
完整版本
schemaPath
是必需的,其他所有选项都可以被覆盖。
# config/packages/popo.yaml # schemaPath is required, other options can be defined in POPO schema file popo: default: # namespace: ExampleVendor\App\Example outputPath: tests namespaceRoot: ExampleVendor\ schemaPathFilter: # e.g. bundles schemaConfigFilename: # e.g. bundles/project.config.yml ignoreNonExistingSchemaFolder: false schemaFilenameMask: '*.popo.yaml' classPluginCollection: [] mappingPolicyPluginCollection: [] namespacePluginCollection: [] phpFilePluginCollection: [] propertyPluginCollection: [] config: # customer, settings here overwrite the default values - schemaPath: config/packages/popo/customer.popo.yaml # order, settings here overwrite the default values - schemaPath: config/packages/popo/order.popo.yaml # product, settings here overwrite the default values - schemaPath: config/packages/popo/product.popo.yaml # or simply load all at once - schemaPath: config/packages/popo
使用方法
bin/console popo:generate
Generating POPO files... Customer:ExampleVendor\App\Customer\Customer -> tests/App/Customer/Customer.php Order:ExampleVendor\App\Order\Order -> tests/App/Order/Order.php Order:ExampleVendor\App\Order\OrderItem -> tests/App/Order/OrderItem.php Product:ExampleVendor\App\Product\Product -> tests/App/Product/Product.php All done.
有关更多选项,请参阅 POPO 文档。
扩展生成的类以添加自定义逻辑
使用插件向生成的 POPO 类添加自定义逻辑很简单。例如,添加 helloWorld
方法
# config/packages/popo.yaml popo: default: classPluginCollection: - \PopoBundle\Plugin\HelloWorldPopoPlugin
HelloWorld 插件
class HelloWorldPopoPlugin implements ClassPluginInterface { public function run(BuilderPluginInterface $builder): void { $builder->getClass() ->addMethod('helloWorld') ->setReturnType('string') ->setBody('return "Hello World";'); } }
生成的代码
public function helloWorld(): string { return "Hello World"; }
有关更多信息,请参阅 POPO 插件文档。