根据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(); ?>
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章
刺客信條陰影:貝殼謎語解決方案
3 週前ByDDD
Windows 11 KB5054979中的新功能以及如何解決更新問題
2 週前ByDDD
在哪裡可以找到原子中的起重機控制鑰匙卡
3 週前ByDDD
<🎜>:死鐵路 - 如何完成所有挑戰
4 週前ByDDD
Atomfall指南:項目位置,任務指南和技巧
1 個月前ByDDD

熱工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3漢化版
中文版,非常好用

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。