xsolve-pl / model-factory-bundle
此扩展包提供了一个灵活的框架来组织模型工厂。
Requires
- php: ^5.6|^7.0|^8.0
- symfony/framework-bundle: ^2.3|^3.0|^4.0|^5.0|^6.0
- xsolve-pl/model-factory: ^1.0
Requires (Dev)
- phpmd/phpmd: ^2.4
- phpspec/prophecy: ^1.16
- phpunit/phpunit: ^5.5|^6.0|^7.0|^8.0|^9.0
- sebastian/phpcpd: ^2.0
- symfony/phpunit-bridge: ^3.0|^4.0|^5.0
This package is not auto-updated.
Last update: 2024-09-18 23:23:00 UTC
README
目录
简介
此扩展包封装了xsolve-pl/model-factory 库,并允许使用标签组合声明为服务的模型工厂集合。
有关特定用例的详细信息,请参阅库文档。
许可证
此扩展包采用MIT许可证。请参阅LICENSE文件中的完整许可证。
入门
使用以下Composer命令将此扩展包包含到您的Symfony项目中(假设已全局安装)
$ composer require xsolve-pl/model-factory-bundle
有关Composer的更多信息,请参阅其简介。
之后,您需要通过向您的项目中的app/AppKernel.php文件添加一行来启用此扩展包
<?php // app/AppKernel.php // ... class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new Xsolve\ModelFactoryBundle\XsolveModelFactoryBundle(), ); // ... } }
就这样 - 现在您已准备就绪!
使用示例
将模型工厂分组到集合中
为了便于为多个对象生成模型,可以将模型工厂分组到集合中。如果您的应用程序提供多个API(或多个API版本,这些版本之间差异很大,以至于它们使用了完全不同的模型),则可以分别将工厂分组到不同的集合中,从而避免生成不正确的模型的风险。
通过提供专门编译器传递,该传递使用模型工厂服务定义上的标签将它们注入适当的集合,使将模型工厂分组到集合中变得更加容易。以下是一个services.xml文件的示例:
<?xml version="1.0" ?> <container xmlns="https://symfony.com.cn/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://symfony.com.cn/schema/dic/services https://symfony.com.cn/schema/dic/services/services-1.0.xsd"> <services> <service id="example.model_factory_collection.first" class="Xsolve\ModelFactoryBundle\ModelFactoryCollection\ModelFactoryCollection" /> <service id="example.model_factory_collection.second" class="Xsolve\ModelFactoryBundle\ModelFactoryCollection\ModelFactoryCollection" /> <service id="example.model_factory.foo" class="Example\FooModelFactory" > <tag name="xsolve.model_factory_bundle.model_factory" model-factory-collection-id="example.model_factory_collection.first" /> <tag name="xsolve.model_factory_bundle.model_factory" model-factory-collection-id="example.model_factory_collection.second" /> </service> </services> </container>
此片段定义了两个模型工厂集合(分别具有ID example.model_factory_collection.first 和 example.model_factory_collection.second)。它还定义了一个单独的模型工厂(ID为example.model_factory.foo)。此服务分配了具有等于xsolve.model_factory_bundle.model_factory(这将导致它由Xsolve\ModelFactoryBundle\DependencyInjection\CompilerPass\ModelFactoryCollectionCompilerPass处理)的name属性和包含相应集合服务ID的model-factory-collection-id属性的标签。