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()andrestore($handle)end the lifetime of, respectively, all redefinitions, or only one of them, where$handle = redefine(...).- Closure
$thisis automatically re-bound to the enclosing class of the method being redefined. - The behavior of
__CLASS__,static::classetc. inside redefinitions disregards the metaphor.getClass(),getCalledClass(),getMethod()andgetFunction()from thePatchworknamespace 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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 antecedent/patchwork 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Framework for aspect-oriented programming in PHP.
Testing Suite For Lumen like Laravel does.
Ray.Di for Laravel
Method filtering system
Common aspect ratios for the TYPO3 CMS system package typo3/cms-seo
Bootstraps errors and handles them via reporters and renderers
统计信息
- 总下载量: 20.6M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 454
- 点击次数: 40
- 依赖项目数: 39
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2013-08-03