CodeInstrumentationMethod.class.php
Go to the documentation of this file.00001 <?php
00014 class CodeInstrumentationMethod extends CodeReflectionMethod
00015 {
00020 const PREFIX_METHOD = "CODETODIAGRAM_";
00021
00027 public function getNewName()
00028 {
00029 return self::PREFIX_METHOD . $this->getName();
00030 }
00031
00037 public function getCallCodeInstrFunctionName()
00038 {
00039 return $this->getName();
00040 }
00041
00047 protected function getCallCodeInstrFunctionCode()
00048 {
00049 $strCode = "";
00050 $strCode .= $this->getCallCodeInstrFunctionHeaderCode();
00051 $strCode .= "{" . "\n";
00052 $strCode .= $this->getCallCodeInstrFunctionContentCode();
00053 $strCode .= "}" . "\n";
00054 return $strCode;
00055 }
00056
00063 protected function getCallCodeInstrFunctionHeaderCode()
00064 {
00065 $strCode = CorujaStringManipulation::retab( $this->getDocComment() , 1 );
00066 $strCode .= $this->createModifiersCode();
00067 $strCode .= " function " . $this->getCallCodeInstrFunctionName();
00068 $strCode .= $this->createParametersCode();
00069 return $strCode;
00070 }
00071
00091 protected function getCallCodeInstrFunctionContentCode()
00092 {
00093
00094 $strCode = "";
00095 $strCode .= ' $strMethod = "' . $this->getNewName() . '"; ' . "\n";
00096 $strCode .= ' $arrArguments = func_get_args(); ' . "\n";
00097 $strCode .= ' // prepare caller // ' . "\n";
00098
00099
00100 if( $this->isStatic() )
00101 {
00102
00103 $strCode .= ' $arrCaller = Array( __CLASS__ , $strMethod ) ; ' . "\n";
00104 $strCode .= ' // log the enter into the real method // ' . "\n";
00105 $strCode .= ' CodeInstrumentationReceiver::getInstance()->onEnterMethod( "static" , __CLASS__ , __METHOD__ , $arrArguments );' . "\n";
00106
00107
00108 $strCode .= ' // execute the real method ' . "\n";
00109 $strCode .= ' $mixReturn = call_user_func_array( $arrCaller, $arrArguments ); ' . "\n";
00110
00111
00112 $strCode .= ' // log the exit of the real method // ' . "\n";
00113 $strCode .= ' CodeInstrumentationReceiver::getInstance()->onLeaveMethod( "static" , __CLASS__ , __METHOD__ , $mixReturn );' . "\n";
00114 $strCode .= ' // return the result of the method into the object // ' . "\n";
00115 $strCode .= ' return $mixReturn; ' . "\n";
00116 }
00117
00118 else
00119 {
00120
00121 $strCode .= ' $arrCaller = Array( $this , $strMethod ) ; ' . "\n";
00122 $strCode .= ' // log the enter into the real method // ' . "\n";
00123 $strCode .= ' CodeInstrumentationReceiver::getInstance()->onEnterMethod( spl_object_hash($this) , __CLASS__ , __METHOD__ , $arrArguments );' . "\n";
00124 $strCode .= ' // execute the real method ' . "\n";
00125
00126
00127 if( $this->getName() == "__call" )
00128 {
00129
00130 $strCode .= ' // if the method exists ' . "\n";
00131 $strCode .= ' if( method_exists( $this , $strMethod ) ) ' . "\n";
00132 $strCode .= ' { ' . "\n";
00133 $strCode .= ' // call the real method ' . "\n";
00134 $strCode .= ' $mixReturn = call_user_func_array( $arrCaller, $arrArguments ); ' . "\n";
00135 $strCode .= ' } ' . "\n";
00136 $strCode .= ' else ' . "\n";
00137 $strCode .= ' { ' . "\n";
00138 $strCode .= ' // exists in the original class a __call method // ' . "\n";
00139 $strCode .= ' if( method_exists( $this , "' . self::PREFIX_METHOD . '__call" ) ) ' . "\n";
00140 $strCode .= ' { ' . "\n";
00141 $strCode .= ' // call it // ' . "\n";
00142 $strCode .= ' $mixReturn = $this->' . self::PREFIX_METHOD . '__call( $strMethod , $arrArguments ); ' . "\n";
00143 $strCode .= ' } ' . "\n";
00144 $strCode .= ' else ' . "\n";
00145 $strCode .= ' { ' . "\n";
00146 $strCode .= ' throw new CodeToDiagramException( "Unable to find the method $strMethod "); ' . "\n";
00147 $strCode .= ' } ' . "\n";
00148 $strCode .= ' } ' . "\n";
00149 }
00150
00151 else
00152 {
00153
00154 $strCode .= ' // if the method exists ' . "\n";
00155 $strCode .= ' if( method_exists( $this , $strMethod ) ) ' . "\n";
00156 $strCode .= ' { ' . "\n";
00157 $strCode .= ' // call the real method ' . "\n";
00158 $strCode .= ' $mixReturn = call_user_func_array( $arrCaller, $arrArguments ); ' . "\n";
00159 $strCode .= ' } ' . "\n";
00160 $strCode .= ' else ' . "\n";
00161 $strCode .= ' { ' . "\n";
00162 $strCode .= ' throw new CodeToDiagramException( "Unable to find the method $strMethod "); ' . "\n";
00163 $strCode .= ' } ' . "\n";
00164 }
00165
00166 $strCode .= ' // log the exit of the real method // ' . "\n";
00167 $strCode .= ' CodeInstrumentationReceiver::getInstance()->onLeaveMethod( spl_object_hash($this) , __CLASS__ , __METHOD__ , $mixReturn );' . "\n";
00168 $strCode .= ' // return the result of the method into the object // ' . "\n";
00169 $strCode .= ' return $mixReturn; ' . "\n";
00170 }
00171
00172 return $strCode;
00173 }
00174
00180 public function getCode()
00181 {
00182 $strCode = "";
00183 $strCode .= $this->getCallCodeInstrFunctionCode();
00184 $strCode .= parent::getCode();
00185 return $strCode;
00186
00187 }
00188
00194 public function createMethodHeaderCode()
00195 {
00196 $strCode = $this->getDocComment();
00197 $strCode .= $this->createModifiersCode();
00198 $strCode .= " function ";
00199 $strCode .= $this->getNewName();
00200 $strCode .= $this->createParametersCode();
00201 return CorujaStringManipulation::retab( $strCode , 1 );
00202 }
00203
00211 protected function createExtendedReflectionClass( ReflectionClass $objOriginalReflectionClass )
00212 {
00213 return new CodeInstrumentationClass( $objOriginalReflectionClass->getName() );
00214 }
00215
00223 protected function createExtendedReflectionParameter( ReflectionParameter $objReflectionParameter )
00224 {
00225 return new CodeInstrumentationParameter( Array( $this->getDeclaringClass()->getName() , $this->getName() ) , $objReflectionParameter->getName() );
00226 }
00227
00233 protected function createMethodContentCode()
00234 {
00235 $strCode = "";
00236
00237 $objCodeInstrFile = CodeReflectionFile::getCodeInstrFileName( $this->getFileName() );
00238 $strCode .= $objCodeInstrFile->getFileBit( $this->getStartLine() , $this->getEndLine() );
00239 $strCode = trim( $strCode );
00240
00241 if( strlen( $strCode ) == 0 )
00242 {
00243 return $strCode;
00244 }
00245
00246
00247 if( $strCode[0] == "{" )
00248 {
00249 $strCode = substr( $strCode , 1 );
00250 }
00251
00252 $intLast = strlen( $strCode ) - 1;
00253
00254 if( $strCode[$intLast] == "}" )
00255 {
00256 $strCode = substr( $strCode , 0 , -1);
00257 }
00258 return $strCode;
00259 }
00260
00261 }
00262
00263 ?>