Skip to content

Commit a5c03b0

Browse files
authored
Merge pull request #2 from A2Workspace/1.0.x
完成說明文件
2 parents 0a3c457 + 3a250fc commit a5c03b0

File tree

3 files changed

+119
-45
lines changed

3 files changed

+119
-45
lines changed

README.md

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,22 @@
22

33
Laravel 的依賴注入很棒,但我們讓他更好!
44

5+
- [關於](##關於)
6+
- [安裝](##安裝)
7+
- [如何使用](##如何使用)
8+
- [不會被處理的屬性](##不會被處理的屬性)
9+
- [限制](##限制)
10+
- [覆寫建構子](##覆寫建構子)
11+
- [避免私有屬性](##避免私有屬性)
12+
13+
514
## 關於
615

716
想想看,當你專案越來越大,一定有遇過又臭又長的建構子:
817

918
```php
19+
use A2Workspace\AutoMount\AutoMountDependencies;
20+
1021
class ProductController extends Controller
1122
{
1223
protected ProductService $productService;
@@ -35,6 +46,8 @@ class ProductController extends Controller
3546
透過 `AutoMountDependencies` 特性自動掛載依賴,讓我們省略繁雜的綁定過程。
3647

3748
```php
49+
use A2Workspace\AutoMount\AutoMountDependencies;
50+
3851
class ProductController extends Controller
3952
{
4053
use AutoMountDependencies; // Add this.
@@ -56,8 +69,111 @@ class ProductController extends Controller
5669

5770
## 安裝
5871

59-
此套件基於 PHP 7.4 的 `typed properties` 特性,請確保你的 PHP 更新到最新版本。
72+
此套件基於 PHP 7.4 的
73+
[Typed Properties](https://www.php.net/manual/en/migration74.new-features.php#migration74.new-features.core.typed-properties)
74+
新功能,請確保你的 PHP 更新到最新版本。
6075

6176
```bash
6277
composer require a2workspace/laravel-automount
78+
```
79+
80+
## 如何使用
81+
82+
僅須在目標類別上使用 `AutoMountDependencies` 特性:
83+
84+
```php
85+
use A2Workspace\AutoMount\AutoMountDependencies;
86+
87+
class PaymentService
88+
{
89+
use AutoMountDependencies;
90+
}
91+
```
92+
93+
接著,有型別定義的類別屬性就會在建構時自動做依賴注入。
94+
95+
```php
96+
use A2Workspace\AutoMount\AutoMountDependencies;
97+
98+
class PaymentService
99+
{
100+
use AutoMountDependencies;
101+
102+
protected PaymentGatewayFactory $factory;
103+
}
104+
```
105+
106+
**注意**: 考慮到繼承,請避免使用 `private` 私有在要被處理的屬性上。
107+
108+
### 不會被處理的屬性
109+
110+
以下類型的屬性會被略過處理:
111+
1. 基本型別 (int, float, bool, array ...)
112+
1. 未定義型別的屬性
113+
1. 定義為 [Nullable](https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.nullable) 的屬性
114+
1. 有初始值的屬性
115+
116+
```php
117+
use A2Workspace\AutoMount\AutoMountDependencies;
118+
119+
class PaymentService
120+
{
121+
use AutoMountDependencies;
122+
123+
protected int $amount; // Pass.
124+
125+
protected array $gateways; // Pass.
126+
127+
protected $something; // Pass.
128+
129+
protected ?PaymentGateway $gateway = null; // Pass.
130+
131+
protected PaymentGatewayFactory $factory; // Do inject.
132+
}
133+
```
134+
135+
## 限制
136+
137+
### 覆寫建構子
138+
139+
`AutoMountDependencies` 特性中定義了在建構子中執行依賴掛載的動作。當你需要覆寫 (Override) `__construct()` 時,記得手動呼叫 `mountDependencies()` 方法。
140+
141+
```php
142+
use A2Workspace\AutoMount\AutoMountDependencies;
143+
144+
class PaymentService
145+
{
146+
use AutoMountDependencies;
147+
148+
public function __construct()
149+
{
150+
$this->mountDependencies(); // Add this line.
151+
152+
// ...
153+
}
154+
}
155+
```
156+
157+
### 避免私有屬性
158+
159+
當型別屬性被定義為私有,會導致繼承後的子類別,在建構時無法寫入屬性而造成錯誤。
160+
161+
請謹慎使用 `private` ,或將屬性定義為 [Nullable](https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.nullable)
162+
163+
```php
164+
use A2Workspace\AutoMount\AutoMountDependencies;
165+
166+
abstract class BasePaymentGateway
167+
{
168+
use AutoMountDependencies;
169+
170+
private PaymentServiceProvider $service;
171+
}
172+
173+
class PaymentGateway extends BasePaymentGateway
174+
{
175+
// ...
176+
}
177+
178+
$gateway = new PaymentGateway; // 拋出 PaymentGateway::$service 未被初始之錯誤
63179
```

src/AutoMountDependencies.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ protected function mountDependencies($override = false)
3636
/**
3737
* 指定屬性或屬性名稱;嘗試解析它定義的型別,並進行依賴注入。
3838
*
39-
* @param \ReflectionProperty|string $property
40-
* @param bool $override
39+
* @param \ReflectionProperty|string $property
40+
* @param bool $override
4141
* @return void
4242
*
4343
* @throws \ReflectionException

src/Support/Reflector.php

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace A2Workspace\AutoMount\Support;
44

55
use ReflectionClass;
6-
use ReflectionObject;
76
use ReflectionProperty;
87
use ReflectionNamedType;
98

@@ -63,45 +62,4 @@ public static function getPropertyHintedClassName(ReflectionProperty $property):
6362

6463
return $name;
6564
}
66-
67-
/**
68-
* 取得包含類別名的完整屬性名稱。
69-
*
70-
* @param \ReflectionProperty $property
71-
* @return string
72-
*/
73-
public static function getPropertyFullName(ReflectionProperty $property): string
74-
{
75-
return sprintf(
76-
'%s::$%s',
77-
$property->getDeclaringClass()->getName(),
78-
$property->getName()
79-
);
80-
}
81-
82-
/**
83-
* 取得物件的動態屬性 (dynamic properties) 列表。
84-
*
85-
* 回傳一個陣列,包含物件在執行階段宣告的,且未定義為成員的屬性。
86-
*
87-
* @param object $object
88-
* @return array
89-
*/
90-
public static function getDynamicProperties(object $object): array
91-
{
92-
$reflector = new ReflectionObject($object);
93-
94-
$properties = $reflector->getProperties();
95-
$properties = array_filter($properties, function (ReflectionProperty $property) {
96-
return ! $property->isDefault();
97-
});
98-
99-
$results = [];
100-
101-
foreach ($properties as $property) {
102-
$results[$property->getName()] = $property->getValue($object);
103-
}
104-
105-
return $results;
106-
}
10765
}

0 commit comments

Comments
 (0)