Implementation of a class of AOP in PHP
1. [Code]Core
Teardown --> open --> setup --> packing. ** for example, if you call source-> doXX (), each process is: * unpacking: unpacking. this is the method that will be called to call any source function; * teardown: tear off the ornaments. for each Decoration in Wrapper, call its before () function; * open: actually calls the source-> doXX () function; * setup: Re-attaches the ornaments. for each Decoration in Wrapper, call its after () function; * packing: repackage. this is a method that can be called by any source function. **/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); // openingif (method_exists ($ this-> source, $ name) {$ retval = call_user_func_array (array ($ this-> source, $ name), $ parameters) ;}$ this-> setup ($ retval, $ name, $ parameters); $ this-> packing ($ retval, $ name, $ parameters ); Return $ retval;} public function unpacking ($ name, $ parameters) {} public function packing ($ name, $ parameters) {} public function tearDown ($ name, $ parameters) {if ($ this-> undecorated) {return;} foreach ($ this-> decorations as $ d) {$ d-> before ($ name, $ parameters );}} public function setup ($ retval, $ name, $ parameters) {if ($ this-> undecorated) {return;} foreach ($ this-> decorations as $ d) {$ d-> after ($ retval, $ name, $ parameters) ;}} Public function decarate ($ decoration) {$ this-> decorations [] = $ decoration;} public static function wrap ($ source) {// wrap the source $ wrapperConfig = app ()-> wrappers [get_class ($ source)]; if ($ wrapperConfig) {$ wrapperClass = $ wrapperConfig ['class']; $ wrapper = new $ wrapperClass ($ source); foreach ($ wrapperConfig ['records'] as $ item) {$ decoration = new $ item; $ wrapper-> decarate ($ decoration) ;}} return $ wra Pper? $ Wrapper: $ source ;}}?>
2. [Code]Configuration
'wrappers'=>array('ContentService'=>array('class'=>'ContentWrapper','decorations'=>array('DasaiContentDecoration',)),'AOPWorker'=>array(//for test'class'=>'DiagnosisWrapper','decorations'=>array('DasaiDiagnosisDecoration'),),),
3. [Code]Test Code
Class AOPWorker {public function testAOP () {Debugger: print_r ("\ n worker: I have done a lot of operations \ n worker :...... \ n worker: finished \ n "); return 'OK' ;}} public function testAOP () {// test aop test entry $ aop = Wrapper :: wrap (new AOPWorker (); $ aop-> testAOP (33347);} class DiagnosisWrapper extends Wrapper {public function unpacking ($ name, $ parameters) {echo "\ nDiagnosisWrapper: hello, someone called $ name. I want to unpack it. \ n ";} public function packing ($ retval, $ name, $ parameters) {echo" \ nDiagnosisWrapper: Hello, call $ name and the result is $ retval. the package is repackaged. \ n ";}} class DasaiDiagnosisDecoration extends Decoration {public function before ($ name, $ parameters) {echo" \ r \ nDasaiDiagnosisDecoration: start to call $ name. Michael has already told James. \ n ";} public function after ($ retval, $ name, $ parameters) {echo" \ nDasaiDiagnosisDecoration: ends the call of $ name and tells Hawking and Sheldon. \ n ";}}