Chris PeBenito 696b41
Chris PeBenito 696b41
/**
Chris PeBenito 696b41
 * Smarty plugin
Chris PeBenito 696b41
 * @package Smarty
Chris PeBenito 696b41
 * @subpackage plugins
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
Chris PeBenito 696b41
/**
Chris PeBenito 696b41
 * Extract non-cacheable parts out of compiled template and write it
Chris PeBenito 696b41
 *
Chris PeBenito 696b41
 * @param string $compile_path
Chris PeBenito 696b41
 * @param string $template_compiled
Chris PeBenito 696b41
 * @return boolean
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
Chris PeBenito 696b41
function smarty_core_write_compiled_include($params, &$smarty)
Chris PeBenito 696b41
{
Chris PeBenito 696b41
    $_tag_start = 'if \(\$this->caching && \!\$this->_cache_including\) \{ echo \'\{nocache\:('.$params['cache_serial'].')#(\d+)\}\';\}';
Chris PeBenito 696b41
    $_tag_end   = 'if \(\$this->caching && \!\$this->_cache_including\) \{ echo \'\{/nocache\:(\\2)#(\\3)\}\';\}';
Chris PeBenito 696b41
Chris PeBenito 696b41
    preg_match_all('!('.$_tag_start.'(.*)'.$_tag_end.')!Us',
Chris PeBenito 696b41
                   $params['compiled_content'], $_match_source, PREG_SET_ORDER);
Chris PeBenito 696b41
Chris PeBenito 696b41
    // no nocache-parts found: done
Chris PeBenito 696b41
    if (count($_match_source)==0) return;
Chris PeBenito 696b41
Chris PeBenito 696b41
    // convert the matched php-code to functions
Chris PeBenito 696b41
    $_include_compiled =  "_version.", created on ".strftime("%Y-%m-%d %H:%M:%S")."\n";
Chris PeBenito 696b41
    $_include_compiled .= "         compiled from " . strtr(urlencode($params['resource_name']), array('%2F'=>'/', '%3A'=>':')) . " */\n\n";
Chris PeBenito 696b41
Chris PeBenito 696b41
    $_compile_path = $params['include_file_path'];
Chris PeBenito 696b41
Chris PeBenito 696b41
    $smarty->_cache_serials[$_compile_path] = $params['cache_serial'];
Chris PeBenito 696b41
    $_include_compiled .= "\$this->_cache_serials['".$_compile_path."'] = '".$params['cache_serial']."';\n\n?>";
Chris PeBenito 696b41
Chris PeBenito 696b41
    $_include_compiled .= $params['plugins_code'];
Chris PeBenito 696b41
    $_include_compiled .= "
Chris PeBenito 696b41
Chris PeBenito 696b41
    $this_varname = ((double)phpversion() >= 5.0) ? '_smarty' : 'this';
Chris PeBenito 696b41
    for ($_i = 0, $_for_max = count($_match_source); $_i < $_for_max; $_i++) {
Chris PeBenito 696b41
        $_match =& $_match_source[$_i];
Chris PeBenito 696b41
        $source = $_match[4];
Chris PeBenito 696b41
        if ($this_varname == '_smarty') {
Chris PeBenito 696b41
            /* rename $this to $_smarty in the sourcecode */
Chris PeBenito 696b41
            $tokens = token_get_all('
Chris PeBenito 696b41
Chris PeBenito 696b41
            /* remove trailing 
Chris PeBenito 696b41
            $open_tag = '';
Chris PeBenito 696b41
            while ($tokens) {
Chris PeBenito 696b41
                $token = array_shift($tokens);
Chris PeBenito 696b41
                if (is_array($token)) {
Chris PeBenito 696b41
                    $open_tag .= $token[1];
Chris PeBenito 696b41
                } else {
Chris PeBenito 696b41
                    $open_tag .= $token;
Chris PeBenito 696b41
                }
Chris PeBenito 696b41
                if ($open_tag == '
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
Chris PeBenito 696b41
            for ($i=0, $count = count($tokens); $i < $count; $i++) {
Chris PeBenito 696b41
                if (is_array($tokens[$i])) {
Chris PeBenito 696b41
                    if ($tokens[$i][0] == T_VARIABLE && $tokens[$i][1] == '$this') {
Chris PeBenito 696b41
                        $tokens[$i] = '$' . $this_varname;
Chris PeBenito 696b41
                    } else {
Chris PeBenito 696b41
                        $tokens[$i] = $tokens[$i][1];
Chris PeBenito 696b41
                    }                   
Chris PeBenito 696b41
                }
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
            $source = implode('', $tokens);
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
Chris PeBenito 696b41
        /* add function to compiled include */
Chris PeBenito 696b41
        $_include_compiled .= "
Chris PeBenito 696b41
function _smarty_tplfunc_$_match[2]_$_match[3](&\$$this_varname)
Chris PeBenito 696b41
{
Chris PeBenito 696b41
$source
Chris PeBenito 696b41
}
Chris PeBenito 696b41
Chris PeBenito 696b41
";
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
    $_include_compiled .= "\n\n?>\n";
Chris PeBenito 696b41
Chris PeBenito 696b41
    $_params = array('filename' => $_compile_path,
Chris PeBenito 696b41
                     'contents' => $_include_compiled, 'create_dirs' => true);
Chris PeBenito 696b41
Chris PeBenito 696b41
    require_once(SMARTY_CORE_DIR . 'core.write_file.php');
Chris PeBenito 696b41
    smarty_core_write_file($_params, $smarty);
Chris PeBenito 696b41
    return true;
Chris PeBenito 696b41
}
Chris PeBenito 696b41
Chris PeBenito 696b41
Chris PeBenito 696b41
?>