1 回答

TA貢獻1834條經驗 獲得超8個贊
經過更多的研究和掃描 Symfony 的源代碼后,我找到了一個適合我需要的解決方案:我手動創建了一個實例ArgumentResolver并使用它。
注意我的用例需要解析一個不是服務的對象,因此它不能像任何其他服務一樣通過依賴注入機制來解析。我將其包含在我的答案中,因為它可能對其他人有用:
$resolvers = [];
array_push($resolvers, ...ArgumentResolver::getDefaultArgumentValueResolvers());
array_push($resolvers, new class implements ArgumentValueResolverInterface {
// implement the interface to resolve an argument with a specific type/name
});
$argument_resolver = new ArgumentResolver(new ArgumentMetadataFactory(), $resolvers);
// to use it:
$arguments = $argument_resolver->getArguments($request, [$datasource, 'query']);
$datasource->query(...$arguments);
我希望這對其他人有用。
我無法證明這是解決方案或最佳解決方案,但它似乎對我的用例有用。我歡迎其他人發表評論/回答,以便我們共同改進解決方案
- 1 回答
- 0 關注
- 108 瀏覽
添加回答
舉報