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 include_once( 'lib/ezutils/classes/ezdebug.php' );
00041 include_once( 'lib/eztemplate/classes/eztemplate.php' );
00042
00043 class eZTemplateOptimizer
00044 {
00045
00046
00047
00048 function eZTemplateOptimizer()
00049 {
00050 }
00051
00052
00053
00054
00055 function optimizeResourceAcquisition( $useComments, &$php, &$tpl, &$var, &$node, &$resourceData )
00056 {
00057 $data = $var[2];
00058
00059 if ( ( $var[1] == 'attributeAccess' ) and
00060 ( count( $data ) == 5 ) and
00061 ( $data[0][0] == EZ_TEMPLATE_TYPE_VARIABLE ) and
00062 ( $data[0][1][2] == 'node' ) and
00063 ( $data[1][0] == EZ_TEMPLATE_TYPE_ATTRIBUTE ) and
00064 ( $data[1][1][0][1] == 'object' ) and
00065 ( $data[2][0] == EZ_TEMPLATE_TYPE_ATTRIBUTE ) and
00066 ( $data[2][1][0][1] == 'data_map' ) and
00067 ( $data[3][0] == EZ_TEMPLATE_TYPE_ATTRIBUTE ) and
00068 ( $data[4][0] == EZ_TEMPLATE_TYPE_ATTRIBUTE ) and
00069 ( $data[4][1][0][1] == 'view_template' ) and
00070 ( $node[9] == 'attributeAccess' ) and
00071 ( isset( $resourceData['class-info'] ) ) )
00072 {
00073 $attribute = $data[3][1][0][1];
00074 if ( isset( $resourceData['class-info'][$attribute] ) and
00075 isset( $node[2][$resourceData['class-info'][$attribute]] ) )
00076 {
00077 $file = $node[2][$resourceData['class-info'][$attribute]];
00078 $node[0] = EZ_TEMPLATE_NODE_OPTIMIZED_RESOURCE_ACQUISITION;
00079 $node[10] = $resourceData['class-info'][$attribute];
00080 $node[2] = array( $node[10] => $file );
00081
00082 return true;
00083 }
00084 else
00085
00086 {
00087 $node[2] = array( 'dummy' => 'foo' );
00088 return false;
00089 }
00090
00091
00092
00093 return false;
00094 }
00095 else
00096 {
00097 return false;
00098 }
00099 }
00100
00101
00102
00103
00104 function optimizeFunction( $useComments, &$php, &$tpl, &$node, &$resourceData )
00105 {
00106 $ret = 0;
00107
00108 if ( isset( $node[3] ) and is_array( $node[3] ) )
00109 {
00110 foreach ( $node[3] as $key => $parameter )
00111 {
00112 $ret = eZTemplateOptimizer::optimizeVariable( $useComments, $php, $tpl, $node[3][$key], $resourceData );
00113 }
00114 }
00115 return $ret;
00116 }
00117
00118
00119
00120
00121 function optimizeVariable( $useComments, &$php, &$tpl, &$data, &$resourceData )
00122 {
00123 $ret = 0;
00124
00125 if ( ( count( $data ) >= 3 ) and
00126 ( $data[0][0] == EZ_TEMPLATE_TYPE_VARIABLE ) and
00127 ( $data[0][1][2] == 'node' ) and
00128 ( $data[1][0] == EZ_TEMPLATE_TYPE_ATTRIBUTE ) and
00129 ( $data[1][1][0][1] == 'object' ) and
00130 ( $data[2][0] == EZ_TEMPLATE_TYPE_ATTRIBUTE ) and
00131 ( $data[2][1][0][1] == 'data_map' ) )
00132 {
00133
00134
00135
00136 if ( ( count( $data ) >= 5 ) and
00137 ( $data[3][0] == EZ_TEMPLATE_TYPE_ATTRIBUTE ) and
00138 ( $data[4][0] == EZ_TEMPLATE_TYPE_ATTRIBUTE ) )
00139 {
00140 $data[3][0] = EZ_TEMPLATE_TYPE_OPTIMIZED_ARRAY_LOOKUP;
00141 if ( $data[4][1][0][1] == "content")
00142 {
00143 $data[4][0] = EZ_TEMPLATE_TYPE_OPTIMIZED_CONTENT_CALL;
00144 }
00145 else
00146 {
00147 $data[4][0] = EZ_TEMPLATE_TYPE_OPTIMIZED_ATTRIBUTE_LOOKUP;
00148 }
00149 }
00150
00151
00152 array_unshift( $data, array( EZ_TEMPLATE_TYPE_OPTIMIZED_NODE, null, 2 ) );
00153 $ret = 1;
00154 }
00155
00156
00157 if ( isset( $data[0] ) and
00158 $data[0][0] == EZ_TEMPLATE_NODE_INTERNAL_CODE_PIECE )
00159 {
00160 $functionRet = eZTemplateOptimizer::optimizeFunction( $useComments, $php, $tpl, $data[0], $resourceData );
00161
00162 $ret = $ret | $functionRet;
00163 }
00164 return $ret;
00165 }
00166
00167
00168
00169
00170 function optimize( $useComments, &$php, &$tpl, &$tree, &$resourceData )
00171 {
00172
00173 if (! is_array( $tree[1] ) )
00174 return;
00175
00176 $addNodeInit = false;
00177
00178
00179 foreach ( $tree[1] as $key => $kiddie )
00180 {
00181
00182 switch ( $kiddie[0] )
00183 {
00184 case EZ_TEMPLATE_NODE_INTERNAL_SPACING_INCREASE:
00185 case EZ_TEMPLATE_NODE_INTERNAL_SPACING_DECREASE:
00186
00187 unset( $tree[1][$key] );
00188 break;
00189 case 3:
00190 if ( isset( $tree[1][$key + 1] ) and
00191 ( $tree[1][$key + 1][0] == EZ_TEMPLATE_NODE_INTERNAL_RESOURCE_ACQUISITION ) and
00192 isset( $resourceData['class-info'] ) )
00193 {
00194 $ret = eZTemplateOptimizer::optimizeResourceAcquisition(
00195 $useComments, $php, $tpl,
00196 $tree[1][$key], $tree[1][$key + 1], $resourceData );
00197
00198
00199
00200 if ($ret)
00201 {
00202 unset( $tree[1][$key] );
00203 }
00204 }
00205 else
00206 {
00207 $ret = eZTemplateOptimizer::optimizeVariable( $useComments, $php, $tpl, $tree[1][$key][2], $resourceData );
00208 if ( $ret & 1 )
00209 $addNodeInit = true;
00210 }
00211 break;
00212 }
00213 }
00214 if ( $addNodeInit )
00215 {
00216 $initializer = array( EZ_TEMPLATE_NODE_OPTIMIZED_INIT, null, false );
00217 array_unshift( $tree[1], $initializer );
00218 }
00219 }
00220
00221 function fetchClassDeclaration( $classID )
00222 {
00223 include_once "kernel/classes/ezcontentclass.php";
00224 $attributes = eZContentClass::fetchAttributes( $classID );
00225 $attributeArray = array();
00226 foreach ( $attributes as $attribute )
00227 {
00228 $attributeArray[ $attribute->Identifier ] = $attribute->DataTypeString;
00229 }
00230 return $attributeArray;
00231 }
00232 }
00233 ?>