Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Patch+ideas to improve smarty's "static" vars mgmt

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Bugs
View previous topic :: View next topic  
Author Message
sylvinus
Smarty Rookie


Joined: 28 Oct 2005
Posts: 8

PostPosted: Mon Nov 06, 2006 1:19 pm    Post subject: Patch+ideas to improve smarty's "static" vars mgmt Reply with quote

Hi !

I've filled a bug report here http://www.phpinsider.com/smarty-forum/viewtopic.php?p=36155#36155

The issue is that when you reinit smarty a second time in the same script, the "static" vars don't get initialized and some strange behaviour happens.

There are also other more "real" problems related to static php vars, like :
Code:

   $this->t->clear_cache("test/dynamic.tpl");

   //returns true !!
   $this->t->is_cached("test/dynamic.tpl");



I think in order to improve the code you should group all the static vars in one function in Smarty.class.php :

Code:

    /**
     * returns or resets a static variable
     *
     * @param string $var_name
     * @param boolean $reset
     */
   function &_static_var($var_name,$reset=false) {
     
      static $cache=array();
     
      if ($reset) {
          if ($var_name===null) {
                $cache=array();
          } else {
                $cache[$var_name]=null;
         }
      }
      return $cache[$var_name];
     
   }


Then patch all the static declarations, like :

in Smarty.class.php:
Code:

   function fetch($resource_name, $cache_id = null, $compile_id = null, $display = false)
    {
        $_cache_info =& $this->_static_var("fetch _cache_info");
        if ($_cache_info===null) {
           $_cache_info=array();
        }
 


in core.read_cache_file.php:
Code:

function smarty_core_read_cache_file(&$params, &$smarty)
{
    $content_cache =& $smarty->_static_var("smarty_core_read_cache_file content_cache");
    if ($content_cache===null) {
       $content_cache=array();
    }


Then you patch the smarty constructor to reinit those variables :

Code:

    function Smarty()
    {
      $this->assign('SCRIPT_NAME', isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME']
                    : @$GLOBALS['HTTP_SERVER_VARS']['SCRIPT_NAME']);
     
      //reinit all static variables
      $this->_static_var(null,true);
    }


That fixes the "reinit smarty" bug

Then to fix the clear_cache + is_cached behaviour, we'll need something like :

in Smarty.class.php :
Code:

    function clear_cache($tpl_file = null, $cache_id = null, $compile_id = null, $exp_time = null)
    {

        if (!isset($compile_id))
            $compile_id = $this->compile_id;

        if (!isset($tpl_file))
            $compile_id = null;

        $_auto_id = $this->_get_auto_id($cache_id, $compile_id);

      // If we don't delete the cache_content, is_cached will return true until the end of the current script

      //functional patch : remove everything each time
      $cache_content =& $this->_static_var("smarty_core_read_cache_file content_cache");
      $cache_content=array();


This last patch is not very accurate, someone more familiar with smarty's internals could improve it.

This kind of things makes me wonder why shouldn't we make _static_var not use "static" anymore but an instance variable to store the value. This would be cleaner in all the ways I can think of. Just use a $smarty->_cache_content value and don't bother with static vars ?

I'd be happy to receive the comments of the devs on this issue !


Last edited by sylvinus on Wed Nov 08, 2006 10:59 am; edited 1 time in total
Back to top
View user's profile Send private message
sylvinus
Smarty Rookie


Joined: 28 Oct 2005
Posts: 8

PostPosted: Wed Nov 08, 2006 10:58 am    Post subject: Reply with quote

For the record, I've been using this patch in production for a few days, it works just ok.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Bugs All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP