From 48c3ee2caecae187afb9d6a70a2ac96a24c1c840 Mon Sep 17 00:00:00 2001 From: Alex Taranovsky Date: Sun, 8 Mar 2020 02:39:19 +0200 Subject: [PATCH 1/2] magento/devdocs#6535: Clarify behavior of plugins in Plugins (Interceptors) topic https://devdocs.magento.com/guides/v2.3/extension-dev-guide/plugins.html --- .../v2.3/extension-dev-guide/plugins.md | 67 +++++++++++++++++-- 1 file changed, 62 insertions(+), 5 deletions(-) diff --git a/src/guides/v2.3/extension-dev-guide/plugins.md b/src/guides/v2.3/extension-dev-guide/plugins.md index 883081761c9..11dd8c6b14f 100644 --- a/src/guides/v2.3/extension-dev-guide/plugins.md +++ b/src/guides/v2.3/extension-dev-guide/plugins.md @@ -332,7 +332,7 @@ The execution will have a different flow, depending on the methods implemented b #### Scenario A -Your plugin classes has this methods: +Your plugin classes have the next methods: | | PluginA | PluginB | PluginC | | ------------ | ---------------- | ---------------- | ---------------- | @@ -353,9 +353,9 @@ The execution will be in this order: * `PluginB::afterDispatch()` * `PluginC::afterDispatch()` -#### Scenario B +#### Scenario B (with a `callable` around) -Your plugin classes has this methods: +Your plugin classes have the next methods: | | PluginA | PluginB | PluginC | | ----------- | -------------- | -------------- | -------------- | @@ -364,11 +364,31 @@ Your plugin classes has this methods: | **around** | | aroundDispatch() | | | **after** | afterDispatch() | afterDispatch() | afterDispatch() | +`PluginB`::`aroundDispatch()` defines the ($next)[{{ site.mage2bloburl }}/{{ page.guide_version }}/lib/internal/Magento/Framework/Interception/Interceptor.php] argument with a `callable` type. For example: + +```php +class PluginB +{ + public function aroundDispatch(\Magento\Framework\App\Action\Action $subject, callable $next, ...$args) + { + // The first half of code goes here + // ... + + $result = $next(...$args); + + // The second half of code goes here + // ... + + return $result; + } +} +``` + The execution will be in this order: * `PluginA::beforeDispatch()` * `PluginB::beforeDispatch()` -* `PluginB::aroundDispatch()` (Magento calls the first half until `callable`) +* `PluginB::aroundDispatch()` (Magento calls the first half before `callable`) * `PluginC::beforeDispatch()` @@ -380,9 +400,46 @@ The execution will be in this order: * `PluginA::afterDispatch()` * `PluginB::afterDispatch()` +#### Scenario B (without a `callable` around) + +Your plugin classes have the next methods: + +| | PluginA | PluginB | PluginC | +| ----------- | -------------- | -------------- | -------------- | +| **sortOrder** | 10 | 20 | 30 | +| **before** | beforeDispatch() | beforeDispatch() | beforeDispatch() | +| **around** | | aroundDispatch() | | +| **after** | afterDispatch() | afterDispatch() | afterDispatch() | + +`PluginB`::`aroundDispatch()` does not define the ($next)[{{ site.mage2bloburl }}/{{ page.guide_version }}/lib/internal/Magento/Framework/Interception/Interceptor.php] argument with a `callable` type. For example: + +```php +class PluginB +{ + public function aroundDispatch(\Magento\Framework\App\Action\Action $subject, $next, $result) + { + // My custom code + + return $result; + } +} +``` + +The execution will be in this order: + +* `PluginA::beforeDispatch()` +* `PluginB::beforeDispatch()` + + * `PluginB::aroundDispatch()` + +* `PluginA::afterDispatch()` +* `PluginB::afterDispatch()` + +Because of absent a `callable` type for the `$next` agrument the plaginized `Action::dispatch()` will be not called and `Plugin C` will be not triggered. + #### Scenario C -Your plugin classes has this methods: +Your plugin classes have the next methods: | | PluginA | PluginB | PluginC | | ------------- | ---------------- | ---------------- | ---------------- | From 93305086bda46a592152d8aa4587d6eef4686e97 Mon Sep 17 00:00:00 2001 From: Donald Booth Date: Thu, 12 Mar 2020 12:43:26 -0500 Subject: [PATCH 2/2] Grammar fixes --- src/guides/v2.3/extension-dev-guide/plugins.md | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/guides/v2.3/extension-dev-guide/plugins.md b/src/guides/v2.3/extension-dev-guide/plugins.md index 11dd8c6b14f..a72e3335134 100644 --- a/src/guides/v2.3/extension-dev-guide/plugins.md +++ b/src/guides/v2.3/extension-dev-guide/plugins.md @@ -332,7 +332,7 @@ The execution will have a different flow, depending on the methods implemented b #### Scenario A -Your plugin classes have the next methods: +With these methods: | | PluginA | PluginB | PluginC | | ------------ | ---------------- | ---------------- | ---------------- | @@ -355,7 +355,7 @@ The execution will be in this order: #### Scenario B (with a `callable` around) -Your plugin classes have the next methods: +With these methods: | | PluginA | PluginB | PluginC | | ----------- | -------------- | -------------- | -------------- | @@ -402,7 +402,7 @@ The execution will be in this order: #### Scenario B (without a `callable` around) -Your plugin classes have the next methods: +Using these methods: | | PluginA | PluginB | PluginC | | ----------- | -------------- | -------------- | -------------- | @@ -419,7 +419,6 @@ class PluginB public function aroundDispatch(\Magento\Framework\App\Action\Action $subject, $next, $result) { // My custom code - return $result; } } @@ -435,11 +434,11 @@ The execution will be in this order: * `PluginA::afterDispatch()` * `PluginB::afterDispatch()` -Because of absent a `callable` type for the `$next` agrument the plaginized `Action::dispatch()` will be not called and `Plugin C` will be not triggered. +Because the`callable` type for the `$next` agrument is absent, `Action::dispatch()` will be not called and `Plugin C` will be not triggered. #### Scenario C -Your plugin classes have the next methods: +Assuming these methods: | | PluginA | PluginB | PluginC | | ------------- | ---------------- | ---------------- | ---------------- | @@ -468,7 +467,7 @@ The execution will be in this order: ## Configuration inheritance -Classes and interfaces that are implementations of or inherit from classes that have plugins will also inherit plugins from the parent class. +Classes and interfaces that are implementations of, or inherit from, classes that have plugins will also inherit plugins from the parent class. Magento uses plugins defined in the global scope when the system is in a specific area (such as frontend or backend). You can extend or override these global plugin configurations with an area's `di.xml` file.