|
| 1 | +## Compiled Interceptors |
| 2 | + |
| 3 | +Proposal based on Community Pull Request https://github.com/magento/magento2/pull/22826 created by [@fsw](https://github.com/fsw) |
| 4 | + |
| 5 | +### Problem Statement |
| 6 | + |
| 7 | +Existing debugging of Magento may be tricky because of Interception implementation via dynamical calls of `___callPlugins` in the call stack. |
| 8 | + |
| 9 | +### Overview |
| 10 | + |
| 11 | +The idea is to use a mechanism which generates Interceptors code using app config instead of reading config at runtime. |
| 12 | +The main benefit of having this approach incorporated to Magento is improving Dev Experience while debugging and profiling Magento codebase. |
| 13 | + |
| 14 | +#### Current Code generated Interceptors |
| 15 | + |
| 16 | +```php |
| 17 | +public function methodX($arg) { |
| 18 | + $pluginInfo = $this->pluginList->getNext($this->subjectType, 'methodX'); |
| 19 | + if (!$pluginInfo) { |
| 20 | + return parent::methodX($arg); |
| 21 | + } else { |
| 22 | + return $this->___callPlugins('methodX', func_get_args(), $pluginInfo); |
| 23 | + } |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +#### Proposed version of Code generated Interceptors |
| 28 | + |
| 29 | +```php |
| 30 | +public function methodX($arg) { |
| 31 | + switch(getCurrentScope()){ |
| 32 | + case 'frontend': |
| 33 | + $this->_get_example_plugin()->beforeMethodX($this, $arg); |
| 34 | + $this->_get_another_plugin()->beforeMethodX($this, $arg); |
| 35 | + $result = $this->_get_around_plugin()->aroundMethodX($this, function($arg){ |
| 36 | + return parent::methodX($arg); |
| 37 | + }); |
| 38 | + return $this->_get_after_plugin()->afterMethodX($this, $result); |
| 39 | + case 'adminhtml': |
| 40 | + // ... |
| 41 | + default: |
| 42 | + return parent::methodX($arg); |
| 43 | + } |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +### Design |
| 48 | + |
| 49 | +The main idea is to compile the Interceptor using information from source code. |
| 50 | +This makes plugins slightly faster (as we no need to resolve all plugins dynamically). |
| 51 | +This is important in places where there is a lot of non-cached PHP logic going on (for example admin panel). |
| 52 | + |
| 53 | +Having plugins called directly also makes code easier to debug. The Interceptors generated by this approach are fully compatible with the ones generated by Magento |
| 54 | +currently, so there is no need to change anything in existing plugins. |
| 55 | + |
| 56 | +The new implementation should be optional, and can be chosen by modifying DI configuration, so that each developer can chose the best option for them. |
| 57 | +For the next minor release of Magento product, collect developers feedback and consider removing one of the implementations. |
| 58 | +This will help reduce maintenance cost and confusion by leaving a single way of working with plugins. |
| 59 | + |
| 60 | +To improve developer experience, a file watcher can be used to regenerate only necessary files when plugins or `di.xml` change. Similar to Node Watch https://www.npmjs.com/package/node-watch |
| 61 | + |
| 62 | + |
| 63 | +#### PROS |
| 64 | + |
| 65 | +1. Easier Debugging process as there is no anymore dynamic method execution via `___callPlugins` (No calls to ___callPlugins in call stack). |
| 66 | +2. Slightly Faster response time for uncached code in production mode |
| 67 | + |
| 68 | +#### CONS |
| 69 | + |
| 70 | +1. Each time after making change in plugins declaration (di.xml) or adding/modyfing the source code of pluings, generated/code/* needs to be purged. |
| 71 | +2. Longer execution and higher memory consumption while the whole code generation phase - https://github.com/magento-engcom/msi/issues/2269#issuecomment-499551378 |
| 72 | +3. Higher latency for the first warmup request and corresponding generation in Dev Mode - https://github.com/magento/magento2/pull/22826#issuecomment-491949914 |
| 73 | + |
| 74 | + |
| 75 | +### Discusssions and Public Involvements |
| 76 | + |
| 77 | +- [YouTube recording of Architectural Discussion](https://www.youtube.com/watch?v=no2jVn_mixk&list=PLDvMskiz68Q3ZVyRsc59IjEqX85LaO8mr&index=16) |
| 78 | +- https://github.com/creatuity/magento2-interceptors |
0 commit comments