MINOR: Remove now-obsolete old partial cache code

This commit is contained in:
Hamish Friedlander 2011-03-10 17:37:46 +13:00
parent 29c06edd23
commit 31c5bb24f7

View File

@ -586,7 +586,7 @@ class SSViewer {
} }
$scope = new SSViewer_DataPresenter($item, array('I18NNamespace' => basename($template))); $scope = new SSViewer_DataPresenter($item, array('I18NNamespace' => basename($template)));
$val = ""; $valStack = array(); $val = "";
include($cacheFile); include($cacheFile);
@ -702,256 +702,3 @@ class SSViewer_FromString extends SSViewer {
return $val; return $val;
} }
} }
/**
* Handle the parsing for cacheblock tags.
*
* Needs to be handled differently from the other tags, because cacheblock can take any number of arguments
*
* This shouldn't be used as an example of how to add functionality to SSViewer - the eventual plan is to re-write
* SSViewer using a proper parser (probably http://github.com/hafriedlander/php-peg), so that extra functionality
* can be added without relying on ad-hoc parsers like this.
*
* @package sapphire
* @subpackage view
*/
class SSViewer_PartialParser {
static $tag = '/< % [ \t]+ (cached|cacheblock|uncached|end_cached|end_cacheblock|end_uncached) [ \t]+ ([^%]+ [ \t]+)? % >/xS';
static $argument_splitter = '/^\s*
# The argument itself
(
(?P<conditional> if | unless ) | # The if or unless keybreak
(?P<property> (?P<identifier> \w+) \s* # A property lookup or a function call
( \( (?P<arguments> [^\)]*) \) )?
) |
(?P<sqstring> \' (\\\'|[^\'])+ \' ) | # A string surrounded by \'
(?P<dqstring> " (\\"|[^"])+ " ) # A string surrounded by "
)
# Some seperator after the argument
(
\s*(?P<comma>,)\s* | # A comma (maybe with whitespace before or after)
(?P<fullstop>\.) # A period (no whitespace before)
)?
/xS';
static function process($template, $content) {
$parser = new SSViewer_PartialParser($template, $content, 0);
$parser->parse();
return $parser->generate();
}
function __construct($template, $content, $offset) {
$this->template = $template;
$this->content = $content;
$this->offset = $offset;
$this->blocks = array();
}
function controlcheck($text) {
// NOP - hook for Cached_PartialParser
}
function parse() {
$current_tag_offset = 0;
while (preg_match(self::$tag, $this->content, $matches, PREG_OFFSET_CAPTURE, $this->offset)) {
$tag = $matches[1][0];
$startpos = $matches[0][1];
$endpos = $matches[0][1] + strlen($matches[0][0]);
switch($tag) {
case 'cached':
case 'uncached':
case 'cacheblock':
$pretext = substr($this->content, $this->offset, $startpos - $this->offset);
$this->controlcheck($pretext);
$this->blocks[] = $pretext;
if ($tag == 'cached' || $tag == 'cacheblock') {
list($keyparts, $conditional, $condition) = $this->parseargs(@$matches[2][0]);
$parser = new SSViewer_Cached_PartialParser($this->template, $this->content, $endpos, $keyparts, $conditional, $condition);
}
else {
$parser = new SSViewer_PartialParser($this->template, $this->content, $endpos);
}
$parser->parse();
$this->blocks[] = $parser;
$this->offset = $parser->offset;
break;
case 'end_cached':
case 'end_cacheblock':
case 'end_uncached':
$this->blocks[] = substr($this->content, $this->offset, $startpos - $this->offset);
$this->content = null;
$this->offset = $endpos;
return $this;
}
}
$this->blocks[] = substr($this->content, $this->offset);
$this->content = null;
}
function parseargs($string) {
preg_match_all(self::$argument_splitter, $string, $matches, PREG_SET_ORDER);
$parts = array();
$conditional = null; $condition = null;
$current = '$item->';
while (strlen($string) && preg_match(self::$argument_splitter, $string, $match)) {
$string = substr($string, strlen($match[0]));
// If this is a conditional keyword, break, and the next loop will grab the conditional
if (@$match['conditional']) {
$conditional = $match['conditional'];
continue;
}
// If it's a property lookup or a function call
if (@$match['property']) {
// Get the property
$what = $match['identifier'];
$args = array();
// Extract any arguments passed to the function call
if (@$match['arguments']) {
foreach (explode(',', $match['arguments']) as $arg) {
$args[] = is_numeric($arg) ? (string)$arg : '"'.$arg.'"';
}
}
$args = empty($args) ? 'null' : 'array('.implode(',',$args).')';
// If this fragment ended with '.', then there's another lookup coming, so return an obj for that lookup
if (@$match['fullstop']) {
$current .= "obj('$what', $args, true)->";
}
// Otherwise this is the end of the lookup chain, so add the resultant value to the key array and reset the key-get php fragement
else {
$accessor = $current . "XML_val('$what', $args, true)"; $current = '$item->';
// If we've hit a conditional already, this is the condition. Set it and be done.
if ($conditional) {
$condition = $accessor;
break;
}
// Otherwise we're another key component. Add it to array.
else $parts[] = $accessor;
}
}
// Else it's a quoted string of some kind
else if (@$match['sqstring']) $parts[] = $match['sqstring'];
else if (@$match['dqstring']) $parts[] = $match['dqstring'];
}
if ($conditional && !$condition) {
throw new Exception("You need to have a condition after the conditional $conditional in your cache block");
}
return array($parts, $conditional, $condition);
}
function generate() {
$res = array();
foreach ($this->blocks as $i => $block) {
if ($block instanceof SSViewer_PartialParser)
$res[] = $block->generate();
else {
$res[] = $block;
}
}
return implode('', $res);
}
}
/**
* @package sapphire
* @subpackage view
*/
class SSViewer_Cached_PartialParser extends SSViewer_PartialParser {
function __construct($template, $content, $offset, $keyparts, $conditional, $condition) {
$this->keyparts = $keyparts;
$this->conditional = $conditional;
$this->condition = $condition;
parent::__construct($template, $content, $offset);
}
function controlcheck($text) {
$ifs = preg_match_all('/<'.'% +if +/', $text, $matches);
$end_ifs = preg_match_all('/<'.'% +end_if +/', $text, $matches);
if ($ifs != $end_ifs) throw new Exception('You can\'t have cached or uncached blocks within condition structures');
$controls = preg_match_all('/<'.'% +control +/', $text, $matches);
$end_controls = preg_match_all('/<'.'% +end_control +/', $text, $matches);
if ($controls != $end_controls) throw new Exception('You can\'t have cached or uncached blocks within control structures');
}
function key() {
if (empty($this->keyparts)) return "''";
return 'sha1(' . implode(".'_'.", $this->keyparts) . ')';
}
function generate() {
$res = array();
$key = $this->key();
$condition = "";
switch ($this->conditional) {
case 'if':
$condition = "{$this->condition} && ";
break;
case 'unless':
$condition = "!({$this->condition}) && ";
break;
}
/* Output this set of blocks */
foreach ($this->blocks as $i => $block) {
if ($block instanceof SSViewer_PartialParser)
$res[] = $block->generate();
else {
// Include the template name and this cache block's current contents as a sha hash, so we get auto-seperation
// of cache blocks, and invalidation of the cache when the template changes
$partialkey = "'".sha1($this->template . $block)."_'.$key.'_$i'";
// Try to load from cache
$res[] = "<?\n".'if ('.$condition.' ($partial = $cache->load('.$partialkey.'))) $val .= $partial;'."\n";
// Cache miss - regenerate
$res[] = "else {\n";
$res[] = '$oldval = $val; $val = "";'."\n";
$res[] = "\n?>" . $block . "<?\n";
$res[] = $condition . ' $cache->save($val); $val = $oldval . $val ;'."\n";
$res[] = "}\n?>";
}
}
return implode('', $res);
}
}
function supressOutput() {
return "";
}
?>