library/smarty/libs/sysplugins/smarty_internal_function_call_handler.php
changeset 46 f11c31f7fa3e
parent 45 a56e7f9a0463
child 47 03388ec805b4
equal deleted inserted replaced
45:a56e7f9a0463 46:f11c31f7fa3e
     1 <?php
       
     2 /**
       
     3  * Smarty Internal Plugin Function Call Handler
       
     4  *
       
     5  * @package    Smarty
       
     6  * @subpackage PluginsInternal
       
     7  * @author     Uwe Tews
       
     8  */
       
     9 
       
    10 /**
       
    11  * This class does handles template functions defined with the {function} tag missing in cache file.
       
    12  * It can happen when the template function was called with the nocache option or within a nocache section.
       
    13  * The template function will be loaded from it's compiled template file, executed and added to the cache file
       
    14  * for later use.
       
    15  *
       
    16  * @package    Smarty
       
    17  * @subpackage PluginsInternal
       
    18  */
       
    19 class Smarty_Internal_Function_Call_Handler {
       
    20     /**
       
    21      * This function handles calls to template functions defined by {function}
       
    22      * It does create a PHP function at the first call
       
    23      *
       
    24      * @param string $_name template function name
       
    25      * @param Smarty_Internal_Template $_smarty_tpl
       
    26      * @param string $_function PHP function name
       
    27      * @param array $_params Smarty variables passed as call parameter
       
    28      * @param bool $_nocache nocache flag
       
    29      *
       
    30      * @return bool
       
    31      */
       
    32     public static function call($_name, Smarty_Internal_Template $_smarty_tpl, $_function, $_params, $_nocache) {
       
    33         $funcParam = $_smarty_tpl->properties['tpl_function'][$_name];
       
    34         if (is_file($funcParam['compiled_filepath'])) {
       
    35             // read compiled file
       
    36             $code = file_get_contents($funcParam['compiled_filepath']);
       
    37             // grab template function
       
    38             if (preg_match("/\/\* {$_function} \*\/([\S\s]*?)\/\*\/ {$_function} \*\//", $code, $match)) {
       
    39                 // grab source info from file dependency
       
    40                 preg_match("/\s*'{$funcParam['uid']}'([\S\s]*?)\),/", $code, $match1);
       
    41                 unset($code);
       
    42                 $output = '';
       
    43                 // make PHP function known
       
    44                 eval($match[0]);
       
    45                 if (function_exists($_function)) {
       
    46                     // search cache file template
       
    47                     $tplPtr = $_smarty_tpl;
       
    48                     while (!isset($tplPtr->cached) && isset($tplPtr->parent)) {
       
    49                         $tplPtr = $tplPtr->parent;
       
    50                     }
       
    51                     // add template function code to cache file
       
    52                     if (isset($tplPtr->cached)) {
       
    53                         $cache = $tplPtr->cached;
       
    54                         $content = $cache->read($tplPtr);
       
    55                         if ($content) {
       
    56                             // check if we must update file dependency
       
    57                             if (!preg_match("/'{$funcParam['uid']}'([\S\s]*?)'nocache_hash'/", $content, $match2)) {
       
    58                                 $content = preg_replace("/('file_dependency'([\S\s]*?)\()/", "\\1{$match1[0]}", $content);
       
    59                             }
       
    60                             $cache->write($tplPtr, $content . "<?php " . $match[0] . "?>\n");
       
    61                         }
       
    62                     }
       
    63                     return true;
       
    64                 }
       
    65             }
       
    66         }
       
    67         return false;
       
    68     }
       
    69 }