Home >php教程 >php手册 >PHP一个类AOP的实现[转及修改]

PHP一个类AOP的实现[转及修改]

WBOY
WBOYOriginal
2016-06-06 19:32:131200browse

根据OSC上的@hoohle的代码修改的。 地址:http://www.oschina.net/code/snippet_186535_24755 发现代码在部分地方没讲清楚,自己完善了一些 无 ?php/** * 包装器(Wrapper). * Wrapper是一个AOP_LIKE的实现. 也可以看作监听者模式的实现. * 一个Wrapper报装了

根据OSC上的@hoohle  的代码修改的。
地址:http://www.oschina.net/code/snippet_186535_24755
发现代码在部分地方没讲清楚,自己完善了一些
<?php

/**
 * 包装器(Wrapper).
 * Wrapper是一个AOP_LIKE的实现. 也可以看作监听者模式的实现.
 * 一个Wrapper报装了一个对象(source). source可以是任意对象(不包括数组及原子类型),甚至是一个Wrapper.
 *
 * 包装器可以任意添加饰品(Decoration).通过Wrapper调用source的函数的流程将是:
 *	unpacking --> teardown --> open --> setup --> packing.
 *
 *	例如调用source->doXX(),各个流程将是:
 *	unpacking: 	解包. 这是调用任意source的函数都会调用的方法;
 *	teardown: 	撕掉饰品. 对于Wrapper中的每个Decoration,调用其before()函数;
 *	open: 		真正调用source->doXX()函数;
 *	setup: 		重新贴上饰品. 对于Wrapper中的每个Decoration,调用其after()函数;
 *	packing:	重新打包.  这是调用任意source的函数都会调用的方法;
 *
 */
class Wrapper{
	private $source;

	/**
	 * @var bool
	 */
	private $undecorated;

	/**
	 * @var array[Decoration]
	 */
	private $decorations=array();

	public function __construct($source){
		$this->source = $source;
	}

	public function __call($name,$parameters){
		$this->unpacking($name,$parameters);
		$this->tearDown($name,$parameters);

		//opening
		$retval = $this->opening($name,$parameters);

		$this->setup($retval,$name,$parameters);
		$this->packing($retval,$name,$parameters);

		return $retval;
	}

	public function unpacking($name,$parameters){
	}

	public function packing($name,$parameters){
	}
	
	private function opening($name,$parameters){
		// opening
		if(method_exists($this->source, $name)){
			$retval = call_user_func_array(array($this->source,$name),$parameters);
		}
		
		return $retval;
	}

	public function tearDown($name,$parameters){
		if($this->undecorated){
			return;
		}
		$before='before';
		foreach ($this->decorations as $d){
			if(method_exists($d, $before))
			{ 
				$d->$before($name,$parameters);
			} 
		}
	}

	public function setup($retval,$name,$parameters){
		if($this->undecorated){
			return ;
		}
		$after='after';
		foreach ($this->decorations as $d){
			if(method_exists($d, $after))
			{ 
				$d->$after($retval,$name,$parameters);
			} 
		}
	}

	public function decarate($decoration){
		$this->decorations[] = $decoration;
	}



	public static function wrap($source){
		//  wrap the source
		$wrapperConfig = Aop::app()->wrappers[get_class($source)];
		if($wrapperConfig){
			$wrapperClass = $wrapperConfig['class'];
			$wrapper = new $wrapperClass($source);

			foreach ($wrapperConfig['decorations'] as $item){
				if(class_exists($item)){
					$decoration = new $item;
					$wrapper->decarate($decoration);
				}
			}
		}
		return $wrapper?$wrapper:$source;
	}

}

?>
<title>AOP测试</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<?php

require 'Wrapper.class.php';

//该类和Wrapper类联合使用,用来获取配置文件
class Aop{
	var wrappers=array();
	
	function __construct(){
	    //配置文件
		$this->wrappers=array(
				'ContentService'=>array(
						'class'=>'ContentWrapper',
						'decorations'=>array(
								'DasaiContentDecoration',
						)
				),
				'AOPWorker'=>array(//for test
					'class'=>'DiagnosisWrapper',
					'decorations'=>array(
						'DasaiDiagnosisDecoration'
					),
				),
		);
		
	}
	
	public static function app(){
		return new Aop();
	
	}
}



class AOPWorker{
	public function testAOP(){
		echo '<br>这里是调用的地方 <br>';
		return 'OK';
	}

}


class DiagnosisWrapper extends Wrapper{

	public function unpacking($name, $parameters){
		echo "DiagnosisWrapper:喂,有人调用$name,我要解包了.<br>";
	}


	public function packing($retval,$name, $parameters){
		echo "DiagnosisWrapper:喂,调用$name,结果为$retval,重新打包好了.<br>";
	}
}



class DasaiDiagnosisDecoration  {
	public function before($name,$parameters){
		echo "DasaiDiagnosisDecoration:开始调用$name , 信息 :$parameters[0] 已经告诉张三李四了.<br>";
	}

	public function after($retval,$name,$parameters){
		echo "<br>DasaiDiagnosisDecoration:结束调用$name,告诉霍金和Sheldon了.<br>";
	}
}


function testAOP(){// test aop  测试入口
		$aop = Wrapper::wrap(new AOPWorker());
		$aop->testAOP(33347);
}


testAOP();






?>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn