Development

Changeset 1300

You must first sign up to be able to contribute.

Changeset 1300

Show
Ignore:
Timestamp:
05/03/06 09:03:12 (3 years ago)
Author:
fabien
Message:

* refactored partial and component support (now it instanciates a view as for actions)
* removed duplicated code
* output escaping support for partial and components
* partial and component cache (closes #406)
* fixed headers/css/js when caching an action
* fixed debug decoration
* added new helpers: get_component_slot(), get_component() and get_partial()
* added parameters to include_component_slot()
* refactored ignore cache parameter in sfWebDebug

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/action/sfAction.class.php

    r1292 r1300  
    101101    } 
    102102 
    103     // ignore cache? (only in debug mode) 
    104     if (sfConfig::get('sf_debug') && $this->request->getParameter('ignore_cache', false, 'symfony/request/sfWebRequest') == true) 
    105     { 
    106       return 1; 
    107     } 
    108  
    109103    $cache = $this->getContext()->getViewCacheManager(); 
    110104 
  • trunk/lib/config/sfCacheConfigHandler.class.php

    r1044 r1300  
    6464                "{\n"; 
    6565 
    66       if ($this->getConfigValue('activate', $actionName)) 
    67       { 
    68         $data[] = $this->addCache($actionName); 
    69       } 
     66      $data[] = $this->addCache($actionName); 
    7067 
    7168      $data[] = "}\n"; 
     
    7572 
    7673    // general cache configuration 
    77     if ($this->getConfigValue('activate', $actionName)) 
    78     { 
    79       $data[] = ($first ? '' : "else\n{")."\n"; 
    80       $data[] = $this->addCache('DEFAULT'); 
    81       $data[] = ($first ? '' : "}")."\n"; 
    82     } 
     74    $data[] = ($first ? '' : "else\n{")."\n"; 
     75    $data[] = $this->addCache('DEFAULT'); 
     76    $data[] = ($first ? '' : "}")."\n"; 
    8377 
    8478    // compile data 
     
    9589    $data = array(); 
    9690 
     91    // activated? 
     92    $activate = $this->getConfigValue('activate', $actionName); 
     93 
    9794    // cache type for this action (slot or page) 
    9895    $type = $this->getConfigValue('type', $actionName); 
    9996 
    10097    // lifetime 
    101     $lifeTime = $this->getConfigValue('lifetime', $actionName); 
     98    $lifeTime = (!$activate) ? 0 : $this->getConfigValue('lifetime', $actionName); 
    10299 
    103100    // client_lifetime 
    104     $clientLifetime = $this->getConfigValue('client_lifetime', $actionName, $lifeTime); 
     101    $clientLifetime = (!$activate) ? 0 : $this->getConfigValue('client_lifetime', $actionName, $lifeTime); 
    105102 
    106103    // vary 
     
    112109 
    113110    // add cache information to cache manager 
    114     $data[] = sprintf("  \$this->cacheManager->addCache(\$context->getModuleName(), '%s', '%s', %s, '%s', %s);\n\n", 
     111    $data[] = sprintf("  \$cacheManager->addCache(\$context->getModuleName(), '%s', '%s', %s, '%s', %s);\n\n", 
    115112                      $actionName, $type, $lifeTime, $clientLifetime, var_export($vary, true)); 
    116113 
  • trunk/lib/controller/sfController.class.php

    r1295 r1300  
    466466        $class = $moduleClass; 
    467467      } 
    468  
    469       return new $class(); 
    470     } 
    471  
    472     // view class (as configured in module.yml or defined in action) 
    473     $viewName = $this->getContext()->getRequest()->getAttribute($moduleName.'_'.$actionName.'_view_name', '', 'symfony/action/view') ? $this->getContext()->getRequest()->getAttribute($moduleName.'_'.$actionName.'_view_name', '', 'symfony/action/view') : sfConfig::get('mod_'.strtolower($moduleName).'_view_class'); 
    474     $file     = sfConfig::get('sf_symfony_lib_dir').'/view/'.$viewName.'View.class.php'; 
    475     if (is_readable($file)) 
    476     { 
    477       $class = $viewName.'View'; 
    478  
    479       return new $class(); 
    480     } 
    481  
    482     $class = 'sfPHPView'; 
     468    } 
     469    else 
     470    { 
     471      // view class (as configured in module.yml or defined in action) 
     472      $viewName = $this->getContext()->getRequest()->getAttribute($moduleName.'_'.$actionName.'_view_name', '', 'symfony/action/view') ? $this->getContext()->getRequest()->getAttribute($moduleName.'_'.$actionName.'_view_name', '', 'symfony/action/view') : sfConfig::get('mod_'.strtolower($moduleName).'_view_class'); 
     473      $file     = sfConfig::get('sf_symfony_lib_dir').'/view/'.$viewName.'View.class.php'; 
     474      $class    = is_readable($file) ? $viewName.'View' : 'sfPHPView'; 
     475    } 
    483476 
    484477    return new $class(); 
  • trunk/lib/debug/sfWebDebug.class.php

    r1296 r1300  
    376376  } 
    377377 
    378   public function decorateContentWithDebug($internalUri, $suffix, $retval, $border_color, $bg_color
     378  public function decorateContentWithDebug($internalUri, $suffix, $retval, $new = false
    379379  { 
    380380    if (!sfConfig::get('sf_web_debug')) 
     
    382382      return $retval; 
    383383    } 
     384 
     385    $border_color = $new ? '#f00' : '#f00'; 
     386    $bg_color     = $new ? '#9ff' : '#ff9'; 
    384387 
    385388    $cache = $this->context->getViewCacheManager(); 
  • trunk/lib/filter/sfCacheFilter.class.php

    r1279 r1300  
    5757      if (is_readable(sfConfig::get('sf_app_module_dir').'/'.$cacheConfigFile)) 
    5858      { 
    59         $actionName = $context->getActionName(); 
     59        $actionName   = $context->getActionName(); 
     60        $cacheManager = $this->cacheManager; 
    6061        require(sfConfigCache::getInstance()->checkConfig(sfConfig::get('sf_app_module_dir_name').'/'.$cacheConfigFile)); 
    6162      } 
     
    6465      list($uri, $suffix) = $this->cacheManager->getInternalUri('page'); 
    6566      $this->toSave[$uri.'_'.$suffix] = false; 
    66       if ($this->cacheManager->hasCacheConfig($uri, $suffix)) 
     67      if ($this->cacheManager->isCacheable($uri, $suffix)) 
    6768      { 
    6869        $inCache = $this->getPageCache($uri, $suffix); 
     
    7980        list($uri, $suffix) = $this->cacheManager->getInternalUri('slot'); 
    8081        $this->toSave[$uri.'_'.$suffix] = false; 
    81         if ($this->cacheManager->hasCacheConfig($uri, $suffix)) 
     82        if ($this->cacheManager->isCacheable($uri, $suffix)) 
    8283        { 
    8384          $inCache = $this->getActionCache($uri, $suffix); 
     
    130131 
    131132          $this->setPageCache($uri, $suffix); 
     133 
     134          if (sfConfig::get('sf_web_debug')) 
     135          { 
     136            $content = sfWebDebug::getInstance()->decorateContentWithDebug($uri, $suffix, $this->response->getContent(), true); 
     137            $this->response->setContent($content); 
     138          } 
    132139        } 
    133140 
     
    157164    $this->cacheManager->set(serialize($this->response), $uri, $suffix); 
    158165 
    159     if (sfConfig::get('sf_web_debug')) 
    160     { 
    161       $content = sfWebDebug::getInstance()->decorateContentWithDebug($uri, $suffix, $this->response->getContent(), '#f00', '#9ff'); 
    162       $this->response->setContent($content); 
    163     } 
    164  
    165166    if (sfConfig::get('sf_logging_active')) 
    166167    { 
     
    172173  { 
    173174    $context = $this->getContext(); 
    174  
    175     // ignore cache? 
    176     if (sfConfig::get('sf_debug') && $this->request->getParameter('ignore_cache', false, 'symfony/request/sfWebRequest') == true) 
    177     { 
    178       if (sfConfig::get('sf_logging_active')) 
    179       { 
    180         $context->getLogger()->info('{sfCacheFilter} discard page cache for "'.$uri.' - '.$suffix.'"'); 
    181       } 
    182  
    183       return false; 
    184     } 
    185175 
    186176    // get the current action information 
     
    195185    } 
    196186 
    197     if ($retval !== null) 
    198     { 
    199       $cachedResponse = unserialize($retval)
    200       $cachedResponse->setContext($context); 
    201  
    202       $controller = $context->getController(); 
    203       if ($controller->getRenderMode() == sfView::RENDER_VAR) 
    204       { 
    205         $controller->getActionStack()->getLastEntry()->setPresentation($cachedResponse->getContent()); 
    206         $this->response->setContent(''); 
    207       } 
    208       else 
    209       { 
    210         $context->setResponse($cachedResponse); 
    211  
    212         if (sfConfig::get('sf_web_debug')) 
    213         { 
    214           $content = sfWebDebug::getInstance()->decorateContentWithDebug($uri, $suffix, $cachedResponse->getContent(), '#f00', '#ff9'); 
    215           $this->response->setContent($content); 
    216         } 
    217       } 
    218  
    219       return true; 
    220     } 
    221  
    222     return false; 
     187    if ($retval === null) 
     188    { 
     189      return false
     190    } 
     191 
     192    $cachedResponse = unserialize($retval); 
     193    $cachedResponse->setContext($context); 
     194 
     195    $controller = $context->getController(); 
     196    if ($controller->getRenderMode() == sfView::RENDER_VAR) 
     197    { 
     198      $controller->getActionStack()->getLastEntry()->setPresentation($cachedResponse->getContent()); 
     199      $this->response->setContent(''); 
     200    } 
     201    else 
     202    { 
     203      $context->setResponse($cachedResponse); 
     204 
     205      if (sfConfig::get('sf_web_debug')) 
     206      { 
     207        $content = sfWebDebug::getInstance()->decorateContentWithDebug($uri, $suffix, $cachedResponse->getContent(), false); 
     208        $context->getResponse()->setContent($content); 
     209      } 
     210    } 
     211 
     212    return true; 
    223213  } 
    224214 
     
    226216  { 
    227217    $content = $this->response->getParameter($uri.'_'.$suffix, null, 'symfony/cache'); 
    228     $this->cacheManager->set($content, $uri, $suffix); 
     218 
     219    if ($content !== null) 
     220    { 
     221      $cached = $this->cacheManager->set($content, $uri, $suffix); 
     222    } 
     223 
     224    if (sfConfig::get('sf_logging_active') && $cached) 
     225    { 
     226      $this->getContext()->getLogger()->info('{sfCacheFilter} save slot "'.$uri.' - '.$suffix.'" in cache'); 
     227    } 
     228  } 
     229 
     230  private function getActionCache($uri, $suffix) 
     231  { 
     232    // retrieve content from cache 
     233    $retval = $this->cacheManager->get($uri, $suffix); 
     234 
     235    if (sfConfig::get('sf_web_debug') && $retval) 
     236    { 
     237      $tmp = unserialize($retval); 
     238      $tmp['content'] = sfWebDebug::getInstance()->decorateContentWithDebug($uri, $suffix, $tmp['content'], false); 
     239      $retval = serialize($tmp); 
     240    } 
     241 
     242    $this->response->setParameter('current_key', $uri.'_'.$suffix, 'symfony/cache/current'); 
     243    $this->response->setParameter($uri.'_'.$suffix, $retval, 'symfony/cache'); 
    229244 
    230245    if (sfConfig::get('sf_logging_active')) 
    231246    { 
    232       $this->getContext()->getLogger()->info('{sfCacheFilter} save slot "'.$uri.' - '.$suffix.'" in cache'); 
    233     } 
    234   } 
    235  
    236   private function getActionCache($uri, $suffix) 
    237   { 
    238     // ignore cache parameter? (only available in debug mode) 
    239     if (sfConfig::get('sf_debug') && $this->request->getParameter('ignore_cache', false, 'symfony/request/sfWebRequest') == true) 
    240     { 
    241       if (sfConfig::get('sf_logging_active')) 
    242       { 
    243         $this->getContext()->getLogger()->info('{sfCacheFilter} discard cache for "'.$uri.'" / '.$suffix.''); 
    244       } 
    245     } 
    246     else 
    247     { 
    248       // retrieve content from cache 
    249       $retval = $this->cacheManager->get($uri, $suffix); 
    250  
    251       if ($retval) 
    252       { 
    253         $this->response->setParameter($uri.'_'.$suffix, $retval, 'symfony/cache'); 
    254       } 
    255  
    256       if (sfConfig::get('sf_logging_active')) 
    257       { 
    258         $this->getContext()->getLogger()->info('{sfCacheFilter} cache for "'.$uri.' - '.$suffix.'" '.($retval !== null ? 'exists' : 'does not exist')); 
    259       } 
    260  
    261       return ($retval ? true : false); 
    262     } 
    263  
    264     return false; 
     247      $this->getContext()->getLogger()->info('{sfCacheFilter} cache for "'.$uri.' - '.$suffix.'" '.($retval !== null ? 'exists' : 'does not exist')); 
     248    } 
     249 
     250    return ($retval ? true : false); 
    265251  } 
    266252} 
  • trunk/lib/helper/PartialHelper.php

    r1132 r1300  
    1818 */ 
    1919 
    20 function include_component_slot($name) 
     20function include_component_slot($name, $vars = array()) 
     21
     22  echo get_component_slot($name, $vars); 
     23
     24 
     25function get_component_slot($name, $vars = array()) 
    2126{ 
    2227  $context = sfContext::getInstance(); 
     
    3641  if ($componentSlot = $viewInstance->getComponentSlot($name)) 
    3742  { 
    38     include_component($componentSlot[0], $componentSlot[1]); 
     43    return get_component($componentSlot[0], $componentSlot[1], $vars); 
    3944  } 
    4045} 
     
    4247function include_component($moduleName, $componentName, $vars = array()) 
    4348{ 
    44   $context    = sfContext::getInstance(); 
     49  echo get_component($moduleName, $componentName, $vars); 
     50
     51 
     52function get_component($moduleName, $componentName, $vars = array()) 
     53
     54  $context      = sfContext::getInstance(); 
     55  $cacheManager = $context->getViewCacheManager(); 
     56 
     57  if (sfConfig::get('sf_cache')) 
     58  { 
     59    $actionName = '_'.$componentName; 
     60    $cacheKey = md5(serialize($vars)); 
     61    $uri = $moduleName.'/'.$actionName.'?key='.$cacheKey; 
     62 
     63    // register our cache configuration 
     64    $cacheConfigFile = $moduleName.'/'.sfConfig::get('sf_app_module_config_dir_name').'/cache.yml'; 
     65    if (is_readable(sfConfig::get('sf_app_module_dir').'/'.$cacheConfigFile)) 
     66    { 
     67      require(sfConfigCache::getInstance()->checkConfig(sfConfig::get('sf_app_module_dir_name').'/'.$cacheConfigFile)); 
     68    } 
     69 
     70    $retval = $cacheManager->get($uri, 'slot'); 
     71 
     72    if (sfConfig::get('sf_logging_active') && $cacheManager->isCacheable($uri, 'slot')) 
     73    { 
     74      $context->getLogger()->info(sprintf('{PartialHelper} cache for "%s" %s', $uri, ($retval !== null ? 'exists' : 'does not exist'))); 
     75    } 
     76 
     77    if ($retval !== null) 
     78    { 
     79      if (sfConfig::get('sf_web_debug')) 
     80      { 
     81        $retval = sfWebDebug::getInstance()->decorateContentWithDebug($uri, 'slot', $retval, false); 
     82      } 
     83 
     84      return $retval; 
     85    } 
     86  } 
     87 
    4588  $controller = $context->getController(); 
    4689 
     
    58101 
    59102  // initialize the action 
    60   if ($componentInstance->initialize($context)) 
    61   { 
    62     $componentInstance->getVarHolder()->add($vars); 
    63  
    64     // dispatch component 
    65     $componentToRun = 'execute'.ucfirst($componentName); 
    66     if (!method_exists($componentInstance, $componentToRun)) 
    67     { 
    68       if (method_exists($componentInstance, 'execute')) 
    69       { 
    70         $componentToRun = 'execute'; 
    71       } 
    72       else 
    73       { 
    74         // component not found 
    75         $error = 'sfComponent initialization failed for module "%s", component "%s"'; 
    76         $error = sprintf($error, $moduleName, $componentName); 
    77         throw new sfInitializationException($error); 
    78       } 
    79     } 
    80  
    81     if (sfConfig::get('sf_logging_active')) $context->getLogger()->info('{sfComponent} call "'.$moduleName.'->'.$componentToRun.'()'.'"'); 
    82  
    83     // run component 
    84     $retval = $componentInstance->$componentToRun(); 
    85  
    86     if ($retval != sfView::NONE) 
    87     { 
    88       // get component vars 
    89       $componentVars = $componentInstance->getVarHolder()->getAll(); 
    90  
    91       // include partial 
    92       include_partial($moduleName.'/'.$componentName, $componentVars); 
    93     } 
    94   } 
    95   else 
     103  if (!$componentInstance->initialize($context)) 
    96104  { 
    97105    // component failed to initialize 
     
    101109    throw new sfInitializationException($error); 
    102110  } 
    103 
    104  
    105 function include_partial($name, $vars = array()) 
    106 
     111 
     112  $componentInstance->getVarHolder()->add($vars); 
     113 
     114  // dispatch component 
     115  $componentToRun = 'execute'.ucfirst($componentName); 
     116  if (!method_exists($componentInstance, $componentToRun)) 
     117  { 
     118    if (!method_exists($componentInstance, 'execute')) 
     119    { 
     120      // component not found 
     121      $error = 'sfComponent initialization failed for module "%s", component "%s"'; 
     122      $error = sprintf($error, $moduleName, $componentName); 
     123      throw new sfInitializationException($error); 
     124    } 
     125 
     126    $componentToRun = 'execute'; 
     127  } 
     128 
     129  if (sfConfig::get('sf_logging_active')) $context->getLogger()->info('{PartialHelper} call "'.$moduleName.'->'.$componentToRun.'()'.'"'); 
     130 
     131  // run component 
     132  $retval = $componentInstance->$componentToRun(); 
     133 
     134  if ($retval != sfView::NONE) 
     135  { 
     136    // get component vars 
     137    $componentVars = $componentInstance->getVarHolder()->getAll(); 
     138 
     139    // include partial 
     140    return get_partial($moduleName.'/'.$componentName, $componentVars); 
     141  } 
     142
     143 
     144function include_partial($templateName, $vars = array()) 
     145
     146  echo get_partial($templateName, $vars); 
     147
     148 
     149function get_partial($templateName, $vars = array()) 
     150
     151  $context      = sfContext::getInstance(); 
     152  $cacheManager = $context->getViewCacheManager(); 
     153 
    107154  // partial is in another module? 
    108   $sep = strpos($name, '/'); 
     155  $sep = strpos($templateName, '/'); 
    109156  if ($sep) 
    110157  { 
    111     $type = substr($name, 0, $sep); 
    112     $filename = '_'.substr($name, $sep + 1).'.php'
     158    $moduleName   = substr($templateName, 0, $sep); 
     159    $templateName = substr($templateName, $sep + 1)
    113160  } 
    114161  else 
    115162  { 
    116     $type = ''; 
    117     $filename = '_'.$name.'.php'; 
    118   } 
    119  
    120   $context = sfContext::getInstance(); 
    121  
    122   $lastActionEntry  = $context->getActionStack()->getLastEntry(); 
    123   $firstActionEntry = $context->getActionStack()->getFirstEntry(); 
    124  
    125   // global variables 
    126   $vars['sf_context']      = $context; 
    127   $vars['sf_params']       = $context->getRequest()->getParameterHolder(); 
    128   $vars['sf_request']      = $context->getRequest(); 
    129   $vars['sf_user']         = $context->getUser(); 
    130   $vars['sf_last_module']  = $lastActionEntry->getModuleName(); 
    131   $vars['sf_last_action']  = $lastActionEntry->getActionName(); 
    132   $vars['sf_first_module'] = $firstActionEntry->getModuleName(); 
    133   $vars['sf_first_action'] = $firstActionEntry->getActionName(); 
    134  
    135   if (sfConfig::get('sf_use_flash')) 
    136   { 
    137     $sf_flash = new sfParameterHolder(); 
    138     $sf_flash->add($context->getUser()->getAttributeHolder()->getAll('symfony/flash')); 
    139     $vars['sf_flash'] = $sf_flash; 
    140   } 
    141  
    142   // local action variables 
    143   $action = $context->getActionStack()->getLastEntry()->getActionInstance(); 
    144   if (method_exists($action, 'getVars')) 
    145   { 
    146     $vars = array_merge($vars, $action->getVars()); 
    147   } 
    148  
    149   // render to client 
    150   if ($sep && $type == 'global') 
    151   { 
    152     $partial = sfConfig::get('sf_app_template_dir').DIRECTORY_SEPARATOR.$filename; 
    153   } 
    154   else if ($sep) 
    155   { 
    156     $partial = sfConfig::get('sf_app_module_dir').DIRECTORY_SEPARATOR.$type.DIRECTORY_SEPARATOR.sfConfig::get('sf_app_module_template_dir_name').DIRECTORY_SEPARATOR.$filename; 
    157   } 
    158   else 
    159   { 
    160     $current_module = sfContext::getInstance()->getActionStack()->getLastEntry()->getModuleName(); 
    161     $partial = sfConfig::get('sf_app_dir').DIRECTORY_SEPARATOR.sfConfig::get('sf_app_module_dir_name').DIRECTORY_SEPARATOR.$current_module.DIRECTORY_SEPARATOR.sfConfig::get('sf_app_module_template_dir_name').DIRECTORY_SEPARATOR.$filename; 
    162   } 
    163  
    164   if (!is_readable($partial)) 
    165   { 
    166     $ok = false; 
    167  
    168     $current_module = sfContext::getInstance()->getActionStack()->getLastEntry()->getModuleName(); 
    169  
    170     // search partial for generated templates in cache 
    171     $partial = sfConfig::get('sf_module_cache_dir').'/auto'.ucfirst($current_module).'/templates/'.$filename; 
    172     if (is_readable($partial)) 
    173     { 
    174       $ok = true; 
    175     } 
    176     else 
    177     { 
    178       // search partial in a symfony module directory 
    179       $partial = sfConfig::get('sf_symfony_data_dir').'/modules/'.$current_module.'/templates/'.$filename; 
    180       if (is_readable($partial)) 
     163    $moduleName = $context->getActionStack()->getLastEntry()->getModuleName(); 
     164  } 
     165  $actionName = '_'.$templateName; 
     166 
     167  if (sfConfig::get('sf_cache')) 
     168  { 
     169    $cacheKey = md5(serialize($vars)); 
     170    $uri = $moduleName.'/'.$actionName.'?key='.$cacheKey; 
     171 
     172    // register our cache configuration 
     173    $cacheConfigFile = $moduleName.'/'.sfConfig::get('sf_app_module_config_dir_name').'/cache.yml'; 
     174    if (is_readable(sfConfig::get('sf_app_module_dir').'/'.$cacheConfigFile)) 
     175    { 
     176      require(sfConfigCache::getInstance()->checkConfig(sfConfig::get('sf_app_module_dir_name').'/'.$cacheConfigFile)); 
     177    } 
     178 
     179    $retval = $cacheManager->get($uri, 'slot'); 
     180 
     181    if (sfConfig::get('sf_logging_active') && $cacheManager->isCacheable($uri, 'slot')) 
     182    { 
     183      $context->getLogger()->info(sprintf('{PartialHelper} cache for "%s" %s', $uri, ($retval !== null ? 'exists' : 'does not exist'))); 
     184    } 
     185 
     186    if ($retval !== null) 
     187    { 
     188      if (sfConfig::get('sf_web_debug')) 
    181189      { 
    182         $ok = true
     190        $retval = sfWebDebug::getInstance()->decorateContentWithDebug($uri, 'slot', $retval, false)
    183191      } 
    184     } 
    185  
    186     if (!$ok) 
    187     { 
    188       // the partial isn't readable 
    189       $error = sprintf('The partial "%s" does not exist or is unreadable', $filename); 
    190       throw new sfRenderException($error); 
    191     } 
    192   } 
    193  
    194   extract($vars); 
    195   require $partial; 
     192 
     193      return $retval; 
     194    } 
     195  } 
     196 
     197  $viewType = ($moduleName == 'global') ? sfView::GLOBAL_PARTIAL : sfView::PARTIAL; 
     198 
     199  $controller = $context->getController(); 
     200 
     201  // get original render mode 
     202  $renderMode = $controller->getRenderMode(); 
     203 
     204  // set render mode to var 
     205  $controller->setRenderMode(sfView::RENDER_VAR); 
     206 
     207  // get the view instance 
     208  $viewName     = $templateName.$viewType; 
     209  $viewInstance = $controller->getView($moduleName, $actionName, $viewName); 
     210 
     211  // initialize the view 
     212  if (!$viewInstance->initialize($context, $moduleName, $viewName)) 
     213  { 
     214    // view failed to initialize 
     215    $error = 'View initialization failed for module "%s", view "%sView"'; 
     216    $error = sprintf($error, $moduleName, $viewName); 
     217 
     218    throw new sfInitializationException($error); 
     219  } 
     220 
     221  // view initialization completed successfully 
     222  $viewInstance->execute(); 
     223 
     224  // no decorator 
     225  $viewInstance->setDecorator(false); 
     226 
     227  // render the partial template 
     228  $retval = $viewInstance->render($vars); 
     229 
     230  // put render mode back 
     231  $controller->setRenderMode($renderMode); 
     232 
     233  if (sfConfig::get('sf_cache')) 
     234  { 
     235    if ($retval !== null) 
     236    { 
     237      $saved = $cacheManager->set($retval, $uri, 'slot'); 
     238 
     239      if (sfConfig::get('sf_web_debug') && $saved) 
     240      { 
     241        $retval = sfWebDebug::getInstance()->decorateContentWithDebug($uri, 'slot', $retval, true); 
     242      } 
     243    } 
     244 
     245    if (sfConfig::get('sf_logging_active') && $saved) 
     246    { 
     247      $context->getLogger()->info(sprintf('{PartialHelper} save slot "%s - %s" in cache', $uri, $cacheKey)); 
     248    } 
     249  } 
     250 
     251  return $retval; 
    196252} 
    197253 
  • trunk/lib/request/sfWebRequest.class.php

    r1291 r1300  
    419419    $this->getParameterHolder()->addByRef($_POST); 
    420420 
     421    // move symfony parameters in a protected namespace (parameters prefixed with sf_) 
     422    foreach ($this->getParameterHolder()->getAll() as $key => $value) 
     423    { 
     424      if (stripos($key, 'sf_') !== false) 
     425      { 
     426        $this->getParameterHolder()->remove($key); 
     427        $this->setParameter($key, $value, 'symfony/request/sfWebRequest'); 
     428        unset($_GET[$key]); 
     429      } 
     430    } 
     431 
    421432    if (sfConfig::get('sf_logging_active')) 
    422433    { 
    423       $parameters = ''; 
    424       foreach ($this->getParameterHolder()->getAll() as $key => $value) 
    425       { 
    426         $parameters .= $key.' => "'.$value.'", '; 
    427       } 
    428  
    429       $this->getContext()->getLogger()->info('{sfWebRequest} request parameters { '.$parameters.'}'); 
    430     } 
    431  
    432     // move some parameters in other namespaces 
    433     $special_parameters = array( 
    434       'ignore_cache' => 'symfony/request/sfWebRequest', 
    435     ); 
    436     foreach ($special_parameters as $param => $namespace) 
    437     { 
    438       if ($this->hasParameter($param)) 
    439       { 
    440         $value = $this->getParameterHolder()->remove($param); 
    441         $this->setParameter($param, $value, $namespace); 
    442       } 
     434      $this->getContext()->getLogger()->info(sprintf('{sfWebRequest} request parameters %s', str_replace("\n", '', var_export($this->getParameterHolder()->getAll(), true)))); 
    443435    } 
    444436  } 
  • trunk/lib/response/sfWebResponse.class.php

    r1249 r1300  
    445445  } 
    446446 
     447  public function mergeProperties($response) 
     448  { 
     449    // add stylesheets 
     450    foreach (array('first', '', 'last') as $position) 
     451    { 
     452      $this->getParameterHolder()->add($response->getStylesheets($position), 'helper/asset/auto/stylesheet'.$position); 
     453    } 
     454 
     455    // add javascripts 
     456    foreach (array('first', '', 'last') as $position) 
     457    { 
     458      $this->getParameterHolder()->add($response->getJavascripts($position), 'helper/asset/auto/javascript'.$position); 
     459    } 
     460 
     461    // add headers 
     462    foreach ($response->getHttpHeaders() as $name => $values) 
     463    { 
     464      foreach ($values as $value) 
     465      { 
     466        $this->setHttpHeader($name, $value); 
     467      } 
     468    } 
     469  } 
     470 
    447471  /** 
    448472   * Execute the shutdown procedure. 
  • trunk/lib/view/sfPHPView.class.php

    r1197 r1300  
    2020class sfPHPView extends sfView 
    2121{ 
    22   protected static 
    23     $coreHelpersLoaded = 0; 
    24  
    2522  public function execute() 
    2623  { 
     
    2825 
    2926  /** 
    30    * Assigns some common variables to the template. 
     27   * Returns an array with some variables that will be accessible to the template. 
    3128   */ 
    3229  protected function getGlobalVars() 
     
    6158  protected function loadCoreAndStandardHelpers() 
    6259  { 
    63     if (self::$coreHelpersLoaded) 
     60    static $coreHelpersLoaded = 0; 
     61 
     62    if ($coreHelpersLoaded) 
    6463    { 
    6564      return; 
    6665    } 
    6766 
    68     self::$coreHelpersLoaded = 1; 
     67    $coreHelpersLoaded = 1; 
    6968 
    7069    $core_helpers = array('Helper', 'Url', 'Asset', 'Tag', 'Escaping'); 
     
    154153 
    155154    // store our current view 
    156     $actionStackEntry->setViewInstance($this); 
     155    if (!$actionStackEntry->getViewInstance()) 
     156    { 
     157      $actionStackEntry->setViewInstance($this); 
     158    } 
     159 
     160    // all directories to look for templates 
     161    $dirs = array( 
     162      // application 
     163      $this->getDirectory(), 
     164 
     165      // local plugin 
     166      sfConfig::get('sf_plugin_data_dir').'/modules/'.$this->moduleName.'/templates', 
     167 
     168      // core modules or global plugins 
     169      sfConfig::get('sf_symfony_data_dir').'/modules/'.$this->moduleName.'/templates', 
     170 
     171      // generated templates in cache 
     172      sfConfig::get('sf_module_cache_dir').'/auto'.ucfirst($this->moduleName).'/templates', 
     173    ); 
    157174 
    158175    // require our configuration 
     
    160177    require(sfConfigCache::getInstance()->checkConfig(sfConfig::get('sf_app_module_dir_name').'/'.$viewConfigFile)); 
    161178 
    162     $viewType = sfView::SUCCESS; 
    163     if (preg_match('/^'.$action->getActionName().'(.+)$/i', $this->viewName, $match)) 
    164     { 
    165       $viewType = $match[1]; 
    166       $templateFile = $templateName.$viewType.$this->extension; 
     179    if (preg_match('/^(.+?)'.sfView::GLOBAL_PARTIAL.'$/i', $this->viewName, $match)) 
     180    { 
     181      // global partial 
     182      $templateFile = '_'.$match[1].$this->extension; 
     183      $dirs = array(sfConfig::get('sf_app_template_dir')); 
     184    } 
     185    else if (preg_match('/^(.+?)'.sfView::PARTIAL.'$/i', $this->viewName, $match)) 
     186    { 
     187      // partial 
     188      $templateFile = '_'.$match[1].$this->extension; 
     189    } 
     190    else if (preg_match('/^'.$action->getActionName().'(.+)$/i', $this->viewName, $match)) 
     191    { 
     192      $templateFile = $templateName.$match[1].$this->extension; 
    167193    } 
    168194    else 
     
    175201 
    176202    // set template directory 
    177  
    178     // all directories to look for templates 
    179     $moduleName = $context->getModuleName(); 
    180     $dirs = array( 
    181       // application 
    182       $this->getDirectory(), 
    183  
    184       // local plugin 
    185       sfConfig::get('sf_plugin_data_dir').'/modules/'.$moduleName.'/templates', 
    186  
    187       // core modules or global plugins 
    188       sfConfig::get('sf_symfony_data_dir').'/modules/'.$moduleName.'/templates', 
    189  
    190       // generated templates in cache 
    191       sfConfig::get('sf_module_cache_dir').'/auto'.ucfirst($moduleName).'/templates', 
    192     ); 
    193  
    194203    foreach ($dirs as $dir) 
    195204    { 
     
    204213    if (sfConfig::get('sf_logging_active')) 
    205214    { 
    206       $context->getLogger()->info('{sfPHPView} execute view for template "'.$templateName.$viewType.$this->extension.'"'); 
     215      $context->getLogger()->info(sprintf('{sfPHPView} execute view for template "%s"', $templateFile)); 
    207216    } 
    208217  } 
     
    243252   *                the controller render mode is sfView::RENDER_VAR, otherwise null. 
    244253   */ 
    245   public function &render() 
    246   { 
    247     $template         = $this->getDirectory().'/'.$this->getTemplate(); 
    248     $actionStackEntry = $this->getContext()->getActionStack()->getLastEntry(); 
    249     $actionInstance   = $actionStackEntry->getActionInstance(); 
    250  
    251     $moduleName = $actionInstance->getModuleName(); 
    252     $actionName = $actionInstance->getActionName(); 
    253  
     254  public function &render($templateVars = null) 
     255  { 
    254256    $retval = null; 
    255257 
     258    $context = $this->getContext(); 
     259 
    256260    // get the render mode 
    257     $mode = $this->getContext()->getController()->getRenderMode(); 
     261    $mode = $context->getController()->getRenderMode(); 
    258262 
    259263    if ($mode != sfView::RENDER_NONE) 
     
    262266      if (sfConfig::get('sf_cache')) 
    263267      { 
    264         list($uri, $suffix) = $this->getContext()->getViewCacheManager()->getInternalUri('slot'); 
    265         $cache = $this->getContext()->getResponse()->getParameter($uri.'_'.$suffix, null, 'symfony/cache'); 
     268        $response = $context->getResponse(); 
     269        $key   = $response->getParameterHolder()->remove('current_key', 'symfony/cache/current'); 
     270        $cache = $response->getParameter($key, null, 'symfony/cache'); 
    266271        if ($cache !== null) 
    267272        { 
     
    269274          $retval = $cache['content']; 
    270275          $vars   = $cache['vars']; 
     276          $response->mergeProperties($cache['response']); 
    271277        } 
     278      } 
     279 
     280      // template variables 
     281      if ($templateVars === null) 
     282      { 
     283        $actionStackEntry = $context->getActionStack()->getLastEntry(); 
     284        $actionInstance   = $actionStackEntry->getActionInstance(); 
     285        $templateVars     = $actionInstance->getVarHolder()->getAll(); 
    272286      } 
    273287 
    274288      // assigns some variables to the template 
    275289      $this->attribute_holder->add($this->getGlobalVars()); 
    276       $this->attribute_holder->add($retval !== null ? $vars : $actionInstance->getVarHolder()->getAll()); 
     290      $this->attribute_holder->add($retval !== null ? $vars : $templateVars); 
    277291 
    278292      // render template if no cache 
     
    283297 
    284298        // render template file 
     299        $template = $this->getDirectory().'/'.$this->getTemplate(); 
    285300        $retval = $this->renderFile($template); 
    286301 
     
    291306        } 
    292307 
    293         if (sfConfig::get('sf_cache')
     308        if (sfConfig::get('sf_cache') && $key !== null
    294309        { 
    295310          $cache = array( 
    296311            'content'   => $retval, 
    297             'vars'      => $actionInstance->getVarHolder()->getAll()
     312            'vars'      => $templateVars
    298313            'view_name' => $this->viewName, 
     314            'response'  => $context->getResponse(), 
    299315          ); 
    300           $this->getContext()->getResponse()->setParameter($uri.'_'.$suffix, serialize($cache), 'symfony/cache'); 
     316          $response->setParameter($key, serialize($cache), 'symfony/cache'); 
     317 
     318          if (sfConfig::get('sf_web_debug')) 
     319          { 
     320            $retval = sfWebDebug::getInstance()->decorateContentWithDebug($key, '', $retval, true); 
     321          } 
    301322        } 
    302323      } 
     
    311332      if ($mode == sfView::RENDER_CLIENT) 
    312333      { 
    313         $this->getContext()->getResponse()->setContent($retval); 
     334        $context->getResponse()->setContent($retval); 
    314335      } 
    315336    } 
  • trunk/lib/view/sfView.class.php

    r1289 r1300  
    5555 
    5656  /** 
     57   * Render a partial template. 
     58   */ 
     59  const PARTIAL = 'Partial'; 
     60 
     61  /** 
     62   * Render a global partial template. 
     63   */ 
     64  const GLOBAL_PARTIAL = 'GlobalPartial'; 
     65 
     66  /** 
    5767   * Render the presentation to the client. 
    5868   */ 
     
    352362        { 
    353363          if ($request->hasError($name)) 
     364          { 
    354365            $this->setAttribute($name.'_error', $request->getError($name)); 
     366          } 
    355367          else 
    356368          { 
     
    383395 
    384396    // set the currently executing module's template directory as the default template directory 
    385     $module = $context->getModuleName(); 
    386  
    387     $this->decoratorDirectory = sfConfig::get('sf_app_module_dir').'/'.$module.'/'.sfConfig::get('sf_app_module_template_dir_name'); 
     397    $this->decoratorDirectory = sfConfig::get('sf_app_module_dir').'/'.$moduleName.'/'.sfConfig::get('sf_app_module_template_dir_name'); 
    388398    $this->directory          = $this->decoratorDirectory; 
    389399 
     
    442452  { 
    443453    return $this->decorator; 
     454  } 
     455 
     456  public function setDecorator ($boolean) 
     457  { 
     458    $this->decorator = (boolean) $boolean; 
    444459  } 
    445460 
     
    495510   * render the presentation directly to the client and null will be returned. 
    496511   * 
     512   * @param  array  An array with variables that will be extracted for the template 
     513   *                If empty, the current actions var holder will be extracted. 
    497514   * @return string A string representing the rendered presentation, if 
    498    *                the controller render mode is sfView::RENDER_VAR, otherwise 
    499    *                null. 
    500    */ 
    501   abstract function & render (); 
     515   *                the controller render mode is sfView::RENDER_VAR, otherwise null. 
     516   */ 
     517  abstract function & render ($templateVars = null); 
    502518 
    503519  /** 
  • trunk/lib/view/sfViewCacheManager.class.php

    r1024 r1300  
    1010 
    1111/** 
    12  * Cache class to cache the HTML results for Actions and Templates. 
     12 * Cache class to cache the HTML results for actions and templates. 
    1313 * 
    14  * This class is based on the PEAR_Cache_Liste class
     14 * This class users viewCacheClassName to serialize cache
    1515 * All cache files are stored in files in the [sf_root_dir].'/cache/'.[sf_app].'/html' directory. 
    1616 * To disable all caching, you can set to false [sf_cache] constant. 
     
    126126      // we add cache information based on slot configuration for this module/action 
    127127      $lifeTime = $this->getLifeTime($internalUri, 'slot'); 
    128       if ($lifeTime) 
    129       { 
    130         $this->addCache($moduleName, $actionName, $suffix, $lifeTime, $this->getClientLifeTime($internalUri, 'slot'), $this->getVary($internalUri, 'slot')); 
    131       } 
     128      $this->addCache($moduleName, $actionName, $suffix, $lifeTime, $this->getClientLifeTime($internalUri, 'slot'), $this->getVary($internalUri, 'slot')); 
    132129    } 
    133130 
     
    185182  } 
    186183 
    187   public function hasCacheConfig($internalUri, $suffix) 
     184  public function isCacheable($internalUri, $suffix) 
    188185  { 
    189186    list($route_name, $params) = $this->controller->convertUrlStringToParameters($internalUri); 
    190187    $entry = $params['module'].'_'.$params['action'].'_'.$suffix; 
    191188 
    192     if (isset($this->cacheConfig[$entry]) || isset($this->cacheConfig[$params['module'].'_DEFAULT_'.$suffix])) 
     189    if ( 
     190      (isset($this->cacheConfig[$entry]) && $this->cacheConfig[$entry]['lifeTime'] > 0) 
     191      || 
     192      (isset($this->cacheConfig[$params['module'].'_DEFAULT_'.$suffix]) && $this->cacheConfig[$params['module'].'_DEFAULT_'.$suffix]['lifeTime'] > 0) 
     193    ) 
    193194    { 
    194195      return true; 
     
    201202  { 
    202203    // no cache or no cache set for this action 
    203     if (!sfConfig::get('sf_cache') || !$this->hasCacheConfig($internalUri, $suffix)) 
     204    if (!sfConfig::get('sf_cache') || !$this->isCacheable($internalUri, $suffix) || $this->ignore()) 
    204205    { 
    205206      return null; 
     
    216217  public function has($internalUri, $suffix = 'slot') 
    217218  { 
    218     if (!sfConfig::get('sf_cache') || !$this->hasCacheConfig($internalUri, $suffix)) 
     219    if (!sfConfig::get('sf_cache') || !$this->isCacheable($internalUri, $suffix) || $this->ignore()) 
    219220    { 
    220221      return null; 
     
    229230  } 
    230231 
     232  protected function ignore() 
     233  { 
     234    // ignore cache parameter? (only available in debug mode) 
     235    if (sfConfig::get('sf_debug') && $this->getContext()->getRequest()->getParameter('sf_ignore_cache', false, 'symfony/request/sfWebRequest') == true) 
     236    { 
     237      if (sfConfig::get('sf_logging_active')) 
     238      { 
     239        $this->getContext()->getLogger()->info('{sfViewCacheManager} discard cache'); 
     240      } 
     241 
     242      return true; 
     243    } 
     244 
     245    return false; 
     246  } 
     247 
    231248  public function set($data, $internalUri, $suffix = 'slot') 
    232249  { 
    233     if (!sfConfig::get('sf_cache') || !$this->hasCacheConfig($internalUri, $suffix)) 
     250    if (!sfConfig::get('sf_cache') || !$this->isCacheable($internalUri, $suffix)) 
    234251    { 
    235252      return false; 
     
    302319  public function lastModified($internalUri, $suffix = 'slot') 
    303320  { 
    304     if (!sfConfig::get('sf_cache') || !$this->hasCacheConfig($internalUri, $suffix)) 
     321    if (!sfConfig::get('sf_cache') || !$this->isCacheable($internalUri, $suffix)) 
    305322    { 
    306323      return null; 

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting, and supporting several large Open-Source projects.