icewind / patcher

替换内置的PHP函数

v0.4.0 2015-10-24 12:54 UTC

This package is auto-updated.

Last update: 2024-09-18 18:17:15 UTC


README

Build Status Code Coverage Scrutinizer Code Quality

替换内置的PHP函数

composer require icewind/patcher

用法

覆盖方法

use Icewind\Patcher\Patcher;

$patcher = new Patcher();
$patcher->patchMethod('time', function () {
    return 100;
});
$patcher->whiteListDirectory(__DIR__ . '/src');
$patcher->autoPatch();

include 'src/....';

使用原始方法

use Icewind\Patcher\Patcher;

$patcher = new Patcher();
$patcher->patchMethod('time', function ($method, $arguments, $original) {
    $time = $original();
    error_log("Time: $time");
    return $time;
});
$patcher->whiteListDirectory(__DIR__ . '/src');
$patcher->autoPatch();

include 'src/....';

覆盖类

use Icewind\Patcher\Patcher;

class DummyDateTime extends DateTime {
	public function __construct() {
		parent::__construct("1970-01-01 00:00:00 UTC");
	}
}

$patcher = new Patcher();
$patcher->patchClass('\DateTime', '\DummyDateTime');
$patcher->whiteListDirectory(__DIR__ . '/src');
$patcher->autoPatch();

include 'src/....';

API

  • patchMethod(string $method, callable $handler): 为方法设置处理器
  • 处理器将使用以下三个参数被调用
  • string $method 被调用的方法名称
  • array $arguments 方法被调用时的参数
  • callable $original 一个闭包,它将使用正确的参数调用被覆盖的方法并返回结果
  • patchClass(string $method, string $replacement): 使用不同的类覆盖类
    • 注意,目前这仅适用于全局命名空间中的类
  • whiteListDirectory(string $path): 将目录添加到自动补丁的白名单中
  • blackListDirectory(string $path): 将目录添加到自动补丁的黑名单中
  • autoPatch(): 从此点开始启用自动补丁
  • 自动应用任何已定义命名空间中的补丁方法
  • 仅应用于白名单目录内的文件