<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ### 個人理解 類似在方法這個層次的中間件,允許在被代理的方法前、后做一些其他事情。主要有 before、after 和 around 方法。可以對比 laravel 或者其他框架中的 middleware 來理解,或者類比設計模式中的裝飾器模式,或者 python 中的 decorator,只不過是在 method 這個層次。 ### 適用場景 plugin 不能被用在以下類型中 > - Final method > - Final class > - 非 public 方法 > - 類方法(比如靜態方法) > - __construct() > - Virtual Types > - 在 Framework\Interception 啟動之前初始化的對象 Plugin 可以被用在下面幾個情況中 > - class > - interface > - 抽象類 > - 父類 通過在 Magento 源碼中搜索 &lt;plugin?就能在 di.xml 文件中搜索到很多 plugin 的例子。 ### before plugin before plugin 會在被監聽的方法之前運行。before plugin 有以下幾條規則 > - before 關鍵詞會被添加到被監聽的方法前面,比如如果監聽的是 getSomeValue 方法,那么在 plugin 中對應的方法名稱就是 beforeGetSomeValue (下稱為 before plugin method) > - before plugin method 中的第一個參數是被監聽的對象實例,通常縮寫為 $subject?或者直接使用對應的類名,在例子中是 $processor? > - before plugin method 中的剩余所有參數都必須和被監聽的方法中的參數一致。 > - before plugin method 返回的參數必須是一個數組,返回值類型和個數必須和被監聽的方法一致。 在<MAGENTO_DIR>module-payment/etc/frontend/di.xml 我們能看到類似下面的寫法 ``` <type name="Magento\Checkout\Block\Checkout\LayoutProcessor"> <plugin name="ProcessPaymentConfiguration" type="Magento\Payment\Plugin\PaymentConfigurationProcess"/> </type> ``` 上面代碼中 plugin 的 beforeProcess?方法監聽的?Magento\Checkout\Block\Checkout\LayoutProcessorMagento\Checkout\Block\Checkout\LayoutProcessor 中的 Magento\Checkout\Block\Checkout\LayoutProcessor?process?方法。 ```php public function process($jsLayout) { //代碼塊 return $jsLayout; } ``` before plugin 的實現是通過 Magento\Payment\Plugin\PaymentConfigurationProcess 類中的 beforeProcess?方法來完成的。 ```php public function beforeProcess( \Magento\Checkout\Block\Checkout\LayoutProcessor $processor, $jsLayout) { // 代碼塊... return [$jsLayout]; } ``` ### around plugin around plugin 功能允許我們在被監聽的方法前、后運行一部分我們自己的代碼。這個功能使我們能夠在改變輸入參數的同時改變返回結果值。 關于 around plugin, 要記住的幾個要點有 > - plugin 中的第一個參數是監聽的類的實例 > - plugin 中的第二個參數是一個 callable/Closure 類型。通常寫作 callable $proceed?,調用 $proceed 時的入參需要和被監聽方法參一致。 > - 其余的參數需要和被監聽方法一致。 > - plugin 的返回值必須和原函數保持一致。通常是直接返回 return $proceed(…) 或者先調用 $returnValue = $proceed(); 后直接返回 $returnValue; 有時候我們也需要修改 $returnValue; 下面來看一個 around plugin 的例子。 <MAGENTO_DIR>module-grouped-product/etc/di.xml 文件中 ``` <type name="Magento\Catalog\Model\ResourceModel\Product\Link"> <plugin name="groupedProductLinkProcessor" type="Magento\GroupedProduct\Model\ResourceModel\Product\Link\RelationPersister" /> </type> ``` plugin 中的方法監聽的是 Magento\GroupedProduct\Model\ResourceModel\Product\Link\RelationPersister 類中的 aroundDeleteProductLink 方法 ```php public function aroundDeleteProductLink( \Magento\GroupedProduct\Model\ResourceModel\Product\Link $subject, \Closure $proceed, $linkId) { // The rest of the code... $result = $proceed($linkId); // The rest of the code... return $result; } ``` ### after plugin after plugin 主要是在被監聽的方法之后執行一部分代碼。 在寫 after plugin 的時候,要記住以下幾點: > - plugin 的第一個參數是被監聽類型的實例 > - plugin 的第二個參數是被監聽方法的執行結果,通常叫做 $result?,也可以在被監聽方法返回值之后被調用。例如下面例子中的 $data? > - 剩下的其他參數和被監聽方法一致 > - plugin 必須返回和?$result|$data?同類型的返回值 在 module-catalog/etc/di.xml 中的 after plugin 的例子如下: ``` <type name="Magento\Indexer\Model\Config\Data"> <plugin name="indexerProductFlatConfigGet" type="Magento\Catalog\Model\Indexer\Product\Flat\Plugin\IndexerConfigData" /> </type> ``` plugin 中監聽的方法是 Magento\Indexer\Model\Config\Data 類中的 get?方法 ```php public function get($path = null, $default = null) { // The rest of the code... return $data; } ``` Magento\Catalog\Model\Indexer\Product\Flat\Plugin\IndexerConfigData 類中的 afterGet?就是這里的 after plugin 的實現,具體如下: ```php public function afterGet(Magento\Indexer\Model\Config\Data, $data, $path = null, $default = null) { // The rest of the code... return $data; } ``` 使用 plugin 時需要特別注意, 它十分靈活,但是也很容易產生 Bug 和造成性能瓶頸,尤其是在多個 plugin 監聽同一個方法的時候。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看