Development

Changeset 7725

You must first sign up to be able to contribute.

Changeset 7725

Show
Ignore:
Timestamp:
03/03/08 15:00:44 (1 year ago)
Author:
fabien
Message:

fixed caching of partial and components (now with css, js, and slots support) (closes #2685)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/lib/plugins/sfCompat10Plugin/lib/helper/PartialHelper.php

    r7724 r7725  
    118118      return $retval; 
    119119    } 
    120   } 
    121  
    122   $controller = $context->getController(); 
    123  
    124   if (!$controller->componentExists($moduleName, $componentName)) 
    125   { 
    126     // cannot find component 
    127     throw new sfConfigurationException(sprintf('The component does not exist: "%s", "%s".', $moduleName, $componentName)); 
    128   } 
    129  
    130   // create an instance of the action 
    131   $componentInstance = $controller->getComponent($moduleName, $componentName); 
    132  
    133   // load component's module config file 
    134   require(sfContext::getInstance()->getConfigCache()->checkConfig('modules/'.$moduleName.'/config/module.yml')); 
    135  
    136   $componentInstance->getVarHolder()->add($vars); 
    137  
    138   // dispatch component 
    139   $componentToRun = 'execute'.ucfirst($componentName); 
    140   if (!method_exists($componentInstance, $componentToRun)) 
    141   { 
    142     if (!method_exists($componentInstance, 'execute')) 
    143     { 
    144       // component not found 
    145       throw new sfInitializationException(sprintf('sfComponent initialization failed for module "%s", component "%s".', $moduleName, $componentName)); 
    146     } 
    147  
    148     $componentToRun = 'execute'; 
    149   } 
    150  
    151   if (sfConfig::get('sf_logging_enabled')) 
    152   { 
    153     $context->getEventDispatcher()->notify(new sfEvent(null, 'application.log', array(sprintf('Call "%s->%s()'.'"', $moduleName, $componentToRun)))); 
    154   } 
    155  
    156   // run component 
    157   if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled')) 
    158   { 
    159     $timer = sfTimerManager::getTimer(sprintf('Component "%s/%s"', $moduleName, $componentName)); 
    160   } 
    161  
    162   $retval = $componentInstance->$componentToRun($context->getRequest()); 
    163  
    164   if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled')) 
    165   { 
    166     $timer->addTime(); 
    167   } 
    168  
    169   if ($retval != sfView::NONE) 
     120    else 
     121    { 
     122      $mainResponse = $context->getResponse(); 
     123      $responseClass = get_class($mainResponse); 
     124      $context->setResponse($response = new $responseClass($context->getEventDispatcher(), $mainResponse->getOptions())); 
     125    } 
     126  } 
     127 
     128  $allVars = _call_component($moduleName, $componentName, $vars); 
     129 
     130  if (!is_null($allVars)) 
    170131  { 
    171132    // render 
    172133    $view = new sfPartialView($context, $moduleName, $actionName, ''); 
    173     $view->getAttributeHolder()->add($componentInstance->getVarHolder()->getAll()); 
     134    $view->getAttributeHolder()->add($allVars); 
    174135 
    175136    $retval = $view->render(); 
     
    178139    { 
    179140      $retval = $cacheManager->setPartialCache($moduleName, $actionName, $cacheManager->computeCacheKey($vars), $retval); 
     141 
     142      $context->setResponse($mainResponse); 
     143      $mainResponse->merge($response); 
    180144    } 
    181145 
     
    241205  { 
    242206    $cacheManager->registerConfiguration($moduleName); 
    243     $uri = '@sf_cache_partial?module='.$moduleName.'&action='.$actionName.'&sf_cache_key='.(isset($vars['sf_cache_key']) ? $vars['sf_cache_key'] : md5(serialize($vars))); 
    244     if ($retval = $cacheManager->getPartialCache($moduleName, $actionName, $cacheManager->computeCacheKey($vars))) 
     207 
     208    $cacheKey = $cacheManager->computeCacheKey($vars); 
     209    if ($retval = $cacheManager->getPartialCache($moduleName, $actionName, $cacheKey)) 
    245210    { 
    246211      return $retval; 
     212    } 
     213    else 
     214    { 
     215      $mainResponse = $context->getResponse(); 
     216      $responseClass = get_class($mainResponse); 
     217      $context->setResponse($response = new $responseClass($context->getEventDispatcher(), $mainResponse->getOptions())); 
    247218    } 
    248219  } 
     
    255226  if ($cacheManager) 
    256227  { 
    257     $retval = $cacheManager->setPartialCache($moduleName, $actionName, $cacheManager->computeCacheKey($vars), $retval); 
     228    $retval = $cacheManager->setPartialCache($moduleName, $actionName, $cacheKey, $retval); 
     229    $context->setResponse($mainResponse); 
     230    $mainResponse->merge($response); 
    258231  } 
    259232 
     
    385358  return isset($slots[$name]) ? $slots[$name] : ''; 
    386359} 
     360 
     361function _call_component($moduleName, $componentName, $vars) 
     362{ 
     363  $context = sfContext::getInstance(); 
     364 
     365  $controller = $context->getController(); 
     366 
     367  if (!$controller->componentExists($moduleName, $componentName)) 
     368  { 
     369    // cannot find component 
     370    throw new sfConfigurationException(sprintf('The component does not exist: "%s", "%s".', $moduleName, $componentName)); 
     371  } 
     372 
     373  // create an instance of the action 
     374  $componentInstance = $controller->getComponent($moduleName, $componentName); 
     375 
     376  // load component's module config file 
     377  require($context->getConfigCache()->checkConfig('modules/'.$moduleName.'/config/module.yml')); 
     378 
     379  $componentInstance->getVarHolder()->add($vars); 
     380 
     381  // dispatch component 
     382  $componentToRun = 'execute'.ucfirst($componentName); 
     383  if (!method_exists($componentInstance, $componentToRun)) 
     384  { 
     385    if (!method_exists($componentInstance, 'execute')) 
     386    { 
     387      // component not found 
     388      throw new sfInitializationException(sprintf('sfComponent initialization failed for module "%s", component "%s".', $moduleName, $componentName)); 
     389    } 
     390 
     391    $componentToRun = 'execute'; 
     392  } 
     393 
     394  if (sfConfig::get('sf_logging_enabled')) 
     395  { 
     396    $context->getEventDispatcher()->notify(new sfEvent(null, 'application.log', array(sprintf('Call "%s->%s()'.'"', $moduleName, $componentToRun)))); 
     397  } 
     398 
     399  // run component 
     400  if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled')) 
     401  { 
     402    $timer = sfTimerManager::getTimer(sprintf('Component "%s/%s"', $moduleName, $componentName)); 
     403  } 
     404 
     405  $retval = $componentInstance->$componentToRun($context->getRequest()); 
     406 
     407  if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled')) 
     408  { 
     409    $timer->addTime(); 
     410  } 
     411 
     412  return sfView::NONE == $retval ? null : $componentInstance->getVarHolder()->getAll(); 
     413} 
  • branches/1.1/test/functional/cacheTest.php

    r7307 r7725  
    303303sfConfig::set('sf_web_debug', false); 
    304304 
     305// check stylesheets, javascripts inclusions 
     306sfToolkit::clearDirectory(sfConfig::get('sf_app_cache_dir')); 
     307$b-> 
     308  get('/cache/multiBis')-> 
     309  isStatusCode(200)-> 
     310  isRequestParameter('module', 'cache')-> 
     311  isRequestParameter('action', 'multiBis')-> 
     312 
     313  // the first time (no cache) 
     314  checkResponseElement('link[href*="/main_css"]')-> 
     315  checkResponseElement('script[src*="/main_js"]')-> 
     316  checkResponseElement('link[href*="/partial_css"]')-> 
     317  checkResponseElement('script[src*="/partial_js"]')-> 
     318  checkResponseElement('link[href*="/another_partial_css"]')-> 
     319  checkResponseElement('script[src*="/another_partial_js"]')-> 
     320  checkResponseElement('link[href*="/component_css"]')-> 
     321  checkResponseElement('script[src*="/component_js"]')-> 
     322 
     323  get('/cache/multiBis')-> 
     324 
     325  // when in cache 
     326  checkResponseElement('link[href*="/main_css"]')-> 
     327  checkResponseElement('script[src*="/main_js"]')-> 
     328  checkResponseElement('link[href*="/partial_css"]')-> 
     329  checkResponseElement('script[src*="/partial_js"]')-> 
     330  checkResponseElement('link[href*="/another_partial_css"]')-> 
     331  checkResponseElement('script[src*="/another_partial_js"]')-> 
     332  checkResponseElement('link[href*="/component_css"]')-> 
     333  checkResponseElement('script[src*="/component_js"]') 
     334; 
     335 
     336$b-> 
     337  get('/cache/partial')-> 
     338  isStatusCode(200)-> 
     339  isRequestParameter('module', 'cache')-> 
     340  isRequestParameter('action', 'partial')-> 
     341 
     342  // only partial specific css and js are included 
     343  checkResponseElement('link[href*="/main_css"]', false)-> 
     344  checkResponseElement('script[src*="/main_js"]', false)-> 
     345  checkResponseElement('link[href*="/partial_css"]')-> 
     346  checkResponseElement('script[src*="/partial_js"]')-> 
     347  checkResponseElement('link[href*="/another_partial_css"]')-> 
     348  checkResponseElement('script[src*="/another_partial_js"]')-> 
     349  checkResponseElement('link[href*="/component_css"]', false)-> 
     350  checkResponseElement('script[src*="/component_js"]', false)-> 
     351 
     352  get('/cache/anotherPartial')-> 
     353  isStatusCode(200)-> 
     354  isRequestParameter('module', 'cache')-> 
     355  isRequestParameter('action', 'anotherPartial')-> 
     356 
     357  // only partial specific css and js are included 
     358  checkResponseElement('link[href*="/main_css"]', false)-> 
     359  checkResponseElement('script[src*="/main_js"]', false)-> 
     360  checkResponseElement('link[href*="/partial_css"]', false)-> 
     361  checkResponseElement('script[src*="/partial_js"]', false)-> 
     362  checkResponseElement('link[href*="/another_partial_css"]')-> 
     363  checkResponseElement('script[src*="/another_partial_js"]')-> 
     364  checkResponseElement('link[href*="/component_css"]', false)-> 
     365  checkResponseElement('script[src*="/component_js"]', false)-> 
     366 
     367  get('/cache/component')-> 
     368  isStatusCode(200)-> 
     369  isRequestParameter('module', 'cache')-> 
     370  isRequestParameter('action', 'component')-> 
     371 
     372  // only partial specific css and js are included 
     373  checkResponseElement('link[href*="/main_css"]', false)-> 
     374  checkResponseElement('script[src*="/main_js"]', false)-> 
     375  checkResponseElement('link[href*="/partial_css"]', false)-> 
     376  checkResponseElement('script[src*="/partial_js"]', false)-> 
     377  checkResponseElement('link[href*="/another_partial_css"]', false)-> 
     378  checkResponseElement('script[src*="/another_partial_js"]', false)-> 
     379  checkResponseElement('link[href*="/component_css"]')-> 
     380  checkResponseElement('script[src*="/component_js"]') 
     381; 
     382 
    305383// test with sfFileCache class (default) 
    306384$b->launch(); 
  • branches/1.1/test/functional/fixtures/project/apps/cache/modules/cache/actions/actions.class.php

    r7301 r7725  
    2929 
    3030  public function executeMultiBis() 
     31  { 
     32  } 
     33 
     34  public function executePartial() 
     35  { 
     36  } 
     37 
     38  public function executeAnotherPartial() 
     39  { 
     40  } 
     41 
     42  public function executeComponent() 
    3143  { 
    3244  } 
  • branches/1.1/test/functional/fixtures/project/apps/cache/modules/cache/templates/_cacheableComponent.php

    r2319 r7725  
     1<?php use_stylesheet('component_css') ?> 
     2<?php use_javascript('component_js') ?> 
     3 
    14<div class="cacheableComponent_<?php echo isset($varParam) ? $varParam : '' ?>_<?php echo isset($componentParam) ? $componentParam : '' ?>_<?php echo isset($requestParam) ? $requestParam : '' ?>">OK</div> 
  • branches/1.1/test/functional/fixtures/project/apps/cache/modules/cache/templates/_cacheablePartial.php

    r2321 r7725  
     1<?php use_stylesheet('partial_css') ?> 
     2<?php use_javascript('partial_js') ?> 
     3 
    14<div class="cacheablePartial_<?php echo isset($varParam) ? $varParam : '' ?>_<?php echo $sf_params->get('param') ?>">OK</div> 
     5 
     6<div id="anotherCacheablePartial"><?php include_partial('cache/anotherCacheablePartial') ?></div> 
  • branches/1.1/test/functional/fixtures/project/apps/cache/modules/cache/templates/multiBisSuccess.php

    r2320 r7725  
     1<?php use_stylesheet('main_css') ?> 
     2<?php use_javascript('main_js') ?> 
     3 
    14<div id="cacheablePartial"><?php include_partial('cache/cacheablePartial') ?></div> 
    25 

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.