gordensong/laravel-autowire
Composer 安装命令:
composer require gordensong/laravel-autowire
包简介
laraval autowire 微擎
README 文档
README
https://gitee.com/gordensong/laravel-autowire
自动绑定类属性
对于声明了
Autowire注解的类,调用autowire($class)可以自动初始化声明Autowire注解的属性.属性自动绑定不限于单元测试使用,Repository, Service, Action 同样可以使用。
注意:不要循环引用注解属性。
直接上栗子
Human
<?php
use GordenSong\Laravel\Support\Autowire;
#[Autowire]
class Human
{
#[Autowire]
public ?Head $head = null;
}
Head
#[Autowire]
class Head
{
#[Autowire]
public ?Mouth $mouth = null;
}
Mouth
class Mouth
{
public ?Teeth $teeth = null;
}
Teeth
class Teeth
{
}
helper demo
<?php
namespace Tests;
class HelperTest extends TestCase
{
public function test_autowire()
{
/** @var Human $cat */
$cat = autowire(Human::class);
self::assertNotNull($cat);
self::assertNotNull($cat->head);
self::assertNull($cat->head->mouth);
}
public function test_autowire_recursive()
{
/** @var Human $cat */
$cat = autowire(Human::class, true);
self::assertNotNull($cat);
self::assertNotNull($cat->head);
self::assertNotNull($cat->head->mouth);
self::assertNull($cat->head->mouth->teeth);
}
}
TestCase 自动注入
<?php
namespace Tests;
use GordenSong\Laravel\Support\Autowire;
use GordenSong\Laravel\Support\AutowireTrait;
class NotRecursiveAutowireTest extends TestCase
{
// 第一步:引用 Trait use AutowireTrait
use AutowireTrait;
protected function setUp(): void
{
parent::setUp();
// 第二步: $this->autowireProperties(); 绑定值为空的属性
$this->autowireProperties(); // 不递归绑定
}
// 第三步:使用注解
#[Autowire]
private ?Human $human = null;
public function test_auto_wire_head_is_null()
{
self::assertNotNull($this->human);
self::assertNull($this->human->head);
}
}
TestCase 递归注入
<?php
namespace Tests;
use GordenSong\Laravel\Support\Autowire;
use GordenSong\Laravel\Support\AutowireTrait;
class RecursiveAutowireTest extends TestCase
{
use AutowireTrait; // 1.
protected function setUp(): void
{
parent::setUp();
$this->autowireProperties(true); // 2. 递归绑定
}
// 3. 使用注解
#[Autowire]
private ?Human $cat = null;
public function test_auto_wire_head_is_null()
{
self::assertNotNull($this->cat);
self::assertNotNull($this->cat->head);
self::assertNotNull($this->cat->head->mouth);
self::assertNull($this->cat->head->mouth->teeth); // teeth 未加注解
}
}
安装
composer require --dev gordensong/laravel-autowire
说明
Function
1. autowire($class) 等价于 app($class)
function autowire(string $class, bool $recursive = false) : mixed
2. 自动绑定属性值, 对象内部使用。
function autowireProperties(object $object, bool $recursive = false)
gordensong/laravel-autowire 适用场景与选型建议
gordensong/laravel-autowire 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 872 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 08 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 gordensong/laravel-autowire 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 gordensong/laravel-autowire 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 872
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-08-26