vcn / symfony-autofactory
库,它使得在 Symfony 服务容器中自动注册工厂类成为可能
Requires
- php: >=7.1
- ext-mbstring: *
- doctrine/annotations: ^1.6
- symfony/dependency-injection: ^5.1 || ^6.4
This package is auto-updated.
Last update: 2024-09-22 17:11:55 UTC
README
vcn/symfony-autofactory 使得为 Symfony 定义服务工厂类变得容易。
用法
请确保您对 Symfony 中的依赖注入工作有很好的理解。您可以在这里找到他们的文档。
要开始使用 vcn/symfony-autofactory,最简单的方法是安装 vcn/symfony-autofactory-bundle。如果您确保所有 AutoFactory-instances 都自动配置,其余的将自动工作。
如果您不想使用捆绑包,您需要
- 将 AutoFactoryPass 添加到您的内核编译器传递中
- 确保您的所有 AutoFactory-instances 都具有在 AutoFactoryPass 中配置的标签
用法
基本用法
要创建一个 AutoFactory,创建一个实现了 AutoFactory 接口的类。要使类方法被视为工厂,它必须是公共的、静态的,并且必须定义一个类返回类型。
<?php use Vcn\Symfony\AutoFactory\AutoFactory; class FooFactory implements AutoFactory { public static function createFoo(): Foo { return new Foo(); } }
配置
可以使用注解来调整工厂的细节。
@Alias
别名概念在这里的 Symfony 文档中进行了演示。您可以通过向您的工厂方法添加一个或多个 @Alias
-注解来向依赖项添加一个或多个别名。该注解必须接收两个命名参数: id: string
和 public: bool
。名称是自解释的。
<?php use Vcn\Symfony\AutoFactory\AutoFactory; use Vcn\Symfony\AutoFactory\Annotation\Alias; class FooFactory implements AutoFactory { /** * @Alias(id="foo.service", public=true) */ public static function createFoo(): Foo { return new Foo(); } }
@Autoconfigure
自动配置概念在这里的 Symfony 文档中进行了解释。默认情况下,工厂是自动配置的。您可以使用 @Autoconfigure
-注解在类级别和方法级别更改此设置。该注解接受一个未命名的布尔参数。
<?php use Vcn\Symfony\AutoFactory\AutoFactory; use Vcn\Symfony\AutoFactory\Annotation\Autoconfigure; /** * Override the default, now all factory methods in this class are not autoconfigured by default * @Autoconfigure(false) */ class FooFactory implements AutoFactory { /** * But we want autoconfiguration for this specific dependency * @Autoconfigure(true) */ public static function createFoo(): Foo { return new Foo(); } }
@Autowire
自动连接概念在这里和这里的 Symfony 文档中进行了解释。默认情况下,工厂是自动连接的。您可以使用 @Autowire
-注解在类级别和方法级别更改此设置。该注解接受一个未命名的布尔参数。
<?php use Vcn\Symfony\AutoFactory\AutoFactory; use Vcn\Symfony\AutoFactory\Annotation\Autowire; /** * Override the default, now all factory methods in this class are not autowired by default * @Autowire(false) */ class FooFactory implements AutoFactory { public static function createFoo(): Foo { return new Foo(); } /** * But we want autowiring for this specific method * @Autowire(true) */ public static function createBar(): Bar { return new Bar(); } }
@Bind
绑定参数的概念在这里的 Symfony 文档中进行了解释。要将工厂方法的参数绑定到由 id 指定的依赖项,您可以使用 @Bind
-注解。该注解必须接收两个命名参数: arg: string
和 id: string
。 arg
的值指的是被绑定的工厂方法的参数名称,应包含前面的美元符号。 id
应引用一个有效的服务 id。
<?php use Vcn\Symfony\AutoFactory\AutoFactory; use Vcn\Symfony\AutoFactory\Annotation\Bind; class FooFactory implements AutoFactory { /** * @Bind(arg="$barService", id="bar.service") */ public static function createFoo(Bar $barService): Foo { return new Foo($barService->getSomethingINeed()); } }
@Id
在Symfony文档中,关于ids的概念在这里有所暗示(点击此处查看)。当没有使用@Id
注解时,将使用依赖项的完全限定类名作为id。您可以使用@Id
注解来覆盖它。注解必须接收一个无名称的字符串参数,其中包含要设置的id。不允许有多个@Id
注解。对于需要用多个id来寻址依赖项的使用场景,请使用@Alias
注解。
<?php use Vcn\Symfony\AutoFactory\AutoFactory; use Vcn\Symfony\AutoFactory\Annotation\Bind; class FooFactory implements AutoFactory { /** * @Id("foo.consumer") */ public static function createConsumerFoo(): Foo { return new Foo(); } /** * @Id("foo.business") */ public static function createBusinessFoo(): Foo { return new Foo(); } /** * @Bind(arg="$foo", id="foo.consumer") */ public static function createConsumerBar(Foo $foo): Bar { return new Bar($foo); } /** * @Bind(arg="$foo", id="foo.business") */ public static function createBusinessBar(Foo $foo): Bar { return new Bar($foo); } }
@IsPublic
在Symfony文档中,关于公开性的概念在这里进行了解释(点击此处查看)。默认情况下,工厂不是公开的。您可以使用@IsPublic
注解在类级别和方法级别更改此设置。注解必须接收一个无名称的布尔参数。
<?php use Vcn\Symfony\AutoFactory\AutoFactory; use Vcn\Symfony\AutoFactory\Annotation\IsPublic; /** * Override the default, now all factory methods in this class are public by default * @IsPublic(true) */ class FooFactory implements AutoFactory { public static function createFoo(): Foo { return new Foo(); } /** * We can override again. This dependency will not be public after all. * @IsPublic(false) */ public static function createBar(): Bar { return new Bar(); } }
@Tag
在Symfony文档中,关于标签的概念在这里进行了解释(点击此处查看)。您可以在任何工厂方法上添加一个或多个@Tag
注解。每个@Tag
注解必须有一个无名称的字符串参数来定义名称,并且可以有多余的命名参数来定义额外的标签属性。
<?php use Vcn\Symfony\AutoFactory\AutoFactory; use Vcn\Symfony\AutoFactory\Annotation\Tag; class FooFactory implements AutoFactory { /** * @Tag("some.tag") * @Tag("foo.tag", important=true, bar="baz") */ public static function createFoo(): Foo { return new Foo(); } }
示例
示例可以在examples目录中找到。