[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/eztemplate/classes/ -> eztemplatedebugfunction.php (source)

   1  <?php
   2  //
   3  // Definition of eZTemplateDebugFunction class
   4  //
   5  // Created on: <01-Mar-2002 13:50:33 amos>
   6  //
   7  // SOFTWARE NAME: eZ publish
   8  // SOFTWARE RELEASE: 3.9.0
   9  // BUILD VERSION: 17785
  10  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  11  // SOFTWARE LICENSE: GNU General Public License v2.0
  12  // NOTICE: >
  13  //   This program is free software; you can redistribute it and/or
  14  //   modify it under the terms of version 2.0  of the GNU General
  15  //   Public License as published by the Free Software Foundation.
  16  //
  17  //   This program is distributed in the hope that it will be useful,
  18  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20  //   GNU General Public License for more details.
  21  //
  22  //   You should have received a copy of version 2.0 of the GNU General
  23  //   Public License along with this program; if not, write to the Free
  24  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25  //   MA 02110-1301, USA.
  26  //
  27  //
  28  
  29  /*!
  30    \class eZTemplateDebugFunction eztemplatedebugfunction.php
  31    \ingroup eZTemplateFunctions
  32    \brief Advanced debug handling
  33  
  34    debug-timing-point
  35    Starts a timing point, executes body and ends the timing point.
  36    This is useful if you want to figure out how fast a piece of
  37    template code goes or to see all debug entries that occur
  38    between these two points.
  39  
  40    \code
  41    {debug-timing-point id=""}
  42    {$item} - {$item2}
  43    {/debug-timing-point}
  44    \endcode
  45  
  46    debug-accumulator
  47    Executes the body and performs statistics.
  48    The number of calls, total time and average time will be shown in debug.
  49  
  50    \code
  51    {debug-accumulator}
  52    {section var=error loop=$errors}{$error}{/section}
  53    {/debug-accumulator}
  54    \endcode
  55  
  56    debug-log
  57    Does exactly the same as eZDebug::writeDebug() method.
  58    Has two parameters:
  59    - var: variable to dump
  60    - msg: text message
  61  
  62    \code
  63    {debug-log var=$object msg='object contents'}
  64    {debug-log msg='hello world'}
  65    {debug-log var=array(1,2,3)}
  66    \endcode
  67  
  68    debug-trace
  69    Executes the body while tracing the result using XDebug.
  70    The result will a trace file made by XDebug which can be analyzed.
  71    Note: This will not do anything when XDebug is not available
  72  
  73    \code
  74    {debug-trace id="loop"}
  75    {section var=error loop=$errors}{$error}{/section}
  76    {/debug-trace}
  77    \endcode
  78  */
  79  
  80  class eZTemplateDebugFunction
  81  {
  82      /*!
  83       Initializes the object with names.
  84      */
  85      function eZTemplateDebugFunction( $timingPoint = 'debug-timing-point',
  86                                        $accumulator = 'debug-accumulator',
  87                                        $log = 'debug-log',
  88                                        $trace = 'debug-trace' )
  89      {
  90          $this->TimingPointName = $timingPoint;
  91          $this->AccumulatorName = $accumulator;
  92          $this->LogName = $log;
  93          $this->TraceName = $trace;
  94      }
  95  
  96      /*!
  97       Return the list of available functions.
  98      */
  99      function functionList()
 100      {
 101          return array( $this->TimingPointName, $this->AccumulatorName, $this->LogName, $this->TraceName );
 102      }
 103  
 104      /*!
 105       * Returns the attribute list.
 106       * key:   parameter name
 107       * value: can have children
 108       */
 109      function attributeList()
 110      {
 111          return array(
 112              $this->TimingPointName => true,
 113              $this->AccumulatorName => true,
 114              $this->LogName => false,
 115              $this->TraceName => true
 116          );
 117      }
 118  
 119      function functionTemplateHints()
 120      {
 121          return array( $this->TimingPointName => array( 'parameters' => true,
 122                                                         'static' => false,
 123                                                         'transform-children' => true,
 124                                                         'tree-transformation' => true,
 125                                                         'transform-parameters' => true ),
 126                        $this->AccumulatorName => array( 'parameters' => true,
 127                                                         'static' => false,
 128                                                         'transform-children' => true,
 129                                                         'tree-transformation' => true,
 130                                                         'transform-parameters' => true ),
 131                        $this->LogName => array( 'parameters' => true,
 132                                                 'static' => false,
 133                                                 'transform-children' => true,
 134                                                 'tree-transformation' => true,
 135                                                 'transform-parameters' => true ),
 136                        $this->TraceName => array( 'parameters' => true,
 137                                                   'static' => false,
 138                                                   'transform-children' => true,
 139                                                   'tree-transformation' => true,
 140                                                   'transform-parameters' => true ) );
 141      }
 142  
 143      function templateNodeTransformation( $functionName, &$node,
 144                                           &$tpl, $parameters, $privateData )
 145      {
 146          if ( $functionName == $this->TimingPointName )
 147          {
 148              $id = false;
 149              if ( isset( $parameters['id'] ) )
 150              {
 151                  if ( !eZTemplateNodeTool::isConstantElement( $parameters['id'] ) )
 152                      return false;
 153                  $id = eZTemplateNodeTool::elementConstantValue( $parameters['id'] );
 154              }
 155  
 156              $newNodes = array();
 157  
 158              $startDescription = "debug-timing-point START: $id";
 159              $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "eZDebug::addTimingPoint( " . var_export( $startDescription, true ) . " );" );
 160  
 161              $children = eZTemplateNodeTool::extractFunctionNodeChildren( $node );
 162              $newNodes = array_merge( $newNodes, $children );
 163  
 164              $endDescription = "debug-timing-point END: $id";
 165              $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "eZDebug::addTimingPoint( " . var_export( $endDescription, true ) . " );" );
 166  
 167              return $newNodes;
 168          }
 169          else if ( $functionName == $this->AccumulatorName )
 170          {
 171              $id = false;
 172              if ( isset( $parameters['id'] ) )
 173              {
 174                  if ( !eZTemplateNodeTool::isConstantElement( $parameters['id'] ) )
 175                      return false;
 176                  $id = eZTemplateNodeTool::elementConstantValue( $parameters['id'] );
 177              }
 178  
 179              $name = false;
 180              if ( isset( $parameters['name'] ) )
 181              {
 182                  if ( !eZTemplateNodeTool::isConstantElement( $parameters['name'] ) )
 183                      return false;
 184                  $name = eZTemplateNodeTool::elementConstantValue( $parameters['name'] );
 185              }
 186              // Assign a name (as $functionName) which will be used in the debug output.
 187              $name = ( $name === false and $id === false ) ?  $functionName : $name;
 188              // To uniquely identify this accumulator.
 189              $id = $id === false ? uniqID( $functionName . '_' ): $id;
 190              $newNodes = array();
 191  
 192              if ( $name )
 193              {
 194                  $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "eZDebug::accumulatorStart( " . var_export( $id, true ) . ", 'Debug-Accumulator', " . var_export( $name, true ) . " );" );
 195              }
 196              else
 197              {
 198                  $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "eZDebug::accumulatorStart( " . var_export( $id, true ) . ", 'Debug-Accumulator' );" );
 199              }
 200  
 201              $children = eZTemplateNodeTool::extractFunctionNodeChildren( $node );
 202              $newNodes = array_merge( $newNodes, $children );
 203  
 204              $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "eZDebug::accumulatorStop( " . var_export( $id, true ) . " );" );
 205  
 206              return $newNodes;
 207          }
 208          else if ( $functionName == $this->LogName )
 209          {
 210              $nodePlacement  = eZTemplateNodeTool::extractFunctionNodePlacement( $node );
 211              $newNodes = array();
 212  
 213              $varIsSet = $msgIsSet = false;
 214              if ( isset( $parameters['var'] ) )
 215              {
 216                  $varIsSet = true;
 217                  $var = $parameters['var'];
 218              }
 219              if ( isset( $parameters['msg'] ) )
 220              {
 221                  $msgIsSet = true;
 222                  $msg = $parameters['msg'];
 223              }
 224  
 225              $newNodes[]= eZTemplateNodeTool::createCodePieceNode( "// debug-log starts\n" );
 226  
 227              if ( $varIsSet )
 228                  $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $var, $nodePlacement, array( 'treat-value-as-non-object' => true ), 'debug_log_var' );
 229              if ( $msgIsSet )
 230                  $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $msg, $nodePlacement, array( 'treat-value-as-non-object' => true ), 'debug_log_msg' );
 231  
 232              if ( $varIsSet && $msgIsSet )
 233                   $newNodes[]= eZTemplateNodeTool::createCodePieceNode( "eZDebug::writeDebug( \$debug_log_var, \$debug_log_msg );\n" );
 234              elseif ( $msgIsSet )
 235                  $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "eZDebug::writeDebug( \$debug_log_msg );\n" );
 236              elseif ( $varIsSet )
 237                  $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "eZDebug::writeDebug( \$debug_log_var );\n" );
 238  
 239              if ( $varIsSet )
 240                  $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "unset( \$debug_log_var );" );
 241              if ( $msgIsSet )
 242                  $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "unset( \$debug_log_msg );" );
 243  
 244              $newNodes[]= eZTemplateNodeTool::createCodePieceNode( "// debug-log ends\n" );
 245  
 246              return $newNodes;
 247          }
 248          else if ( $functionName == $this->TraceName )
 249          {
 250              $id = false;
 251              if ( isset( $parameters['id'] ) )
 252              {
 253                  if ( !eZTemplateNodeTool::isConstantElement( $parameters['id'] ) )
 254                      return false;
 255                  $id = eZTemplateNodeTool::elementConstantValue( $parameters['id'] );
 256              }
 257  
 258              if ( !$id )
 259                  $id = 'template-debug';
 260  
 261              $newNodes = array();
 262  
 263              $code = ( "if ( extension_loaded( 'xdebug' ) )\n" .
 264                        "{\n" .
 265                        "if ( file_exists( " . var_export( $id . '.xt', true ) . " ) )\n" .
 266                        "{\n" .
 267                        "\$fd = fopen( " . var_export( $id . '.xt', true ) . ", 'w' ); fclose( \$fd ); unset( \$fd );\n" .
 268                        "}\n" .
 269                        "xdebug_start_trace( " . var_export( $id, true ) . " );\n" .
 270                        "}\n" );
 271              $newNodes[] = eZTemplateNodeTool::createCodePieceNode( $code );
 272  
 273              $children = eZTemplateNodeTool::extractFunctionNodeChildren( $node );
 274              $newNodes = array_merge( $newNodes, $children );
 275  
 276              $code = ( "if ( extension_loaded( 'xdebug' ) )\n" .
 277                        "xdebug_stop_trace();\n" );
 278              $newNodes[] = eZTemplateNodeTool::createCodePieceNode( $code );
 279  
 280              return $newNodes;
 281          }
 282          return false;
 283      }
 284  
 285      /*!
 286       Processes the function with all it's children.
 287      */
 288      function process( &$tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace )
 289      {
 290          switch ( $functionName )
 291          {
 292              case $this->TimingPointName:
 293              {
 294                  $children = $functionChildren;
 295                  $parameters = $functionParameters;
 296  
 297                  $id = false;
 298                  if ( isset( $parameters["id"] ) )
 299                  {
 300                      $id = $tpl->elementValue( $parameters["id"], $rootNamespace, $currentNamespace, $functionPlacement );
 301                  }
 302  
 303  
 304                  $startDescription = "debug-timing-point START: $id";
 305                  eZDebug::addTimingPoint( $startDescription );
 306  
 307                  if ( is_array( $children ) )
 308                  {
 309                      foreach ( array_keys( $children ) as $childKey )
 310                      {
 311                          $child =& $children[$childKey];
 312                          $tpl->processNode( $child, $textElements, $rootNamespace, $currentNamespace );
 313                      }
 314                  }
 315  
 316                  $endDescription = "debug-timing-point END: $id";
 317                  eZDebug::addTimingPoint( $endDescription );
 318  
 319              } break;
 320  
 321              case $this->AccumulatorName:
 322              {
 323                  $children = $functionChildren;
 324                  $parameters = $functionParameters;
 325  
 326                  $id = false;
 327                  if ( isset( $parameters["id"] ) )
 328                  {
 329                      $id = $tpl->elementValue( $parameters["id"], $rootNamespace, $currentNamespace, $functionPlacement );
 330                  }
 331  
 332                  $name = false;
 333                  if ( isset( $parameters["name"] ) )
 334                  {
 335                      $name = $tpl->elementValue( $parameters["name"], $rootNamespace, $currentNamespace, $functionPlacement );
 336                  }
 337  
 338                  // Assign a name (as $functionName) which will be used in the debug output.
 339                  $name = ( $name === false and $id === false ) ?  $functionName : $name;
 340                  // To uniquely identify this accumulator.
 341                  $id = $id === false ? uniqID( $functionName . '_' ): $id;
 342  
 343                  eZDebug::accumulatorStart( $id, 'Debug-Accumulator', $name );
 344  
 345                  if ( is_array( $children ) )
 346                  {
 347                      foreach ( array_keys( $children ) as $childKey )
 348                      {
 349                          $child =& $children[$childKey];
 350                          $tpl->processNode( $child, $textElements, $rootNamespace, $currentNamespace );
 351                      }
 352                  }
 353  
 354                  eZDebug::accumulatorStop( $id, 'Debug-Accumulator', $name );
 355  
 356              } break;
 357  
 358              case $this->LogName:
 359              {
 360                  $parameters = $functionParameters;
 361  
 362                  if ( isset( $parameters['var'] ) )
 363                      $var = $tpl->elementValue( $parameters['var'], $rootNamespace, $currentNamespace, $functionPlacement );
 364                  if ( isset( $parameters['msg'] ) )
 365                      $msg = $tpl->elementValue( $parameters['msg'], $rootNamespace, $currentNamespace, $functionPlacement );
 366  
 367                  if ( isset( $var ) && isset( $msg ) )
 368                      eZDebug::writeDebug( $var, $msg );
 369                  elseif ( isset( $msg ) )
 370                      eZDebug::writeDebug( $msg );
 371                  elseif ( isset( $var ) )
 372                      eZDebug::writeDebug( $var );
 373              } break;
 374  
 375              case $this->TraceName:
 376              {
 377                  $children = $functionChildren;
 378  
 379                  $id = false;
 380                  // If we have XDebug we start the trace, execute children and stop it
 381                  // if not we just execute the children as normal
 382                  if ( extension_loaded( 'xdebug' ) )
 383                  {
 384                      $parameters = $functionParameters;
 385                      if ( isset( $parameters["id"] ) )
 386                      {
 387                          $id = $tpl->elementValue( $parameters["id"], $rootNamespace, $currentNamespace, $functionPlacement );
 388                      }
 389  
 390                      if ( !$id )
 391                          $id = 'template-debug';
 392  
 393                      // If we already have a file, make sure it is truncated
 394                      if ( file_exists( $id . '.xt' ) )
 395                      {
 396                          $fd = fopen( $id, '.xt', 'w' ); fclose( $fd );
 397                      }
 398                      xdebug_start_trace( $id );
 399  
 400                      if ( is_array( $children ) )
 401                      {
 402                          foreach ( array_keys( $children ) as $childKey )
 403                          {
 404                              $child =& $children[$childKey];
 405                              $tpl->processNode( $child, $textElements, $rootNamespace, $currentNamespace );
 406                          }
 407                      }
 408  
 409                      xdebug_stop_trace();
 410                  }
 411                  elseif ( is_array( $children ) )
 412                  {
 413                      foreach ( array_keys( $children ) as $childKey )
 414                      {
 415                          $child =& $children[$childKey];
 416                          $tpl->processNode( $child, $textElements, $rootNamespace, $currentNamespace );
 417                      }
 418                  }
 419  
 420              } break;
 421          }
 422      }
 423  
 424      /*!
 425       Returns true.
 426      */
 427      function hasChildren()
 428      {
 429          return $this->attributeList();
 430      }
 431  
 432      /// \privatesection
 433      /// Name of the function
 434      var $DebugName;
 435      var $AppendDebugName;
 436      var $OnceName;
 437  }
 438  
 439  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7