Skip to content

Instantly share code, notes, and snippets.

@davefreiman
Last active August 29, 2015 14:07
Show Gist options
  • Save davefreiman/305b1ac180fc24064079 to your computer and use it in GitHub Desktop.
Save davefreiman/305b1ac180fc24064079 to your computer and use it in GitHub Desktop.
protected function _processContent($content)
{
$containers = $this->_processContainers($content);
$isProcessed = empty($containers);
// renew session cookie
$sessionInfo = Enterprise_PageCache_Model_Cache::getCacheInstance()->load($this->getSessionInfoCacheId());
if ($sessionInfo) {
$sessionInfo = unserialize($sessionInfo);
foreach ($sessionInfo as $cookieName => $cookieInfo) {
if (isset($_COOKIE[$cookieName]) && isset($cookieInfo['lifetime'])
&& isset($cookieInfo['path']) && isset($cookieInfo['domain'])
&& isset($cookieInfo['secure']) && isset($cookieInfo['httponly'])
) {
$lifeTime = (0 == $cookieInfo['lifetime']) ? 0 : time() + $cookieInfo['lifetime'];
setcookie($cookieName, $_COOKIE[$cookieName], $lifeTime,
$cookieInfo['path'], $cookieInfo['domain'],
$cookieInfo['secure'], $cookieInfo['httponly']
);
}
}
} else {
$isProcessed = false;
}
/**
* restore session_id in content whether content is completely processed or not
*/
$sidCookieName = $this->getMetadata('sid_cookie_name');
$sidCookieValue = $sidCookieName && isset($_COOKIE[$sidCookieName]) ? $_COOKIE[$sidCookieName] : '';
Enterprise_PageCache_Helper_Url::restoreSid($content, $sidCookieValue);
if ($isProcessed) {
return $content;
} else {
Mage::register('cached_page_content', $content);
Mage::register('cached_page_containers', $containers);
Mage::app()->getRequest()
->setModuleName('pagecache')
->setControllerName('request')
->setActionName('process')
->isStraight(true);
// restore original routing info
$routingInfo = array(
'aliases' => $this->getMetadata('routing_aliases'),
'requested_route' => $this->getMetadata('routing_requested_route'),
'requested_controller' => $this->getMetadata('routing_requested_controller'),
'requested_action' => $this->getMetadata('routing_requested_action')
);
Mage::app()->getRequest()->setRoutingInfo($routingInfo);
return false;
}
}
protected function _processContainers(&$content)
{
$placeholders = array();
preg_match_all(
Enterprise_PageCache_Model_Container_Placeholder::HTML_NAME_PATTERN,
$content, $placeholders, PREG_PATTERN_ORDER
);
$placeholders = array_unique($placeholders[1]);
$containers = array();
foreach ($placeholders as $definition) {
$placeholder = new Enterprise_PageCache_Model_Container_Placeholder($definition);
$container = $placeholder->getContainerClass();
if (!$container) {
continue;
}
$container = new $container($placeholder);
$container->setProcessor($this);
if (!$container->applyWithoutApp($content)) {
$containers[] = $container;
} else {
preg_match($placeholder->getPattern(), $content, $matches);
if (array_key_exists(1,$matches)) {
$containers = array_merge($this->_processContainers($matches[1]), $containers);
$content = preg_replace($placeholder->getPattern(), str_replace('$', '\\$', $matches[1]), $content);
}
}
}
return $containers;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment