定制 creatuity/magento2-interceptors 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

creatuity/magento2-interceptors

Composer 安装命令:

composer require creatuity/magento2-interceptors

包简介

Creatuity Compiled Interceptors Module

README 文档

README

ABOUT

This component changes the way Magento 2 generates Interceptor classes (a mechanism that allows plugins to work together).

Instead of generating boilerplate code it compiles the Interceptor using information from source code. This makes plugins slightly faster and as Magento uses a lot of plugins, even at its core, it lowers request time by ~10%. This is important in places where there is a lot of non-cached PHP logic going on (for example admin panel is noticeably faster).

The default method uses code that is called on nearly each method to see if there are any plugins connected, in generated code this is not required and call-stack is reduced.

Having plugins called directly also makes code easier to debug and bugs easier to find.

The Interceptors generated by this plugin are 100% compatible with the ones generated by Magento by default, so there is no need to change anything in your plugins.

VERSIONS

As Magento 2.4 introduced changes in Interceptors generator which are not backward compatible this module has separate branch for both Magento versions:

  • use 1.3.* for Magento 2.4 (branch: master)
  • use 1.2.* for Magento 2.3 (branch: magento23)

CONFIGURATION

This module adds following preference to work in developer mode:

<preference for="Magento\Framework\Interception\Code\Generator\Interceptor" type="Creatuity\Interception\Generator\CompiledInterceptor" />

And following preferences to work in production or default modes:

<preference for="Magento\Setup\Module\Di\Code\Generator\Interceptor" type="Creatuity\Interception\Generator\CompiledInterceptor" />
<preference for="Magento\Setup\Module\Di\Compiler\Config\Chain\InterceptorSubstitution" type="Creatuity\Interception\Generator\CompiledInterceptorSubstitution" />

Those are enabled by default in module di.xml.

However this module does not touch the default generation method so you can override above preferences to fallback to the default mechanism for all or selected modes.

TECHNICAL DETAILS

Instead of interceptors that read plugins config at runtime like this:

public function methodX($arg) {
    $pluginInfo = $this->pluginList->getNext($this->subjectType, 'methodX');
    if (!$pluginInfo) {
        return parent::methodX($arg);
    } else {
        return $this->___callPlugins('methodX', func_get_args(), $pluginInfo);
    }
}

This generator generates static interceptors like this:

public function methodX($arg) {
    switch(getCurrentScope()){
        case 'frontend':
            $this->_get_example_plugin()->beforeMethodX($this, $arg);
            $this->_get_another_plugin()->beforeMethodX($this, $arg);
            $result = $this->_get_around_plugin()->aroundMethodX($this, function($arg){
                return parent::methodX($arg);
            });
            return $this->_get_after_plugin()->afterMethodX($this, $result);
        case 'adminhtml':
            // ...
        default:
            return parent::methodX($arg);
    }
}

PROS

  • Easier debugging.

    • If you ever stumbled upon ___callPlugins when debugging you should know how painful it is to debug issues inside plugins.
    • Generated code is decorated with PHPDoc for easier debugging in IDE
  • Fastest response time (5%-15% faster in developer and production mode)

    • No redundant calls to ___callPlugins in call stack.
    • Methods with no plugins are not overridden in parent at all.
  • Implemented as a module and can be easily reverted to the default Generator\Interceptor

CONS

  • Each time after making change in etc plugins config, generated/code/* and var/cache/* needs to be purged
  • As this does not load plugins at runtime, might not work in an edge case of plugging into core Magento classes like PluginsList etc.

creatuity/magento2-interceptors 适用场景与选型建议

creatuity/magento2-interceptors 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 149.05k 次下载、GitHub Stars 达 137, 最近一次更新时间为 2022 年 04 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 creatuity/magento2-interceptors 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 creatuity/magento2-interceptors 我们能提供哪些服务?
定制开发 / 二次开发

基于 creatuity/magento2-interceptors 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 149.05k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 137
  • 点击次数: 18
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 137
  • Watchers: 21
  • Forks: 20
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-04-13