1 Star 2 Fork 0

gordensong / laravel-autowire

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

https://gitee.com/gordensong/laravel-autowire

自动绑定类属性

  1. 对于声明了 Autowire 注解的类,调用 autowire($class) 可以自动初始化声明 Autowire 注解的属性.

  2. 属性自动绑定不限于单元测试使用,Repository, Service, Action 同样可以使用。

  3. 注意:不要循环引用注解属性。

直接上栗子

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)
MIT License Copyright (c) 2021 gordensong Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

基于 laravel,Autowire 注解的方式自动注入。 展开 收起
PHP
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
PHP
1
https://gitee.com/gordensong/laravel-autowire.git
git@gitee.com:gordensong/laravel-autowire.git
gordensong
laravel-autowire
laravel-autowire
master

搜索帮助