Code Duplication    Length = 31-31 lines in 2 locations

view/SSTemplateParser.php 1 location

@@ 2938-2968 (lines=31) @@
2935
		$res['php'] .= $sub['php'];
2936
	}
2937
2938
	function CacheBlock_CacheBlockTemplate(&$res, $sub){
2939
		// Get the block counter
2940
		$block = ++$res['subblocks'];
2941
		// Build the key for this block from the global key (evaluated in a closure within the template),
2942
		// the passed cache key, the block index, and the sha hash of the template.
2943
		$res['php'] .= '$keyExpression = function() use ($scope, $cache) {' . PHP_EOL;
2944
		$res['php'] .= '$val = \'\';' . PHP_EOL;
2945
		if($globalKey = Config::inst()->get('SSViewer', 'global_key')) {
2946
			// Embed the code necessary to evaluate the globalKey directly into the template,
2947
			// so that SSTemplateParser only needs to be called during template regeneration.
2948
			// Warning: If the global key is changed, it's necessary to flush the template cache.
2949
			$parser = Injector::inst()->get('SSTemplateParser', false);
2950
			$result = $parser->compileString($globalKey, '', false, false);
2951
			if(!$result) throw new SSTemplateParseException('Unexpected problem parsing template', $parser);
2952
			$res['php'] .= $result . PHP_EOL;
2953
		}
2954
		$res['php'] .= 'return $val;' . PHP_EOL;
2955
		$res['php'] .= '};' . PHP_EOL;
2956
		$key = 'sha1($keyExpression())' // Global key
2957
			. '.\'_' . sha1($sub['php']) // sha of template
2958
			. (isset($res['key']) && $res['key'] ? "_'.sha1(".$res['key'].")" : "'") // Passed key
2959
			. ".'_$block'"; // block index
2960
		// Get any condition
2961
		$condition = isset($res['condition']) ? $res['condition'] : '';
2962
2963
		$res['php'] .= 'if ('.$condition.'($partial = $cache->load('.$key.'))) $val .= $partial;' . PHP_EOL;
2964
		$res['php'] .= 'else { $oldval = $val; $val = "";' . PHP_EOL;
2965
		$res['php'] .= $sub['php'] . PHP_EOL;
2966
		$res['php'] .= $condition . ' $cache->save($val); $val = $oldval . $val;' . PHP_EOL;
2967
		$res['php'] .= '}';
2968
	}
2969
2970
	/* OldTPart: "_t" N "(" N QuotedString (N "," N CallArguments)? N ")" N (";")? */
2971
	protected $match_OldTPart_typestack = array('OldTPart');

view/SSTemplateParser.php.inc 1 location

@@ 673-703 (lines=31) @@
670
		$res['php'] .= $sub['php'];
671
	}
672
673
	function CacheBlock_CacheBlockTemplate(&$res, $sub){
674
		// Get the block counter
675
		$block = ++$res['subblocks'];
676
		// Build the key for this block from the global key (evaluated in a closure within the template),
677
		// the passed cache key, the block index, and the sha hash of the template.
678
		$res['php'] .= '$keyExpression = function() use ($scope, $cache) {' . PHP_EOL;
679
		$res['php'] .= '$val = \'\';' . PHP_EOL;
680
		if($globalKey = Config::inst()->get('SSViewer', 'global_key')) {
681
			// Embed the code necessary to evaluate the globalKey directly into the template,
682
			// so that SSTemplateParser only needs to be called during template regeneration.
683
			// Warning: If the global key is changed, it's necessary to flush the template cache.
684
			$parser = Injector::inst()->get('SSTemplateParser', false);
685
			$result = $parser->compileString($globalKey, '', false, false);
686
			if(!$result) throw new SSTemplateParseException('Unexpected problem parsing template', $parser);
687
			$res['php'] .= $result . PHP_EOL;
688
		}
689
		$res['php'] .= 'return $val;' . PHP_EOL;
690
		$res['php'] .= '};' . PHP_EOL;
691
		$key = 'sha1($keyExpression())' // Global key
692
			. '.\'_' . sha1($sub['php']) // sha of template
693
			. (isset($res['key']) && $res['key'] ? "_'.sha1(".$res['key'].")" : "'") // Passed key
694
			. ".'_$block'"; // block index
695
		// Get any condition
696
		$condition = isset($res['condition']) ? $res['condition'] : '';
697
698
		$res['php'] .= 'if ('.$condition.'($partial = $cache->load('.$key.'))) $val .= $partial;' . PHP_EOL;
699
		$res['php'] .= 'else { $oldval = $val; $val = "";' . PHP_EOL;
700
		$res['php'] .= $sub['php'] . PHP_EOL;
701
		$res['php'] .= $condition . ' $cache->save($val); $val = $oldval . $val;' . PHP_EOL;
702
		$res['php'] .= '}';
703
	}
704
705
	/*!*
706