smarty/internals/core.write_compiled_include.php
changeset 0 c63de9eb7045
equal deleted inserted replaced
-1:000000000000 0:c63de9eb7045
       
     1 <?php
       
     2 /**
       
     3  * Smarty plugin
       
     4  * @package Smarty
       
     5  * @subpackage plugins
       
     6  */
       
     7 
       
     8 /**
       
     9  * Extract non-cacheable parts out of compiled template and write it
       
    10  *
       
    11  * @param string $compile_path
       
    12  * @param string $template_compiled
       
    13  * @return boolean
       
    14  */
       
    15 
       
    16 function smarty_core_write_compiled_include($params, &$smarty)
       
    17 {
       
    18     $_tag_start = 'if \(\$this->caching && \!\$this->_cache_including\)\: echo \'\{nocache\:('.$params['cache_serial'].')#(\d+)\}\'; endif;';
       
    19     $_tag_end   = 'if \(\$this->caching && \!\$this->_cache_including\)\: echo \'\{/nocache\:(\\2)#(\\3)\}\'; endif;';
       
    20 
       
    21     preg_match_all('!('.$_tag_start.'(.*)'.$_tag_end.')!Us',
       
    22                    $params['compiled_content'], $_match_source, PREG_SET_ORDER);
       
    23     
       
    24     // no nocache-parts found: done
       
    25     if (count($_match_source)==0) return;
       
    26 
       
    27     // convert the matched php-code to functions
       
    28     $_include_compiled =  "<?php /* Smarty version ".$smarty->_version.", created on ".strftime("%Y-%m-%d %H:%M:%S")."\n";
       
    29     $_include_compiled .= "         compiled from " . strtr(urlencode($params['resource_name']), array('%2F'=>'/', '%3A'=>':')) . " */\n\n";
       
    30 
       
    31     $_compile_path = $params['include_file_path'];
       
    32 
       
    33     $smarty->_cache_serials[$_compile_path] = $params['cache_serial'];
       
    34     $_include_compiled .= "\$this->_cache_serials['".$_compile_path."'] = '".$params['cache_serial']."';\n\n?>";
       
    35 
       
    36     $_include_compiled .= $params['plugins_code'];
       
    37     $_include_compiled .= "<?php";
       
    38 
       
    39     $this_varname = ((double)phpversion() >= 5.0) ? '_smarty' : 'this';
       
    40     for ($_i = 0, $_for_max = count($_match_source); $_i < $_for_max; $_i++) {
       
    41         $_match =& $_match_source[$_i];
       
    42         $source = $_match[4];
       
    43         if ($this_varname == '_smarty') {
       
    44             /* rename $this to $_smarty in the sourcecode */
       
    45             $tokens = token_get_all('<?php ' . $_match[4]);
       
    46 
       
    47             /* remove trailing <?php */
       
    48             $open_tag = '';
       
    49             while ($tokens) {
       
    50                 $token = array_shift($tokens);
       
    51                 if (is_array($token)) {
       
    52                     $open_tag .= $token[1];
       
    53                 } else {
       
    54                     $open_tag .= $token;
       
    55                 }
       
    56                 if ($open_tag == '<?php ') break;
       
    57             }
       
    58 
       
    59             for ($i=0, $count = count($tokens); $i < $count; $i++) {
       
    60                 if (is_array($tokens[$i])) {
       
    61                     if ($tokens[$i][0] == T_VARIABLE && $tokens[$i][1] == '$this') {
       
    62                         $tokens[$i] = '$' . $this_varname;
       
    63                     } else {
       
    64                         $tokens[$i] = $tokens[$i][1];
       
    65                     }                   
       
    66                 }
       
    67             }
       
    68             $source = implode('', $tokens);
       
    69         }
       
    70 
       
    71         /* add function to compiled include */
       
    72         $_include_compiled .= "
       
    73 function _smarty_tplfunc_$_match[2]_$_match[3](&\$$this_varname)
       
    74 {
       
    75 $source
       
    76 }
       
    77 
       
    78 ";
       
    79     }
       
    80     $_include_compiled .= "\n\n?>\n";
       
    81 
       
    82     $_params = array('filename' => $_compile_path,
       
    83                      'contents' => $_include_compiled, 'create_dirs' => true);
       
    84 
       
    85     require_once(SMARTY_CORE_DIR . 'core.write_file.php');
       
    86     smarty_core_write_file($_params, $smarty);
       
    87     return true;
       
    88 }
       
    89 
       
    90 
       
    91 ?>