00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 class eZTemplateDebugFunction
00083 {
00084
00085
00086
00087 function eZTemplateDebugFunction( $timingPoint = 'debug-timing-point',
00088 $accumulator = 'debug-accumulator',
00089 $log = 'debug-log',
00090 $trace = 'debug-trace' )
00091 {
00092 $this->TimingPointName = $timingPoint;
00093 $this->AccumulatorName = $accumulator;
00094 $this->LogName = $log;
00095 $this->TraceName = $trace;
00096 }
00097
00098
00099
00100
00101 function functionList()
00102 {
00103 return array( $this->TimingPointName, $this->AccumulatorName, $this->LogName, $this->TraceName );
00104 }
00105
00106
00107
00108
00109
00110
00111 function attributeList()
00112 {
00113 return array(
00114 $this->TimingPointName => true,
00115 $this->AccumulatorName => true,
00116 $this->LogName => false,
00117 $this->TraceName => true
00118 );
00119 }
00120
00121 function functionTemplateHints()
00122 {
00123 return array( $this->TimingPointName => array( 'parameters' => true,
00124 'static' => false,
00125 'transform-children' => true,
00126 'tree-transformation' => true,
00127 'transform-parameters' => true ),
00128 $this->AccumulatorName => array( 'parameters' => true,
00129 'static' => false,
00130 'transform-children' => true,
00131 'tree-transformation' => true,
00132 'transform-parameters' => true ),
00133 $this->LogName => array( 'parameters' => true,
00134 'static' => false,
00135 'transform-children' => true,
00136 'tree-transformation' => true,
00137 'transform-parameters' => true ),
00138 $this->TraceName => array( 'parameters' => true,
00139 'static' => false,
00140 'transform-children' => true,
00141 'tree-transformation' => true,
00142 'transform-parameters' => true ) );
00143 }
00144
00145 function templateNodeTransformation( $functionName, &$node,
00146 &$tpl, $parameters, $privateData )
00147 {
00148 if ( $functionName == $this->TimingPointName )
00149 {
00150 $id = false;
00151 if ( isset( $parameters['id'] ) )
00152 {
00153 if ( !eZTemplateNodeTool::isConstantElement( $parameters['id'] ) )
00154 return false;
00155 $id = eZTemplateNodeTool::elementConstantValue( $parameters['id'] );
00156 }
00157
00158 $newNodes = array();
00159
00160 $startDescription = "debug-timing-point START: $id";
00161 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "eZDebug::addTimingPoint( " . var_export( $startDescription, true ) . " );" );
00162
00163 $children = eZTemplateNodeTool::extractFunctionNodeChildren( $node );
00164 $newNodes = array_merge( $newNodes, $children );
00165
00166 $endDescription = "debug-timing-point END: $id";
00167 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "eZDebug::addTimingPoint( " . var_export( $endDescription, true ) . " );" );
00168
00169 return $newNodes;
00170 }
00171 else if ( $functionName == $this->AccumulatorName )
00172 {
00173 $id = false;
00174 if ( isset( $parameters['id'] ) )
00175 {
00176 if ( !eZTemplateNodeTool::isConstantElement( $parameters['id'] ) )
00177 return false;
00178 $id = eZTemplateNodeTool::elementConstantValue( $parameters['id'] );
00179 }
00180
00181 $name = false;
00182 if ( isset( $parameters['name'] ) )
00183 {
00184 if ( !eZTemplateNodeTool::isConstantElement( $parameters['name'] ) )
00185 return false;
00186 $name = eZTemplateNodeTool::elementConstantValue( $parameters['name'] );
00187 }
00188
00189 $name = ( $name === false and $id === false ) ? $functionName : $name;
00190
00191 $id = $id === false ? uniqID( $functionName . '_' ): $id;
00192 $newNodes = array();
00193
00194 if ( $name )
00195 {
00196 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "eZDebug::accumulatorStart( " . var_export( $id, true ) . ", 'Debug-Accumulator', " . var_export( $name, true ) . " );" );
00197 }
00198 else
00199 {
00200 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "eZDebug::accumulatorStart( " . var_export( $id, true ) . ", 'Debug-Accumulator' );" );
00201 }
00202
00203 $children = eZTemplateNodeTool::extractFunctionNodeChildren( $node );
00204 $newNodes = array_merge( $newNodes, $children );
00205
00206 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "eZDebug::accumulatorStop( " . var_export( $id, true ) . " );" );
00207
00208 return $newNodes;
00209 }
00210 else if ( $functionName == $this->LogName )
00211 {
00212 $nodePlacement = eZTemplateNodeTool::extractFunctionNodePlacement( $node );
00213 $newNodes = array();
00214
00215 $varIsSet = $msgIsSet = false;
00216 if ( isset( $parameters['var'] ) )
00217 {
00218 $varIsSet = true;
00219 $var = $parameters['var'];
00220 }
00221 if ( isset( $parameters['msg'] ) )
00222 {
00223 $msgIsSet = true;
00224 $msg = $parameters['msg'];
00225 }
00226
00227 $newNodes[]= eZTemplateNodeTool::createCodePieceNode( "// debug-log starts\n" );
00228
00229 if ( $varIsSet )
00230 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $var, $nodePlacement, array( 'treat-value-as-non-object' => true ), 'debug_log_var' );
00231 if ( $msgIsSet )
00232 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $msg, $nodePlacement, array( 'treat-value-as-non-object' => true ), 'debug_log_msg' );
00233
00234 if ( $varIsSet && $msgIsSet )
00235 $newNodes[]= eZTemplateNodeTool::createCodePieceNode( "eZDebug::writeDebug( \$debug_log_var, \$debug_log_msg );\n" );
00236 elseif ( $msgIsSet )
00237 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "eZDebug::writeDebug( \$debug_log_msg );\n" );
00238 elseif ( $varIsSet )
00239 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "eZDebug::writeDebug( \$debug_log_var );\n" );
00240
00241 if ( $varIsSet )
00242 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "unset( \$debug_log_var );" );
00243 if ( $msgIsSet )
00244 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "unset( \$debug_log_msg );" );
00245
00246 $newNodes[]= eZTemplateNodeTool::createCodePieceNode( "// debug-log ends\n" );
00247
00248 return $newNodes;
00249 }
00250 else if ( $functionName == $this->TraceName )
00251 {
00252 $id = false;
00253 if ( isset( $parameters['id'] ) )
00254 {
00255 if ( !eZTemplateNodeTool::isConstantElement( $parameters['id'] ) )
00256 return false;
00257 $id = eZTemplateNodeTool::elementConstantValue( $parameters['id'] );
00258 }
00259
00260 if ( !$id )
00261 $id = 'template-debug';
00262
00263 $newNodes = array();
00264
00265 $code = ( "if ( extension_loaded( 'xdebug' ) )\n" .
00266 "{\n" .
00267 "if ( file_exists( " . var_export( $id . '.xt', true ) . " ) )\n" .
00268 "{\n" .
00269 "\$fd = fopen( " . var_export( $id . '.xt', true ) . ", 'w' ); fclose( \$fd ); unset( \$fd );\n" .
00270 "}\n" .
00271 "xdebug_start_trace( " . var_export( $id, true ) . " );\n" .
00272 "}\n" );
00273 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( $code );
00274
00275 $children = eZTemplateNodeTool::extractFunctionNodeChildren( $node );
00276 $newNodes = array_merge( $newNodes, $children );
00277
00278 $code = ( "if ( extension_loaded( 'xdebug' ) )\n" .
00279 "xdebug_stop_trace();\n" );
00280 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( $code );
00281
00282 return $newNodes;
00283 }
00284 return false;
00285 }
00286
00287
00288
00289
00290 function process( &$tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace )
00291 {
00292 switch ( $functionName )
00293 {
00294 case $this->TimingPointName:
00295 {
00296 $children = $functionChildren;
00297 $parameters = $functionParameters;
00298
00299 $id = false;
00300 if ( isset( $parameters["id"] ) )
00301 {
00302 $id = $tpl->elementValue( $parameters["id"], $rootNamespace, $currentNamespace, $functionPlacement );
00303 }
00304
00305
00306 $startDescription = "debug-timing-point START: $id";
00307 eZDebug::addTimingPoint( $startDescription );
00308
00309 if ( is_array( $children ) )
00310 {
00311 foreach ( array_keys( $children ) as $childKey )
00312 {
00313 $child =& $children[$childKey];
00314 $tpl->processNode( $child, $textElements, $rootNamespace, $currentNamespace );
00315 }
00316 }
00317
00318 $endDescription = "debug-timing-point END: $id";
00319 eZDebug::addTimingPoint( $endDescription );
00320
00321 } break;
00322
00323 case $this->AccumulatorName:
00324 {
00325 $children = $functionChildren;
00326 $parameters = $functionParameters;
00327
00328 $id = false;
00329 if ( isset( $parameters["id"] ) )
00330 {
00331 $id = $tpl->elementValue( $parameters["id"], $rootNamespace, $currentNamespace, $functionPlacement );
00332 }
00333
00334 $name = false;
00335 if ( isset( $parameters["name"] ) )
00336 {
00337 $name = $tpl->elementValue( $parameters["name"], $rootNamespace, $currentNamespace, $functionPlacement );
00338 }
00339
00340
00341 $name = ( $name === false and $id === false ) ? $functionName : $name;
00342
00343 $id = $id === false ? uniqID( $functionName . '_' ): $id;
00344
00345 eZDebug::accumulatorStart( $id, 'Debug-Accumulator', $name );
00346
00347 if ( is_array( $children ) )
00348 {
00349 foreach ( array_keys( $children ) as $childKey )
00350 {
00351 $child =& $children[$childKey];
00352 $tpl->processNode( $child, $textElements, $rootNamespace, $currentNamespace );
00353 }
00354 }
00355
00356 eZDebug::accumulatorStop( $id, 'Debug-Accumulator', $name );
00357
00358 } break;
00359
00360 case $this->LogName:
00361 {
00362 $parameters = $functionParameters;
00363
00364 if ( isset( $parameters['var'] ) )
00365 $var = $tpl->elementValue( $parameters['var'], $rootNamespace, $currentNamespace, $functionPlacement );
00366 if ( isset( $parameters['msg'] ) )
00367 $msg = $tpl->elementValue( $parameters['msg'], $rootNamespace, $currentNamespace, $functionPlacement );
00368
00369 if ( isset( $var ) && isset( $msg ) )
00370 eZDebug::writeDebug( $var, $msg );
00371 elseif ( isset( $msg ) )
00372 eZDebug::writeDebug( $msg );
00373 elseif ( isset( $var ) )
00374 eZDebug::writeDebug( $var );
00375 } break;
00376
00377 case $this->TraceName:
00378 {
00379 $children = $functionChildren;
00380
00381 $id = false;
00382
00383
00384 if ( extension_loaded( 'xdebug' ) )
00385 {
00386 $parameters = $functionParameters;
00387 if ( isset( $parameters["id"] ) )
00388 {
00389 $id = $tpl->elementValue( $parameters["id"], $rootNamespace, $currentNamespace, $functionPlacement );
00390 }
00391
00392 if ( !$id )
00393 $id = 'template-debug';
00394
00395
00396 if ( file_exists( $id . '.xt' ) )
00397 {
00398 $fd = fopen( $id, '.xt', 'w' ); fclose( $fd );
00399 }
00400 xdebug_start_trace( $id );
00401
00402 if ( is_array( $children ) )
00403 {
00404 foreach ( array_keys( $children ) as $childKey )
00405 {
00406 $child =& $children[$childKey];
00407 $tpl->processNode( $child, $textElements, $rootNamespace, $currentNamespace );
00408 }
00409 }
00410
00411 xdebug_stop_trace();
00412 }
00413 elseif ( is_array( $children ) )
00414 {
00415 foreach ( array_keys( $children ) as $childKey )
00416 {
00417 $child =& $children[$childKey];
00418 $tpl->processNode( $child, $textElements, $rootNamespace, $currentNamespace );
00419 }
00420 }
00421
00422 } break;
00423 }
00424 }
00425
00426
00427
00428
00429 function hasChildren()
00430 {
00431 return $this->attributeList();
00432 }
00433
00434
00435
00436 var $DebugName;
00437 var $AppendDebugName;
00438 var $OnceName;
00439 }
00440
00441 ?>