Changeset 2604

Show
Ignore:
Timestamp:
12/06/07 13:17:29 (5 years ago)
Author:
tasuku
Message:

r332@dhcp158 (orig r75): tasuku | 2007-09-17 03:12:21 +0900
cache with include/extends properlly refreshed


Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/php/mumu/trunk/mumu.php

    r2603 r2604  
    206206} 
    207207 
    208 class MuNode { 
    209   public function _render() { 
    210     return ''; 
    211   } 
     208interface MuNode { 
     209  public function _render($context); 
    212210} 
    213211 
     
    229227} 
    230228 
    231 class MuErrorNode extends MuNode { 
     229class MuErrorNode implements MuNode { 
    232230  private $errorMsg; 
    233231  private $filename; 
     
    278276 
    279277// 1�ĤΥƥ�졼�Ȥ������������ 
    280 class MuFile extends MuNode { 
     278class MuFile implements MuNode { 
    281279  public $nodelist;        // �ե������ѡ�������NodeList 
    282   private $block_dict;      // nodelist�������ock̾ => MuBlockNode(�λ��� 
    283   private $parent_tfile;    // extends���������ƥƥ�졼��  function __construct($nodelist, $block_dict, $parentPath = null, $path = null) { 
     280  private $block_dict;     // nodelist�������ock̾ => MuBlockNode(�λ��� 
     281  public $include_paths;   // include�����ե��������ʥ�����������ǻȤ���  public $path;            // ��ʬ�Υե����� 
     282  private $parent_path;    // extends�Υե����� 
     283  private $parent_tfile;   // extends���������ƥƥ�졼��  function __construct($nodelist, $block_dict, $include_paths, 
     284                       $path = null, $parent_path = null) { 
    284285    $this->nodelist = $nodelist; 
    285286    $this->block_dict = $block_dict; 
    286     if ($parentPath && $path) { 
    287       if (($epath = MuUtil::getpath($path, $parentPath)) === false 
    288           || ($this->parent_tfile = MuParser::parse_from_file($epath)) === false) { 
     287    $this->include_paths = $include_paths; 
     288    $this->path = $path; 
     289    if ($parent_path && $path) { 
     290      if (($this->parent_path = MuUtil::getpath($path, $parent_path)) === false 
     291          || ($this->parent_tfile = MuParser::parse_from_file($this->parent_path)) === false) { 
    289292        throw new MuParserException('invalid filename specified on extends'); 
    290293      } 
     
    327330  } 
    328331  public function is_child() { 
    329     return ($parent_tfile !== false); 
    330   } 
    331 } 
    332  
    333 class MuTextNode extends MuNode { 
     332    return (isset($this->parent_tfile)); 
     333  } 
     334  // extends��clude���Ƥ��������뤬��������������Ȥ˹����������뤫 
     335  public function check_cache_mtime($cache_mtime) { 
     336    foreach ($this->include_paths as $inc_path) { 
     337      if ($cache_mtime < filemtime($inc_path)) { 
     338        return false; 
     339      } 
     340    } 
     341    if (isset($this->parent_path)) { 
     342      if ($cache_mtime < filemtime($this->parent_path)) { 
     343        return false; 
     344      } 
     345      // check parent 
     346      return $this->parent_tfile->check_cache_mtime($cache_mtime); 
     347    } 
     348    return true; 
     349  } 
     350} 
     351 
     352class MuTextNode implements MuNode { 
    334353  private $text; 
    335354  function __construct($text) { 
     
    341360} 
    342361 
    343 class MuVariableNode extends MuNode { 
     362class MuVariableNode implements MuNode { 
    344363  private $filter_expression; 
    345364  function __construct($filter_expression) { 
     
    356375} 
    357376 
    358 class MuIncludeNode extends MuNode { 
     377class MuIncludeNode implements MuNode { 
    359378  private $tplfile; 
    360   function __construct($includePath, $path) { 
    361     if (($epath = MuUtil::getpath($path, $includePath)) === false 
     379  function __construct($include_path, $path) { 
     380    if (($epath = MuUtil::getpath($path, $include_path)) === false 
    362381        || ($this->tplfile = MuParser::parse_from_file($epath)) === false) { 
    363382      throw new MuParserException('include filename is invalid'); 
     
    367386    return $this->tplfile->_render($context); 
    368387  } 
    369 } 
    370  
    371 class MuBlockNode extends MuNode { 
     388  public function get_tplfile() { 
     389    return $this->tplfile; 
     390  } 
     391} 
     392 
     393class MuBlockNode implements MuNode { 
    372394  public $name; 
    373395  public $nodelist; 
     
    404426} 
    405427 
    406 class MuCycleNode extends MuNode { 
     428class MuCycleNode implements MuNode { 
    407429  private $cyclevars; 
    408430  private $cyclevars_len; 
     
    426448} 
    427449 
    428 class MuDebugNode extends MuNode { 
     450class MuDebugNode implements MuNode { 
    429451  function _render($context) { 
    430452    ob_start(); 
     
    445467} 
    446468 
    447 class MuFilterNode extends MuNode { 
     469class MuFilterNode implements MuNode { 
    448470  private $filter_expr; 
    449471  private $nodelist; 
     
    465487} 
    466488 
    467 class MuForNode extends MuNode { 
     489class MuForNode implements MuNode { 
    468490  private $loopvar; 
    469491  private $sequence; 
     
    513535} 
    514536 
    515 class MuIfNode extends MuNode { 
     537class MuIfNode implements MuNode { 
    516538  private $bool_exprs; 
    517539  private $nodelist_true; 
     
    559581} 
    560582 
    561 class MuIfEqualNode extends MuNode { 
     583class MuIfEqualNode implements MuNode { 
    562584  private $var1; 
    563585  private $var2; 
     
    589611} 
    590612 
    591 class MuSpacelessNode extends MuNode { 
     613class MuSpacelessNode implements MuNode { 
    592614  private $nodelist; 
    593615  function __construct($nodelist) { 
     
    599621} 
    600622 
    601 class MuTemplateTagNode extends MuNode { 
     623class MuTemplateTagNode implements MuNode { 
    602624  private $tagtype; 
    603625  // TODO: php5 don't support array as const... 
     
    620642} 
    621643 
    622 class MuWidthRatioNode extends MuNode { 
     644class MuWidthRatioNode implements MuNode { 
    623645  private $val_expr; 
    624646  private $max_expr; 
     
    643665} 
    644666 
    645 class MuNowNode extends MuNode { 
     667class MuNowNode implements MuNode { 
    646668  private $format_string; 
    647669  function __construct($format_string) { 
     
    653675} 
    654676 
    655 class MuFirstOfNode extends MuNode { 
     677class MuFirstOfNode implements MuNode { 
    656678  private $vars; 
    657679  function __construct($vars) { 
     
    974996 
    975997class MuParser { 
    976   private $template;             // �ѡ�����Υƥ�졼����� private $template_len;         // �ƥ�졼�����Ĺ�� 
    977   private $template_path;         // �ƥ�졼�ȤΥѥ�(����) 
    978   private $block_dict = array(); // block���� => block�ؤλ���  private $extends = false;      // extends�ξ��Υե����� 
    979   private $spos = 0;             // ���ߥѡ�����ΰ�� 
     998  private $template;                // �ѡ�����Υƥ�졼����� private $template_len;            // �ƥ�졼�����Ĺ�� 
     999  private $template_path;           // �ƥ�졼�ȤΥѥ�(����) 
     1000  private $serialize_path;          // �ѡ����ѤߤΥƥ�졼�Ȥ򥷥ꥢ�饤����������ݴ�ath 
     1001  private $block_dict = array();    // block���� => block�ؤλ���  private $extends;                 // extends�Υե����� 
     1002  private $include_paths = array(); // include���Ƥ����������ΰ� 
     1003  private $spos = 0;                // ���ߥѡ�����ΰ�� 
    9801004  # template syntax constants 
    9811005  const FILTER_SEPARATOR = '|'; 
     
    9921016  // FIXME: ����������Ǥ������ѡ�����������äƤޤ� 
    9931017 
    994   function __construct($template, $template_path = null) { 
     1018  function __construct($template, $template_path = null, $serialize_path = null) { 
    9951019    $this->template = $template; 
    9961020    $this->template_len = strlen($template); 
    9971021    $this->template_path = $template_path; 
     1022    $this->serialize_path = $serialize_path; 
    9981023  } 
    9991024 
     
    11341159    switch ($in[0]) { 
    11351160      // TODO: ������å����      case 'extends': 
    1136         if ($this->extends !== false) { 
     1161        if (isset($this->extends)) { 
    11371162          return $this->make_errornode('multiple_extends_tag'); 
    11381163        } 
     
    11571182        $this->spos = $lpos + 2; 
    11581183        $node = new MuIncludeNode($param[1], $this->template_path); 
     1184        $tplfile = $node->get_tplfile(); 
     1185        $this->include_paths[] = $tplfile->path; 
     1186        unset($tplfile->path); 
     1187        // ���󥯥롼�ɤΥ��󥯥롼��褬�������줿�饭���������� 
     1188        if (isset($tplfile->include_paths)) { 
     1189          $this->include_paths = array_merge($this->include_paths, $tplfile->include_paths); 
     1190          unset($tplfile->include_paths); 
     1191        } 
    11591192        break; 
    11601193      case 'block': // endblock 
     
    13611394  } 
    13621395 
    1363   static public function parse_from_file($template_path, $serialize_path = null) { 
    1364     if (($t = file_get_contents($template_path)) === false) { 
    1365       return false; 
    1366     } 
    1367     // check cache 
    1368     $cachePath = $template_path.'.cache'; 
    1369     if ($useSerialize && 
    1370         file_exists($cachePath) && 
    1371         filemtime($template_path) <= filemtime($cachePath)) { 
    1372       $mf = MuUtil::unserialize_from_file($cachePath); 
    1373     } else { 
    1374       $p = new MuParser($t, $template_path); 
     1396  static public function parse_from_file($template_path, $serialize_store = null) { 
     1397    // ������������å� 
     1398    if (isset($serialize_store)) { 
     1399      if (stristr($serialize_store, 'file://') == 0) { 
     1400        // TODO: check whether absolute path or not 
     1401        if (($spath = realpath(substr($serialize_store, 7))) === false) { 
     1402          return false; 
     1403        } 
     1404        $sfpath = $spath . '/' . basename($template_path) . '.cache'; 
     1405        if (file_exists($sfpath)) { 
     1406          if (($sfmtime = filemtime($sfpath)) === false) { 
     1407            return false; 
     1408          } 
     1409          if (filemtime($template_path) <= $sfmtime) { 
     1410            $mf = MuUtil::unserialize_from_file($sfpath); 
     1411            if (!$mf->check_cache_mtime($sfmtime)) { 
     1412              unset($mf); 
     1413            } 
     1414          } 
     1415        } 
     1416      } else { 
     1417        // TODO: now file cache only 
     1418        return false; 
     1419      } 
     1420    } 
     1421    if (!isset($mf)) { 
     1422      if (($t = file_get_contents($template_path)) === false) { 
     1423        return false; 
     1424      } 
     1425      $p = new MuParser($t, $template_path, $serialize_path); 
    13751426      try { 
    13761427        list($nl) = $p->_parse(array()); 
     
    13791430        $nl->push($p->make_errornode($e->getMessage())); 
    13801431      } 
    1381       $mf = new MuFile($nl, $p->block_dict, $p->extends, $template_path); 
    1382       MuUtil::serialize_to_file($mf, $cachePath); 
     1432      $mf = new MuFile($nl, $p->block_dict, $p->include_paths, $template_path, $p->extends); 
     1433      if (isset($sfpath)) { 
     1434        MuUtil::serialize_to_file($mf, $sfpath); 
     1435      } 
    13831436    } 
    13841437    return $mf; 
     
    13931446      $nl->push($p->make_errornode($e->getMessage())); 
    13941447    } 
    1395     return new MuFile($nl, $p->block_dict); 
     1448    return new MuFile($nl, $p->block_dict, $p->include_paths); 
    13961449  } 
    13971450