プラグインには依存噴射が可能ですか?
-
13-12-2019 - |
質問
\ Magento \ Salesrule \ Model \ Validator
を拡張するためにプラグインを作成しようとしています。魅力のように機能しますが、問題はMagento \ Salesrule \ Model \ RuleFactory
を利用する必要があります。プラグインクラスで__constructorを作成し、このように見えたHET Magento 2開発者ガイドに記載されているように依存インジェクションを介してそこにルールファクトリーを追加しました。
「バリデータ」プラグインクラス全体がこのようになるようになりました:
class Validator{
/**
* @var \Magento\SalesRule\Model\RuleFactory
*/
private $ruleFactory;
/**
* @param \Magento\SalesRule\Model\RuleFactory $ruleFactory
*/
public function __constructor(\Magento\SalesRule\Model\RuleFactory $ruleFactory){
$this->ruleFactory = $ruleFactory;
error_log('validator constructed');
}
public function aroundInit(\Magento\SalesRule\Model\Validator $subject, $procede, $websiteId, $customerGroupId, $couponCode){
//custom before code
$return = $proceed($websiteId, $customerGroupId, $couponCode);
//custom after code
return $return;
}
}
.
しかし、これは常に$ this-> allefactoryのためにNULLを返すだけです。
di.xml文書には、次のものが含まれています。
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<type name="\Magento\SalesRule\Model\Validator">
<plugin name="ValidatorPlugin" type="\MAS\CouponAttempt\Model\SalesRule\Validator" sortOrder="1" disabled="false"/>
</type>
<type name="\MAS\CouponAttempt\Model\SalesRule\Validator">
<arguments>
<argument name="ruleFactoryInjection" xsi:type="object">Magento\SalesRule\Model\RuleFactory</argument>
</arguments>
</type>
</config>
.
興味深いことに、生成されたinterceptor.phpファイルには、コンストラクタが実際に行われています。しかし、それは呼ばれていない、誰にもこれがなぜなのか私に言うことができますか?
解決 3
私は、より少ない解決策で行くことを決定し、ObjectManagerの静的インスタンスを使用し、その
を通してルールファクトリを取得しました。他のヒント
遅い回答ですが、__constructor
を__construct
に変更する必要があり、それは完全にうまく機能します。可能であれば静的ObjectManagerを使用しないでください。代わりに常にdiを使うべきです。
はい、サポートされています。
サンプルfunction
キーワードが見つからないようになります。
public function __constructor(\Magento\SalesRule\Model\RuleFactory $ruleFactory){
$this->ruleFactory = $ruleFactory;
}
. 所属していません magento.stackexchange