[ 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/ -> eztemplateoptimizer.php (source)

   1  <?php
   2  //
   3  // Definition of eZTemplateOptimizer class
   4  //
   5  // Created on: <16-Aug-2004 15:02:51 dr>
   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  /*! \file eztemplateoptimizer.php
  30  */
  31  
  32  /*!
  33    \class eZTemplateOptimizer eztemplateoptimizer.php
  34    \brief Analyses a compiled template tree and tries to optimize certain parts of it.
  35  
  36  */
  37  
  38  include_once ( 'lib/ezutils/classes/ezdebug.php' );
  39  include_once ( 'lib/eztemplate/classes/eztemplate.php' );
  40  
  41  class eZTemplateOptimizer
  42  {
  43      /*!
  44       Constructor
  45      */
  46      function eZTemplateOptimizer()
  47      {
  48      }
  49  
  50      /*!
  51       Optimizes a resource acquisition node and the variable data before it
  52      */
  53      function optimizeResourceAcquisition( $useComments, &$php, &$tpl, &$var, &$node, &$resourceData )
  54      {
  55          $data = $var[2];
  56          /* Check if the variable node has the correct format */
  57          if ( ( $var[1] == 'attributeAccess' ) and
  58               ( count( $data ) == 5 ) and
  59               ( $data[0][0] == EZ_TEMPLATE_TYPE_VARIABLE ) and
  60               ( $data[0][1][2] == 'node' ) and
  61               ( $data[1][0] == EZ_TEMPLATE_TYPE_ATTRIBUTE ) and
  62               ( $data[1][1][0][1] == 'object' ) and
  63               ( $data[2][0] == EZ_TEMPLATE_TYPE_ATTRIBUTE ) and
  64               ( $data[2][1][0][1] == 'data_map' ) and
  65               ( $data[3][0] == EZ_TEMPLATE_TYPE_ATTRIBUTE ) and
  66               ( $data[4][0] == EZ_TEMPLATE_TYPE_ATTRIBUTE ) and
  67               ( $data[4][1][0][1] == 'view_template' ) and
  68               ( $node[9] == 'attributeAccess' ) and
  69               ( isset( $resourceData['class-info'] ) ) )
  70          {
  71              $attribute = $data[3][1][0][1];
  72              if ( isset( $resourceData['class-info'][$attribute] ) and
  73                   isset( $node[2][$resourceData['class-info'][$attribute]] ) )
  74              {
  75                  $file = $node[2][$resourceData['class-info'][$attribute]];
  76                  $node[0] = EZ_TEMPLATE_NODE_OPTIMIZED_RESOURCE_ACQUISITION;
  77                  $node[10] = $resourceData['class-info'][$attribute];
  78                  $node[2] = array( $node[10] => $file );
  79  
  80                  return true;
  81              }
  82              else /* If we can't find it in the lookup table then it's simply
  83                    * not there, so we can just kill the array. */
  84              {
  85                  $node[2] = array( 'dummy' => 'foo' );
  86                  return false;
  87              }
  88              /* Added as an extra fall back, this point should never be reached,
  89               * but if it does then we make sure not to mess up the original
  90               * array in the calling function. */
  91              return false;
  92          }
  93          else
  94          {
  95              return false;
  96          }
  97      }
  98  
  99      /*!
 100       Analyses function nodes and tries to optimize them
 101      */
 102      function optimizeFunction( $useComments, &$php, &$tpl, &$node, &$resourceData )
 103      {
 104          $ret = 0;
 105          /* Just run the optimizer over all parameters */
 106          if ( isset( $node[3] ) and is_array( $node[3] ) )
 107          {
 108              foreach ( $node[3] as $key => $parameter )
 109              {
 110                  $ret = eZTemplateOptimizer::optimizeVariable( $useComments, $php, $tpl, $node[3][$key], $resourceData );
 111              }
 112          }
 113          return $ret;
 114      }
 115  
 116      /*!
 117       Analyses variables and tries to optimize them
 118      */
 119      function optimizeVariable( $useComments, &$php, &$tpl, &$data, &$resourceData )
 120      {
 121          $ret = 0;
 122          /* node.object.data_map optimization */
 123          if ( ( count( $data ) >= 3 ) and
 124               ( $data[0][0] == EZ_TEMPLATE_TYPE_VARIABLE ) and
 125               ( $data[0][1][2] == 'node' ) and
 126               ( $data[1][0] == EZ_TEMPLATE_TYPE_ATTRIBUTE ) and
 127               ( $data[1][1][0][1] == 'object' ) and
 128               ( $data[2][0] == EZ_TEMPLATE_TYPE_ATTRIBUTE ) and
 129               ( $data[2][1][0][1] == 'data_map' ) )
 130          {
 131              /* Modify the next two nodes in the array too as we know for sure
 132               * what type it is. This fixes the dependency on
 133               * compiledFetchAttribute */
 134              if ( ( count( $data ) >= 5 ) and
 135                   ( $data[3][0] == EZ_TEMPLATE_TYPE_ATTRIBUTE ) and
 136                   ( $data[4][0] == EZ_TEMPLATE_TYPE_ATTRIBUTE ) )
 137              {
 138                  $data[3][0] = EZ_TEMPLATE_TYPE_OPTIMIZED_ARRAY_LOOKUP;
 139                  if ( $data[4][1][0][1] == "content")
 140                  {
 141                      $data[4][0] = EZ_TEMPLATE_TYPE_OPTIMIZED_CONTENT_CALL;
 142                  }
 143                  else
 144                  {
 145                      $data[4][0] = EZ_TEMPLATE_TYPE_OPTIMIZED_ATTRIBUTE_LOOKUP;
 146                  }
 147              }
 148  
 149              /* Create a new node representing the optimization */
 150              array_unshift( $data, array( EZ_TEMPLATE_TYPE_OPTIMIZED_NODE, null, 2 ) );
 151              $ret = 1;
 152          }
 153  
 154          /* node.object.data_map optimization through function */
 155          if ( isset( $data[0] ) and
 156               $data[0][0] == EZ_TEMPLATE_NODE_INTERNAL_CODE_PIECE )
 157          {
 158              $functionRet = eZTemplateOptimizer::optimizeFunction( $useComments, $php, $tpl, $data[0], $resourceData );
 159              // Merge settings
 160              $ret = $ret | $functionRet;
 161          }
 162          return $ret;
 163      }
 164  
 165      /*!
 166       Runs the optimizer
 167      */
 168      function optimize( $useComments, &$php, &$tpl, &$tree, &$resourceData )
 169      {
 170          /* If for some reason we don't have elements, simply return */
 171          if (! is_array( $tree[1] ) )
 172              return;
 173  
 174          $addNodeInit = false;
 175  
 176          /* Loop through the children of the root */
 177          foreach ( $tree[1] as $key => $kiddie )
 178          {
 179              /* Analyse per node type */
 180              switch ( $kiddie[0] )
 181              {
 182                  case EZ_TEMPLATE_NODE_INTERNAL_SPACING_INCREASE:
 183                  case EZ_TEMPLATE_NODE_INTERNAL_SPACING_DECREASE:
 184                      /* Removing unnecessary whitespace changes */
 185                      unset( $tree[1][$key] );
 186                      break;
 187                  case 3: /* Variable */
 188                      if ( isset( $tree[1][$key + 1] ) and
 189                           ( $tree[1][$key + 1][0] == EZ_TEMPLATE_NODE_INTERNAL_RESOURCE_ACQUISITION ) and
 190                           isset( $resourceData['class-info'] ) )
 191                      {
 192                          $ret = eZTemplateOptimizer::optimizeResourceAcquisition(
 193                              $useComments, $php, $tpl,
 194                              $tree[1][$key], $tree[1][$key + 1], $resourceData );
 195                          /* We only unset the tree node when the optimization
 196                           * function returns false, as that means that the
 197                           * optimization could not be made. */
 198                          if ($ret)
 199                          {
 200                              unset( $tree[1][$key] );
 201                          }
 202                      }
 203                      else
 204                      {
 205                          $ret = eZTemplateOptimizer::optimizeVariable( $useComments, $php, $tpl, $tree[1][$key][2], $resourceData );
 206                          if ( $ret & 1 )
 207                              $addNodeInit = true;
 208                      }
 209                      break;
 210              }
 211          }
 212          if ( $addNodeInit )
 213          {
 214              $initializer = array( EZ_TEMPLATE_NODE_OPTIMIZED_INIT, null, false );
 215              array_unshift( $tree[1], $initializer );
 216          }
 217      }
 218  
 219      function fetchClassDeclaration( $classID )
 220      {
 221          include_once  "kernel/classes/ezcontentclass.php";
 222          $attributes = eZContentClass::fetchAttributes( $classID );
 223          $attributeArray = array();
 224          foreach ( $attributes as $attribute )
 225          {
 226              $attributeArray[ $attribute->Identifier ] = $attribute->DataTypeString;
 227          }
 228          return $attributeArray;
 229      }
 230  }
 231  ?>


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