antecedent/patchwork 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

antecedent/patchwork

Composer 安装命令:

composer require --dev antecedent/patchwork

包简介

Method redefinition (monkey-patching) functionality for PHP.

README 文档

README

Patchwork implements the redefinition (monkey-patching) of functions and methods in PHP. This includes both user-defined and internal callables, which can be functions, class methods, or instance methods. In addition, many function-like constructs, such as exit or include, are supported in an analogous way.

Internally, Patchwork uses a stream wrapper on file://. In the case of user-defined functions and methods, it is used to inject a simple interceptor snippet to the beginning of every such callable. For the remaining types of callables, various other strategies are applied.

Example: a DIY profiler

use function Patchwork\{redefine, relay, getMethod};

$profiling = fopen('profiling.csv', 'w');

redefine('App\*', function(...$args) use ($profiling) {
    $begin = microtime(true);
    relay(); # calls the original definition
    $end = microtime(true);
    fputcsv($profiling, [getMethod(), $end - $begin]);
});

Notes

  • Method redefinition is the internally preferred metaphor for Patchwork's behavior.
  • restoreAll() and restore($handle) end the lifetime of, respectively, all redefinitions, or only one of them, where $handle = redefine(...).
  • Closure $this is automatically re-bound to the enclosing class of the method being redefined.
  • The behavior of __CLASS__, static::class etc. inside redefinitions disregards the metaphor. getClass(), getCalledClass(), getMethod() and getFunction() from the Patchwork namespace should be used instead.

Testing-related uses

Patchwork can be used to stub static methods, which, however, is a controversial practice.

It should be applied prudently, that is, only after making oneself familiar with its pitfalls and temptations in other programming languages. For instance, in Javascript, Ruby, Python and some others, the native support for monkey-patching has made its testing-related uses more commonplace than in PHP.

Tests that use monkey-patching are often no longer unit tests, because they become sensitive to details of implementation, not only those of interface: for example, such a test might no longer pass after switching from time() to DateTime.

That being said, they still have their place where the only economically viable alternative is having no tests at all.

Other use cases

Patchwork is not suggested for AOP and other kinds of production usage. Its impact on the application's performance is highly likely to be prohibitively large. Additionally, while no particular Patchwork-related security risks are either known or anticipated, please keep in mind that Patchwork was never developed with production environments in mind.

antecedent/patchwork 适用场景与选型建议

antecedent/patchwork 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20.6M 次下载、GitHub Stars 达 451, 最近一次更新时间为 2013 年 08 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「aop」 「testing」 「aspect」 「runkit」 「redefinition」 「monkeypatching」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 antecedent/patchwork 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 20.6M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 454
  • 点击次数: 40
  • 依赖项目数: 39
  • 推荐数: 0

GitHub 信息

  • Stars: 451
  • Watchers: 12
  • Forks: 42
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-08-03