jeroen-g / autowire
使用PHP 8属性在Laravel中自动装配和配置。
Requires
- php: ^8.0
- ergebnis/classy: ^1.3
- illuminate/console: ~8.0||~9.0||~10.0||~11.0
- illuminate/support: ~8.0||~9.0||~10.0||~11.0
- webmozart/assert: ^1.10
Requires (Dev)
- infection/infection: ^0.25
- mockery/mockery: ^1.4
- phpunit/phpunit: ~9.0
- symplify/easy-coding-standard: ^9.0
README
使用PHP 8属性在Laravel中自动装配和配置。
安装
通过Composer
composer require jeroen-g/autowire
您需要配置文件来更改它应该查找的位置
php artisan vendor:publish --tag=autowire.config
用法
自动装配
你是否厌倦了总是将抽象接口绑定到具体类上?
$this->app->bind(HelloInterface::class, WorldClass::class);
使用本包的PHP 8属性来自动装配您的任何接口
namespace App\Contracts; use JeroenG\Autowire\Attribute\Autowire; #[Autowire] interface HelloInterface { public function hello(): string; }
实现该接口的类不需要任何更改
namespace App; use App\Contracts\HelloInterface; class WorldClass implements HelloInterface { public function hello(): string { return 'world'; } }
Autowire包将遍历类并将抽象接口绑定到具体类。如果容器中已经存在绑定,则将跳过自动装配。
标签
如果您想快速为接口的所有实现添加标签,只需将Tag
属性添加到接口中即可
namespace App\Contracts; use JeroenG\Autowire\Attribute\Tag; #[Tag('myTag')] interface HelloInterface { public function hello(): string; }
现在所有实现都可在'myTag'标签下使用
$this->app->when(Greeting::class) ->needs(HelloInterface::class) ->giveTagged('myTag');
如果未指定属性中的标签值,则使用类的完全限定名称作为标签
namespace App\Contracts; use JeroenG\Autowire\Attribute\Tag; #[Tag] interface GoodbyeInterface { public function goodbye(): string; }
$this->app->when(Greeting::class) ->needs(GoodbyeInterface::class) ->giveTagged(GoodbyeInterface::class);
配置
我个人喜欢通过注入依赖项而不是使用make()
助手函数来解析它们。然而,这意味着要编写绑定定义,例如
$this->app->when($x)->needs($y)->give($z);
不再需要使用Configure属性!下面是WorldClass示例再次出现
namespace App; use App\Contracts\HelloInterface; #[Configure(['$message' => 'world'])] class WorldClass { private $message; public function __construct($message) { $this->message = $message; } }
在这个例子中,message是一个简单的字符串。然而,它可以是一个配置值或类的引用!配置和服务定义的表示法与Symfony中使用的相同。
// Will get the value set in config/app.php #[Configure(['$message' => '%app.message%'])] // Will inject an instance of the Message class #[Configure(['$message' => '@App\Domain\Message'])] // When you have multiple constructor arguments #[Configure(['$message' => '%app.message%', '$logger' => '@Psr\Log\LoggerInterface'])]
监听事件
如果您使用了很多事件,那么EventServiceProvider
可能会非常长且混乱
protected $listen = [ Registered::class => [ UpdateLastLogin::class, ... ], ... ];
使用本包的PHP 8属性,您可以在每个监听器旁边定义事件
#[Listen(Registered::class)] #[Listen(Login::class)] class UpdateLastLoginListener { ... }
该包将遍历类并将监听器绑定到事件类。
缓存
可以使用php artisan autowire:cache
命令缓存自动装配、配置和监听器。类似地,可以使用php artisan autowire:clear
清除它们。请注意,缓存意味着它不会遍历所有类,并且对注释的更改不会被加载。
配置
包的配置可以在config/autowire.php
中找到。它应包含Autowire应查找接口和实现的目录列表。
自定义属性
可以使用自定义属性类来实现自动装配或配置功能之一或两者
- 创建一个自定义属性类,确保实现所需的
JeroenG\Autowire\Attribute\AutowireInterface
或JeroenG\Autowire\Attribute\ConfigureInterface
。 - 将
autowire_attribute
、configure_attribute
、tag_attribute
或listen_attribute
设置添加到config/autowire.php
文件中,包含您自定义属性类的完全限定名称。 - 使用您的自定义属性标记您想要自动装配或配置的接口或类。
变更日志
有关最近更改的更多信息,请参阅变更日志。
致谢
许可证
MIT。有关更多信息,请参阅许可证文件。