22
33Laravel 的依賴注入很棒,但我們讓他更好!
44
5+ - [ 關於] ( ##關於 )
6+ - [ 安裝] ( ##安裝 )
7+ - [ 如何使用] ( ##如何使用 )
8+ - [ 不會被處理的屬性] ( ##不會被處理的屬性 )
9+ - [ 限制] ( ##限制 )
10+ - [ 覆寫建構子] ( ##覆寫建構子 )
11+ - [ 避免私有屬性] ( ##避免私有屬性 )
12+
13+
514## 關於
615
716想想看,當你專案越來越大,一定有遇過又臭又長的建構子:
817
918``` php
19+ use A2Workspace\AutoMount\AutoMountDependencies;
20+
1021class 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+
3851class 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
6277composer 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```
0 commit comments