Пример #1
0
 static function compileTemplate($tpl, $key, &$resourceData)
 {
     if (!eZTemplateCompiler::isCompilationEnabled()) {
         return false;
     }
     $resourceData['use-comments'] = eZTemplateCompiler::isCommentsEnabled();
     $cacheFileName = eZTemplateCompiler::compilationFilename($key, $resourceData);
     $resourceData['uniqid'] = md5($resourceData['template-filename'] . uniqid("ezp" . getmypid(), true));
     // Time limit #1:
     // We reset the time limit to 30 seconds to ensure that templates
     // have enough time to compile
     // However if time limit is unlimited (0) we leave it be
     // Time limit will also be reset after subtemplates are compiled
     $maxExecutionTime = ini_get('max_execution_time');
     if ($maxExecutionTime != 0 && $maxExecutionTime < 30) {
         @set_time_limit(30);
     }
     $rootNode =& $resourceData['root-node'];
     if (!$rootNode) {
         return false;
     }
     $GLOBALS['eZTemplateCompilerResourceCache'][$resourceData['template-filename']] =& $resourceData;
     $useComments = eZTemplateCompiler::isCommentsEnabled();
     if (!$resourceData['test-compile']) {
         eZTemplateCompiler::createCommonCompileTemplate();
     }
     /* Check if we need to disable the generation of spacing for the compiled templates */
     $ini = eZINI::instance();
     $spacing = 'disabled';
     if ($ini->variable('TemplateSettings', 'UseFormatting') == 'enabled') {
         $spacing = 'enabled';
     }
     $php = new eZPHPCreator(eZTemplateCompiler::compilationDirectory(), $cacheFileName, eZTemplateCompiler::TemplatePrefix(), array('spacing' => $spacing));
     $php->addComment('URI:       ' . $resourceData['uri']);
     $php->addComment('Filename:  ' . $resourceData['template-filename']);
     $php->addComment('Timestamp: ' . $resourceData['time-stamp'] . ' (' . date('D M j G:i:s T Y', $resourceData['time-stamp']) . ')');
     $php->addCodePiece("\$oldSetArray_{$resourceData['uniqid']} = isset( \$setArray ) ? \$setArray : array();\n" . "\$setArray = array();\n");
     // Code to decrement include level of the templates
     $php->addCodePiece("\$tpl->Level++;\n");
     $php->addCodePiece("if ( \$tpl->Level > {$tpl->MaxLevel} )\n" . "{\n" . "\$text = \$tpl->MaxLevelWarning;" . "\$tpl->Level--;\n" . "return;\n" . "}\n");
     if ($resourceData['locales'] && count($resourceData['locales'])) {
         $php->addComment('Locales:   ' . join(', ', $resourceData['locales']));
         $php->addCodePiece('$locales = array( "' . join('", "', $resourceData['locales']) . "\" );\n" . '$oldLocale_' . $resourceData['uniqid'] . ' = setlocale( LC_CTYPE, null );' . "\n" . '$currentLocale_' . $resourceData['uniqid'] . ' = setlocale( LC_CTYPE, $locales );' . "\n");
     }
     //         $php->addCodePiece( "print( \"" . $resourceData['template-filename'] . " ($cacheFileName)<br/>\n\" );" );
     if ($useComments) {
         $templateFilename = $resourceData['template-filename'];
         if (file_exists($templateFilename)) {
             $fd = fopen($templateFilename, 'rb');
             if ($fd) {
                 $templateText = fread($fd, filesize($templateFilename));
                 $php->addComment("Original code:\n" . $templateText);
                 fclose($fd);
             }
         }
     }
     $php->addVariable('eZTemplateCompilerCodeDate', eZTemplateCompiler::CODE_DATE);
     $php->addCodePiece("if ( !defined( 'EZ_TEMPLATE_COMPILER_COMMON_CODE' ) )\n");
     $php->addInclude(eZTemplateCompiler::compilationDirectory() . '/common.php', eZPHPCreator::INCLUDE_ONCE_STATEMENT, array('spacing' => 4));
     $php->addSpace();
     if (eZTemplateCompiler::isAccumulatorsEnabled()) {
         $php->addCodePiece("eZDebug::accumulatorStart( 'template_compiled_execution', 'template_total', 'Template compiled execution', true );\n");
     }
     if (eZTemplateCompiler::isTimingPointsEnabled()) {
         $php->addCodePiece("eZDebug::addTimingPoint( 'Script start {$cacheFileName}' );\n");
     }
     //         $php->addCodePiece( "if ( !isset( \$vars ) )\n    \$vars =& \$tpl->Variables;\n" );
     //         $php->addSpace();
     $parameters = array();
     $textName = eZTemplateCompiler::currentTextName($parameters);
     //         $php->addCodePiece( "if ( !isset( \$$textName ) )\n    \$$textName = '';\n" );
     //         $php->addSpace();
     //         $variableStats = array();
     //         eZTemplateCompiler::prepareVariableStatistics( $tpl, $resourceData, $variableStats );
     //         eZTemplateCompiler::calculateVariableStatistics( $tpl, $rootNode, $resourceData, $variableStats );
     //         print_r( $variableStats );
     $transformedTree = array();
     eZTemplateCompiler::processNodeTransformation($useComments, $php, $tpl, $rootNode, $resourceData, $transformedTree);
     if ($ini->variable('TemplateSettings', 'TemplateOptimization') == 'enabled') {
         /* Retrieve class information for the attribute lookup table */
         if (isset($resourceData['handler']->Keys) and isset($resourceData['handler']->Keys['class'])) {
             $resourceData['class-info'] = eZTemplateOptimizer::fetchClassDeclaration($resourceData['handler']->Keys['class']);
         }
         /* Run the optimizations */
         eZTemplateOptimizer::optimize($useComments, $php, $tpl, $transformedTree, $resourceData);
     }
     $staticTree = array();
     eZTemplateCompiler::processStaticOptimizations($useComments, $php, $tpl, $transformedTree, $resourceData, $staticTree);
     $combinedTree = array();
     eZTemplateCompiler::processNodeCombining($useComments, $php, $tpl, $staticTree, $resourceData, $combinedTree);
     $finalTree = $combinedTree;
     if (!eZTemplateCompiler::isNodePlacementEnabled()) {
         eZTemplateCompiler::processRemoveNodePlacement($finalTree);
     }
     eZTemplateCompiler::generatePHPCode($useComments, $php, $tpl, $finalTree, $resourceData);
     if (eZTemplateCompiler::isTreeEnabled('final')) {
         $php->addVariable('finalTree', $finalTree, eZPHPCreator::VARIABLE_ASSIGNMENT, array('full-tree' => true));
     }
     if (eZTemplateCompiler::isTreeEnabled('combined')) {
         $php->addVariable('combinedTree', $combinedTree, eZPHPCreator::VARIABLE_ASSIGNMENT, array('full-tree' => true));
     }
     if (eZTemplateCompiler::isTreeEnabled('static')) {
         $php->addVariable('staticTree', $staticTree, eZPHPCreator::VARIABLE_ASSIGNMENT, array('full-tree' => true));
     }
     if (eZTemplateCompiler::isTreeEnabled('transformed')) {
         $php->addVariable('transformedTree', $transformedTree, eZPHPCreator::VARIABLE_ASSIGNMENT, array('full-tree' => true));
     }
     if (eZTemplateCompiler::isTreeEnabled('original')) {
         $php->addVariable('originalTree', $rootNode, eZPHPCreator::VARIABLE_ASSIGNMENT, array('full-tree' => true));
     }
     if (eZTemplateCompiler::isTimingPointsEnabled()) {
         $php->addCodePiece("eZDebug::addTimingPoint( 'Script end {$cacheFileName}' );\n");
     }
     if (eZTemplateCompiler::isAccumulatorsEnabled()) {
         $php->addCodePiece("eZDebug::accumulatorStop( 'template_compiled_execution', true );\n");
     }
     if ($resourceData['locales'] && count($resourceData['locales'])) {
         $php->addCodePiece('setlocale( LC_CTYPE, $oldLocale_' . $resourceData['uniqid'] . ' );' . "\n");
     }
     $php->addCodePiece('$setArray = $oldSetArray_' . $resourceData['uniqid'] . ";\n");
     // Code to decrement include level of the templates
     $php->addCodePiece("\$tpl->Level--;\n");
     /*
     // dump names of all defined PHP variables
     $php->addCodePiece( "echo \"defined vars in $resourceData[uri]:<br/><pre>\\n\";\n" );
     $php->addCodePiece( 'foreach ( array_keys( get_defined_vars() ) as $var_name  ) echo "- $var_name\n";' );
     // dump tpl vars
     $php->addCodePiece( 'echo "\n-----------------------------------------------------------\nvars: ";' );
     $php->addCodePiece( 'var_dump( $vars );' );
     $php->addCodePiece( 'echo "</pre><hr/>\n";' );
     */
     if (!$resourceData['test-compile']) {
         $php->store(true);
     }
     return true;
 }