Skip to content

Commit dba70d7

Browse files
committed
♻️ 重構(Model.php):改進物件創建邏輯以使用反射和容器
🔧 移除(composer.json):刪除不必要的laminas/laminas-di依賴 這次提交的重構旨在提升物件創建的靈活性,通過使用反射來獲取建構函數的參數,並從容器中解析依賴,這樣可以更好地支持依賴注入。同時,移除不再需要的laminas/laminas-di依賴,以簡化依賴管理。
1 parent d14822d commit dba70d7

File tree

3 files changed

+23
-81
lines changed

3 files changed

+23
-81
lines changed

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"laminas/laminas-paginator": "^2.11",
2323
"vlucas/phpdotenv": "^5.4",
2424
"psr/container": "*",
25-
"laminas/laminas-di": "^3.12",
2625
"illuminate/collections": "^9.52|^10.0"
2726
},
2827
"require-dev": {

composer.lock

Lines changed: 1 addition & 78 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Model.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,29 @@ function jsonSerialize()
119119
static function Create(?array $data = [])
120120
{
121121

122-
$injector = new Injector(null, self::GetSchema()->getContainer());
122+
//container
123+
$container = self::GetSchema()->getContainer();
123124

124-
$obj = $injector->create(static::class);
125+
//reflector class
126+
$ref_class = new ReflectionClass(static::class);
127+
128+
//get contructor
129+
$constructor = $ref_class->getConstructor();
130+
131+
//get parameters
132+
$parameters = $constructor->getParameters();
133+
134+
$args = [];
135+
foreach ($parameters as $parameter) {
136+
if ($container->has($parameter->getType()->getName())) {
137+
$args[] = $container->get($parameter->getType()->getName());
138+
} else {
139+
$args[] = null;
140+
}
141+
}
142+
143+
//create instance with args
144+
$obj = $ref_class->newInstanceArgs($args);
125145

126146
$fields = $obj->__fields();
127147
foreach ($data as $field => $value) {

0 commit comments

Comments
 (0)