tmont/phroxy

支持拦截方法调用的代理生成器

1.1.0 2014-01-22 22:09 UTC

This package is not auto-updated.

Last update: 2024-09-24 06:49:08 UTC


README

Build Status

Phroxy 是一个代理生成器。它可能很有用。您可以使用它来创建一个模拟对象,或者创建一个代理并在该代理上拦截方法。

安装

用户 composer

{
  "require": {
    "tmont/phroxy": "1.1.x"
  }
}

用法

我不想写文档,所以请查看单元测试。其中包含基本代理和高级方法拦截的示例。

基本思路是

use Tmont\Phroxy\Interceptor;
use Tmont\Phroxy\InterceptorCache;
use Tmont\Phroxy\InterceptionContext;
use ReflectionClass;

class ReturnBeforeCallInterceptor implements Interceptor {
	public function onBeforeMethodCall(InterceptionContext $context) {
		$context->setReturnValue('oh hai!');
	}

	public function onAfterMethodCall(InterceptionContext $context) {}
}

class MyClass {
	public function hello() {
		return 'hello';
	}
}

$interceptor = new ReturnBeforeCallInterceptor();
InterceptorCache::registerInterceptor($interceptor, function($x) { return true; });

$proxy = $this->builder->build(new ReflectionClass('MyClass'));
$proxy->hello(); // "oh hai!"

开发

git clone git@github.com:tmont/phroxy.git
cd phroxy
composer install
vendor/bin/phpunit